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
(ns dtw.core) | |
(defn distance [a b] | |
(Math/Pow (- a b) 2)) | |
(defn dtw [s t] | |
(let [n (count s) | |
m (count t) | |
bottom (apply hash-map | |
(mapcat (fn [i] [[i 0] Double/PositiveInfinity]) |
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
dtwMatrix2[s_List, t_List] := | |
With[{n = Length@s, m = Length@t}, | |
Fold[ | |
{acc, pos} \[Function] pos /. {i_, j_} :> | |
ReplacePart[acc, | |
pos -> (s[[i - 1]] - t[[j - 1]])^2 + | |
Min[Part[acc, ##] & @@@ {{i - 1, j}, {i, j - 1}, {i - 1, | |
j - 1}}]], | |
SparseArray[{{1, 1} -> | |
0, {1, _} -> \[Infinity], {_, 1} -> \[Infinity]}, {n, m} + 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
(defn tensor [init dims] | |
(reduce #(vec (repeat %2 %1)) init (reverse dims))) | |
(defn tensor-2 [init [d & dims]] | |
(vec (repeat d (if dims (tensor-2 init dims) init)))) |
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
SetAttributes[let, HoldAll] | |
let[bndgs_, expr_] := | |
Activate[ | |
Fold[ | |
{acc, bndg} \[Function] Inactive[With][{bndg}, acc], | |
Inactivate[expr], | |
Reverse[Inactivate[bndgs]]]] |
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 screw-up-some-collections-1 [xs ys] | |
(reduce | |
(fn [acc1, x] | |
(conj acc1 | |
(reduce | |
(fn [acc2, y] | |
(conj acc2 [x y])) | |
[] | |
ys))) | |
[] |
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
(time | |
(loop [i 1000000] | |
(if (> i 0) | |
(recur (dec i)) | |
i))) |
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 some-nums (vec (range 100000))) | |
(time (do (reduce conj '() some-nums) nil)) |
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 some-other-nums (doall (range 100000))) | |
(let [f (fn [x y] :bla)] | |
(time (do (reduce f '() some-other-nums) nil))) |
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
(let [ns 'arcadia.core] | |
(require ns) | |
(in-ns ns) | |
(use | |
'arcadia.core | |
'arcadia.hydrate | |
'clojure.pprint | |
'clojure.repl)) |
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 time-it | |
"Signature slightly awkward to resemble that of time-it-mac" | |
([f] (time-it f 1)) | |
([n f] | |
(. clojure.lang.RT (StartStopwatch)) | |
(dotimes [_ n] (f)) | |
(let [t (. clojure.lang.RT StopStopwatch)] | |
(/ t n)))) | |
(defmacro time-it-mac |