Skip to content

Instantly share code, notes, and snippets.

@mfikes

mfikes/tree-seq.clj

Last active May 22, 2020
Embed
What would you like to do?
Directly-reducible PoC in Clojure
(defn nth' [coll n]
(transduce (drop n) (completing #(reduced %2)) nil coll))
(defn tree-seq'
[branch? children root]
(eduction (take-while some?) (map first)
(iterate (fn [[node pair]]
(when-some [[[node' & r] cont] (if (branch? node)
(if-some [cs (not-empty (children node))]
[cs pair]
pair)
pair)]
(if (some? r)
[node' [r cont]]
[node' cont])))
[root nil])))
(time (dotimes [x 10000] (nth' (tree-seq seq? identity (repeat 10 (repeat 100 [1 2 (repeat 100 3)]))) 1000)))
;; "Elapsed time: 6161.53902 msecs"
(time (dotimes [x 10000] (nth' (tree-seq' seq? identity (repeat 10 (repeat 100 [1 2 (repeat 100 3)]))) 1000)))
;; "Elapsed time: 2830.583306 msecs"
@green-coder

This comment has been minimized.

Copy link

@green-coder green-coder commented Feb 19, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment