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
;;; | |
;;; see https://www.reddit.com/r/Clojure/comments/gcry9d/why_does_my_clojure_implementation_of_conways/ | |
;;; for initial implementation | |
;;; | |
(ns gol.core | |
(:gen-class) | |
(:import | |
[java.awt Color Graphics]) | |
(:use seesaw.core |
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- pair | |
[k v] | |
(clojure.lang.MapEntry. k v)) | |
(defn prod | |
[xs ys] | |
(persistent! | |
(reduce | |
(fn [acc x] | |
(reduce |
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
:int? 1 vmap-into 314.527436 ns | |
:int? 1 vmap-red 124.050875 ns | |
:int? 1 vmap-ret 142.164359 ns | |
:keyword? 1 vmap-into 291.031109 ns | |
:keyword? 1 vmap-red 113.564282 ns | |
:keyword? 1 vmap-ret 150.486043 ns | |
:string? 1 vmap-into 339.743369 ns | |
:string? 1 vmap-red 119.235523 ns | |
:string? 1 vmap-ret 156.019188 ns | |
:int? 2 vmap-into 405.677769 ns |
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 either-> | |
[expr & clauses] | |
(assert (zero? (rem (count clauses) 3))) | |
(let [g (gensym) | |
steps (map (fn [[test then else]] `(if (reduced? ~g) | |
~g | |
(if (-> ~g ~test) | |
(-> ~g ~then) | |
(reduced (-> ~g ~else))))) | |
(partition 3 clauses))] |
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 completing2 | |
([f] (completing2 f identity)) | |
([f cf] | |
(fn | |
([] (f)) | |
([x] (cf x)) | |
([m k v] (f m k v))))) | |
(defn transduce-kv | |
([xform f coll] (transduce-kv xform f (f) coll)) |
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 |
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
(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
(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
(defn chat | |
[^CharSequence cs index] | |
(.charAt cs ^int index)) | |
(defn strlen | |
[^CharSequence s] | |
(.length s)) | |
(defn trie |