Skip to content

Instantly share code, notes, and snippets.

@amalloy
Last active August 29, 2015 14:13
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 amalloy/8fdfdda5160581245f23 to your computer and use it in GitHub Desktop.
Save amalloy/8fdfdda5160581245f23 to your computer and use it in GitHub Desktop.
(fn parens [n]
(set
(map (partial apply str)
((fn lazy-recur [open close]
(lazy-seq
(concat (when (pos? open)
(map #(cons \( %)
(lazy-recur (dec open) (inc close))))
(when (pos? close)
(map #(cons \) %)
(lazy-recur open (dec close))))
(when (= [0 0] [open close])
[""]))))
n 0))))
;; and much prettier with flatland.useful.seq/lazy-loop, of course:
(fn parens [n]
(set
(map (partial apply str)
(lazy-loop [open n, close 0]
(concat (when (pos? open)
(map #(cons \( %)
(lazy-recur (dec open) (inc close))))
(when (pos? close)
(map #(cons \) %)
(lazy-recur open (dec close))))
(when (= [0 0] [open close])
[""]))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment