Skip to content

Instantly share code, notes, and snippets.

@sztamas
Last active Dec 22, 2015
Embed
What would you like to do?
(defn product
([] [])
([xs] (if (empty? xs) [] (seq xs)))
([xs & rest]
(for [x xs p (apply product rest)] (flatten (list x p)))))
(defn endless-product [xs]
(let [repeat-xs (map #(repeat % xs) (drop 1 (range)))]
(apply concat (map #(apply product %) repeat-xs))))
(comment
(take 13 (endless-product "abc"))
'(\a \b \c (\a \a) (\a \b) (\a \c) (\b \a) (\b \b) (\b \c) (\c \a) (\c \b) (\c \c) (\a \a \a))
(take 2 (drop 10000 (endless-product "abc")))
'((\a \a \a \a \b \c \c \c \b) (\a \a \a \a \b \c \c \c \c))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment