Skip to content

Commit

Permalink
with iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew cooke committed Jul 20, 2012
1 parent 72ede8b commit 76d2405
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/org/acooke/lab/perms.clj
Expand Up @@ -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)))
32 changes: 32 additions & 0 deletions test/org/acooke/lab/perms-test.clj
Expand Up @@ -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)

0 comments on commit 76d2405

Please sign in to comment.