This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmacro defnormalizer | |
| [name m] | |
| {:pre (map? m)} | |
| (let [arg 'rec | |
| body | |
| (reduce-kv | |
| (fn [m k v] | |
| ((if (sequential? v) assoc-in assoc) | |
| m | |
| v |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defmacro maybe-apply-> | |
| [x & pfs] | |
| (assert (even? (count pfs))) | |
| (let [g (gensym) | |
| bs (mapcat (fn [[p f]] `(~g (if (-> ~g ~p) (-> ~g ~f) ~g))) (partition 2 pfs))] | |
| `(let [~g ~x | |
| ~@bs] | |
| ~g))) | |
| ;;; example: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (import '[javax.xml.bind DatatypeConverter]) | |
| (defn hex->bin [x] (DatatypeConverter/parseHexBinary x)) | |
| (defn bin->hex [x] (DatatypeConverter/printHexBinary x)) | |
| (def bin (type (hex->bin "a1"))) | |
| (defmethod print-method bin | |
| [v ^java.io.Writer w] | |
| (.write w "#x \"") | |
| (.write w (bin->hex v)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is just a contrived example | |
| sudo docker run -e "TOKEN=$TOKEN" --rm -v "$PWD":/usr/src/app -w /usr/src/app clojure:openjdk-14-lein-2.9.3 lein do clean, test, uberjar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn etl | |
| "Perform ETL flow with parallelism `n`. | |
| Will return a channel which will block until the job is done. | |
| Receives the following arguments: | |
| - `prepare-fn`: returns an input collection from `in`. | |
| - `xf`: transducer, will perform the E, T stages. | |
| - `rf`: Reducing function, will combine results in some manner. | |
| - `init`: initial value to reduce xf results into. | |
| - `post-fn`: perform final operation on value returned after reduction | |
| - `in`: input argument to process. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn chat | |
| [^CharSequence cs index] | |
| (.charAt cs ^int index)) | |
| (defn strlen | |
| [^CharSequence s] | |
| (.length s)) | |
| (defn trie |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require '[clojure.core.protocols :as p]) | |
| (deftype Concatenation [coll1 coll2] | |
| clojure.lang.IReduceInit | |
| (reduce [_ f init] | |
| (reduce f coll2 (reduce f init coll1 ))) | |
| clojure.lang.Seqable | |
| (seq [this] (concat coll1 coll2))) | |
| (defn concatenation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (definline pair | |
| [x y] | |
| `(clojure.lang.MapEntry/create ~x ~y)) | |
| (defn create-state-monad | |
| [init] | |
| (fn [f in] | |
| (let [state' (f init in)] | |
| (pair (create-state-monad state') state')))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (import '[java.net URLEncoder]) | |
| (definline url-encode | |
| [s] | |
| `(URLEncoder/encode (str ~s) "utf8")) | |
| (defn sb-qs-kvrf | |
| [^StringBuilder sb k v] | |
| (let [k (url-encode (name k)) | |
| v (url-encode v)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn fold-by ;;; similar to group-by | |
| ([kf ;;; Key function to group on | |
| op ;;; operation how to combine two elements under the same key | |
| coll] ;; collection of maps | |
| (persistent! | |
| (reduce | |
| (fn [ret x] ;; ret is a map from (kf x) to xs, similar to the result of group-by | |
| (let [k (kf x) ;; derive the key | |
| v (get ret k)] ;; get the value already associated with the key if it exists | |
| (assoc! ret |