This file contains 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 vector-map-slow | |
"A version of map that only works on vectors" | |
[f v] | |
(loop [i 0 out [] n (count v)] | |
(if (<= n i) | |
out | |
(recur (inc i) (conj out (f (v i))) n)))) |
This file contains 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 vector-map-transient | |
"A version of map that only works on vectors" | |
[f v] | |
(loop [i 0 out (transient []) n (count v)] | |
(if (<= n i) | |
(persistent! out) | |
(recur (inc i) (conj! out (f (v i))) n)))) |
This file contains 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
(mac accum (accfn . body) | |
(w/uniq gacc | |
`(withs (,gacc nil ,accfn [push _ ,gacc]) | |
,@body | |
(rev ,gacc)))) | |
(def keys (h) | |
(accum a (each (k v) h (a k)))) | |
(def vals (h) |
This file contains 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 accum | |
[f body] | |
`(with-local-vars [acc# (transient []) | |
~f (fn [x#](conj! (var-get acc#) x#))] | |
~body | |
(persistent! (var-get acc#)))) | |
(defn my-keys [m] (accum a (doseq [[k v] m] (a k)))) | |
(defn my-vals [m] (accum a (doseq [[k v] m] (a k)))) |
This file contains 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 rec-seq | |
[binding-name & body] | |
`(let [s# (atom nil)] | |
(reset! s# (lazy-seq (let [~binding-name @s#] ~@body))))) | |
(defmacro rec-cat | |
[binding-name & exprs] | |
`(rec-seq ~binding-name (lazy-cat ~@exprs))) | |
;; Fibonacci |
This file contains 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
;; The original definition | |
(rec-cat s [2 1] (map + s (next s))) | |
;; Expanding out | |
(let [s# (atom nil)] | |
(reset! s# (lazy-seq | |
(let [s @s#] | |
(lazy-cat | |
[2 1] | |
(map + s (next s))))))) |
This file contains 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 a | |
[k x1 x2 x3 x4 x5] | |
(letfn [(b [] | |
(reset! k (dec @k)) | |
(a (atom @k) b x1 x2 x3 x4))] | |
(if (<= @k 0) | |
(+ (x4) (x5)) | |
(b)))) | |
(defn man-or-boy |
This file contains 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
var width; | |
var height; | |
var cellSize; | |
var cellCount; | |
var grid; | |
var ctx; | |
var canvas; | |
var OFF = 0; | |
var ON = 1; |
This file contains 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
module Database.Redis.ConsistentHash where | |
import Database.Redis.Protocol | |
import Database.Redis.Key | |
import Database.Redis.Serializable | |
import Data.List (sort,delete,(\\),findIndex,(!!)) | |
import Data.Map (Map) | |
import qualified Data.Map as Map | |
import Data.Digest.Pure.SHA |
This file contains 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 Database.Redis.Protocol | |
import Database.Redis.ConsistentHash | |
import Criterion.Main | |
import System.IO | |
main = defaultMain [ | |
bgroup "con-hash" [ bench "getNode \"banana\"" $ whnf lookupServer "banana" ] | |
] |