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
(def fib-var-obj | |
(fn [x] | |
(if (< x 2) | |
1 | |
(+ (fib-var-obj (- x 1)) | |
(fib-var-obj (- x 2)))))) | |
(defn fib-lex-obj [x] | |
(if (< x 2) | |
1 |
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 <- [& body] | |
`(-> ~(last body) ~@(butlast body))) | |
(deftest <--test | |
(is (= [2 3] | |
(-> {1 1} | |
(assoc 3 4) | |
(update-in [1] inc) | |
(->> (map-vals dec) | |
(map-keys inc) |
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
;; Take a map {:xs xs} and return a map | |
;; of simple univariate statistics of xs | |
(defn stats [{:keys [xs]}] | |
(let [n (count xs) | |
m (/ (sum identity xs) n) | |
m2 (/ (sum #(* % %) xs) n) | |
v (- m2 (* m m))] | |
{:n n ; count | |
:m m ; mean | |
:m2 m2 ; mean square |
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
(def almost-stats-graph | |
{:n (fn [xs] (count xs)) | |
:m (fn [xs n] (/ (sum identity xs) n)) | |
:m2 (fn [xs n] (/ (sum #(* % %) xs) n)) | |
:v (fn [m m2] (- m2 (* m m)))}) |
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
(def stats-graph | |
{:n (fnk [xs] (count xs)) | |
:m (fnk [xs n] (/ (sum identity xs) n)) | |
:m2 (fnk [xs n] (/ (sum #(* % %) xs) n)) | |
:v (fnk [m m2] (- m2 (* m m)))}) |
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
(defnk foo [x y [s 1]] | |
(+ x (* y s))) | |
(foo {:x 2 :y 3 :s 2}) | |
;; ==> 8 | |
(foo {:x 2 :y 3}) | |
;; ==> 5 | |
(foo {:x 2}) |
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
;; Functionally identical to (defn stats ...) above | |
(def stats (graph/eager-compile stats-graph)) | |
(stats {:xs [1 2 3 6]}) | |
; ==> {:n 4 | |
; :m 3 | |
; :m2 12.5 | |
; :v 3.5) | |
;; Result is error checked |
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
(def lazy-stats (graph/lazy-compile stats-graph)) | |
(:m (lazy-stats {:xs [1 2 3 6]})) | |
; ==> 3 | |
; WIN: :m2 and :v are not computed. | |
(def par-stats (graph/parallel-compile stats-graph)) | |
(:v (par-stats {:xs [1 2 3 6]})) |
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 observe-graph [g record-node-time!] | |
(into {} | |
(for [[k f] g] | |
[k | |
(with-meta | |
(fn [m] | |
(let [t0 (System/nanoTime) | |
v (f m) | |
t1 (System/nanoTime)] | |
(record-node-time! k (- t1 t0)) |
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
(defnk foo [x y [s 1]] | |
(+ x (* y s))) | |
(foo {:x 2 :y 3 :s 2}) | |
; ==> 8 | |
;; 's' defaults to 1 | |
(foo {:x 2 :y 3}) | |
; ==> 5 |
OlderNewer