Skip to content

Instantly share code, notes, and snippets.

@scotthaleen
Created February 27, 2020 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scotthaleen/77b60a6b4a3a95e338fe513e301be27e to your computer and use it in GitHub Desktop.
Save scotthaleen/77b60a6b4a3a95e338fe513e301be27e to your computer and use it in GitHub Desktop.
(defn flattener
[input flattened?]
(((fn [f] (f f))
(fn [f]
(fn [x xs]
(if (flattened? x)
(cons x ((f f) (first xs) (rest xs)))
(if (empty? x)
xs
((f f) (first x) ((f f) (rest x) xs)))))))
(first input) (rest input)))
(def tree [1
[2 3
[4
[[5 6] 8]]]
[9 4]
[10 20]
[[5]]])
(flattener tree integer?)
;;=> (1 2 3 4 5 6 8 9 4 10 20 5)
;;y-combinator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment