Skip to content

Instantly share code, notes, and snippets.

@bsless
bsless / gol.clj
Created May 4, 2020 14:40
Game of life in Clojure + seesaw, based on reddit thread, see comments
;;;
;;; 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
@bsless
bsless / prod.clj
Created May 26, 2020 12:40
Eager cartesian product between two seqs in Clojure
(defn- pair
[k v]
(clojure.lang.MapEntry. k v))
(defn prod
[xs ys]
(persistent!
(reduce
(fn [acc x]
(reduce
@bsless
bsless / results.txt
Last active June 6, 2020 19:58
testing different implementations of mapping over map values
: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
@bsless
bsless / either.clj
Last active June 16, 2020 15:29
Either / ROP threading macro. Clojury
(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))]
@bsless
bsless / transduce-kv.clj
Last active August 3, 2020 07:18
Transduce maps natively, because why not
(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))
@bsless
bsless / foldmaps.clj
Last active October 21, 2020 06:57
Annotated version for Oded
(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
@bsless
bsless / fast-query-string.clj
Created July 31, 2020 13:15
Build up a simple query string really fast
(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)]
@bsless
bsless / minimal-state-monad.clj
Last active August 30, 2020 07:04
The bare minimum to create a state monad in Clojure. No good reason, really.
(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'))))
@bsless
bsless / concatenation.clj
Created August 4, 2020 06:40
sort of cons-ing of sequences
(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
@bsless
bsless / trie.clj
Last active August 13, 2020 05:58
build a trie from strings
(defn chat
[^CharSequence cs index]
(.charAt cs ^int index))
(defn strlen
[^CharSequence s]
(.length s))
(defn trie