-
-
Save amalloy/8fdfdda5160581245f23 to your computer and use it in GitHub Desktop.
This file contains 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
(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