Skip to content

Instantly share code, notes, and snippets.

@mfikes
Last active May 22, 2020 21:03
Show Gist options
  • Save mfikes/cc1d1fac47e7dc112b0b9f4e3de11eae to your computer and use it in GitHub Desktop.
Save mfikes/cc1d1fac47e7dc112b0b9f4e3de11eae to your computer and use it in GitHub Desktop.
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
Copy link

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