Skip to content

Instantly share code, notes, and snippets.

View rauhs's full-sized avatar

Andre W. rauhs

  • Bavaria, Germany
View GitHub Profile
(ns xyz.utils.css-parser
"
Generates a bunch of keywords from parsing a CSS files.
Useful to get autocompletion when using Cursive (or other editors?)
Then you can write you cljs/sablono code like this:
[:div {:class [:mdl-chip--contact
:mdl-chip--deletable] (etc)
"
(:require [clojure.java.io :as io]
(defn obj->enums
"Takes a js object and returns all keys that map to a string or a number (thus constant)"
[obj]
(sort
(mapv first
(js->clj
(goog.object/filter
obj
(fn [v k]
(ns utils.styler
"
A macro namespace that generates classes on the fly for usage in cljs.
# Motivation
https://ryantsao.com/blog/virtual-css-with-styletron
# Synopsis:
In your cljs files you can use inline styles:

What?

This is a sketch on a new/better macro for creating components

Requirement

  • Should provide an "open" (defmulti) function that can be extended by the user with his/her options.
  • Local state should be first class
  • Allow easy inspection of mounted component's state
  • Should not introduce new syntax, Cursive and other IDEs should understand the syntax. So it should look like a defn
(ns schablono.compiler
(:require
[schablono.normalize :as normalize]
[schablono.util :refer :all])
(:import
(clojure.lang Keyword)))
(def ^:dynamic *emit-inline* true)
(defmulti compile-react
(defn- gen-horner
[x coeffs]
(let [[p & r] (seq coeffs)]
`(+ ~p ~(if (empty? r)
x
`(* ~x ~(gen-horner x r))))))
(defmacro horner
"Generates code:
p[0] + x * (p[1] + x * (...))"
(defn sort-by-fast
"Returns a sorted sequence of the items in coll, where the sort
order is determined by comparing (keyfn item). Comp can be
boolean-valued comparison funcion, or a -/0/+ valued comparator.
Comp defaults to compare."
([keyfn coll]
(sort-by-fast keyfn compare coll))
([keyfn comp coll]
(let [arr (to-array coll)
;; Hack: access private function:
(defn sort-by-
"A faster sort-by for clojure.
See http://dev.clojure.org/jira/browse/CLJ-1402"
([keyfn coll]
(sort-by- keyfn compare coll))
([keyfn ^java.util.Comparator comp coll]
(if (seq coll)
(let [ary (object-array (count coll))]
(loop [i 0 xs (seq coll)]
(when xs
(defmacro amap-inplace-new
"Maps an expression across an array a, using an index named idx, and
element el:
(amap-inplace [[idx el] arr]
[idx el])"
[[[idx el] arr] & body]
`(let [a# ~arr]
(dotimes [~idx (alength a#)]
(let [~el (aget a# ~idx)]
(aset a# ~idx (do ~@body))))))

Synopsis

Implement an efficient datastructure that allows FAST access to the nth datom of a datomic index. This allows getting the "most recent post", "most popular post" etc.

;; API: to implement:
(nth-datoms db :avet :post/date 2284) ;; == (drop 2284 (d/datoms ...))
(rdatoms db :avet :post/date 50) ;; 50 last datoms of the index
(rdatoms-skip db :avet :post/date 150 50) ;; skip 150 datoms, get 50.