diff --git a/src/org/acooke/lab/perms.clj b/src/org/acooke/lab/perms.clj index b2ce876..10ddef0 100644 --- a/src/org/acooke/lab/perms.clj +++ b/src/org/acooke/lab/perms.clj @@ -31,3 +31,15 @@ (let [slow (cons "" (lazy-seq delayed))] (def delayed (helper slow)) delayed)) + +; http://stackoverflow.com/questions/11567625/tying-the-knot-in-clojure-circular-references-without-explicit-ugly-mutation + +(defn stream4 [seed] + (defn helper [slow] + (apply concat (map (fn [c] (map #(str c %) seed)) slow))) + (apply concat (iterate helper seed))) + +(defn stream5 [seed] + (defn helper [slow] + (mapcat (fn [c] (map #(str c %) seed)) slow)) + (apply concat (iterate helper seed))) diff --git a/test/org/acooke/lab/perms-test.clj b/test/org/acooke/lab/perms-test.clj index e5aeedd..464b6a9 100644 --- a/test/org/acooke/lab/perms-test.clj +++ b/test/org/acooke/lab/perms-test.clj @@ -50,4 +50,36 @@ (let [s (take 25 (stream3 ["a" "b" "c"]))] (is (= s ["a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba"])))) +(deftest test-perms4-2 + (let [s (take 2 (stream4 ["a" "b" "c"]))] + (is (= s ["a" "b"])))) + +(deftest test-perms4-3 + (let [s (take 3 (stream4 ["a" "b" "c"]))] + (is (= s ["a" "b" "c"])))) + +(deftest test-perms4-4 + (let [s (take 4 (stream4 ["a" "b" "c"]))] + (is (= s ["a" "b" "c" "aa"])))) + +(deftest test-perms4-25 + (let [s (take 25 (stream4 ["a" "b" "c"]))] + (is (= s ["a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba"])))) + +(deftest test-perms5-2 + (let [s (take 2 (stream5 ["a" "b" "c"]))] + (is (= s ["a" "b"])))) + +(deftest test-perms5-3 + (let [s (take 3 (stream5 ["a" "b" "c"]))] + (is (= s ["a" "b" "c"])))) + +(deftest test-perms5-4 + (let [s (take 4 (stream5 ["a" "b" "c"]))] + (is (= s ["a" "b" "c" "aa"])))) + +(deftest test-perms5-25 + (let [s (take 25 (stream5 ["a" "b" "c"]))] + (is (= s ["a" "b" "c" "aa" "ab" "ac" "ba" "bb" "bc" "ca" "cb" "cc" "aaa" "aab" "aac" "aba" "abb" "abc" "aca" "acb" "acc" "baa" "bab" "bac" "bba"])))) + (run-tests)