Skip to content

Commit

Permalink
Refactored to match the material.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jani Rahkola committed Oct 21, 2012
1 parent 38d08ac commit 3a590e4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 43 deletions.
9 changes: 6 additions & 3 deletions project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
(defproject recursion "0.1.0-SNAPSHOT"
(defproject recursion "1.0.0-SNAPSHOT"
:description "Recursion"
:repositories {"stuart" "http://stuartsierra.com/maven2"}
:dependencies [[org.clojure/clojure "1.4.0"]]
:profiles {:dev {:dependencies [[midje "1.4.0"]]
:plugins [[lein-midje "2.0.0-SNAPSHOT"]]}})
:profiles {:dev {:dependencies [[midje "1.4.0"]
[com.stuartsierra/lazytest "1.2.3"]]
:plugins [[lein-midje "2.0.0-SNAPSHOT"]]}})
48 changes: 30 additions & 18 deletions src/recursion.clj
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,67 @@
(defn last-element [coll]
:-)

(defn my-filter [f a-seq]
[:-])

(defn sequence-contains? [elem a-seq]
:-)

(defn seq= [a-seq b-seq]
:-)

(defn my-map [f seq-1 seq-2]
[:-])

(defn power [a b]
:-)

(defn fib [n]
:-)

(defn my-range [up-to]
:-)
(defn my-repeat [how-many-times
what-to-repeat]
[:-])

(defn map-1 [f a-seq]
:-)
(defn my-range [up-to]
[:-])

(defn tails [a-seq]
:-)
[:-])

(defn inits [a-seq]
:-)
[:-])

(defn split-into-monotonics [a-seq]
:-)
[:-])

(defn rotations [a-seq]
:-)

(defn frequencies-helper [freqs collection]
:-)
[:-])

(defn my-frequencies [a-seq]
:-)

(defn un-frequencies [a-map]
:-)
[:-])

(defn my-take [n coll]
[:-])

(defn my-drop [n coll]
[:-])

(defn halve [a-seq]
[:-])

(defn seq-merge [a-seq b-seq]
:-)
[:-])

(defn mergesort [a-seq]
:-)
(defn merge-sort [a-seq]
[:-])

(defn permutations [a-seq]
:-)
[:-])

(defn powerset [a-seq]
:-)
[:-])

; %_____%
65 changes: 43 additions & 22 deletions test/recursion_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
(last-element [1 2 3]) => 3
(last-element [2 5]) => 5)

(facts "my-filter"
(my-filter odd? [1 2 3 4]) => '(1 3)
(my-filter (fn [x] (> x 9000)) [12 49 90 9001]) => '(9001)
(my-filter even? [1 3 5 7]) => '())

(facts "sequence-contains?"
(sequence-contains? 3 [1 2 3]) => true
(sequence-contains? 3 [4 7 9]) => false
Expand All @@ -24,6 +29,11 @@
(seq= [1 2 3] [1 2 3 4]) => false
(seq= [1 3 5] []) => false)

(facts "my-map"
(my-map + [1 2 3] [4 4 4]) => '(5 6 7)
(my-map + [1 2 3 4] [0 0 0]) => '(1 2 3)
(my-map + [1 2 3] []) => '())

(facts "power"
(power 2 2) => 4
(power 5 3) => 125
Expand All @@ -40,36 +50,33 @@
(fib 6) => 8
(fib 10) => 55)

(facts "my-repeat"
(my-repeat 2 :a) => '(:a :a)
(my-repeat 3 "lol") => '("lol" "lol" "lol")
(my-repeat -1 :a) => '())

(facts "my-range"
(my-range 0) => nil
(my-range 0) => '()
(my-range 1) => [0]
(my-range 2) => [1 0]
(my-range 3) => [2 1 0])

(facts "map-1"
(map-1 identity []) => empty?
(map-1 identity [1 2 3]) => [1 2 3]
(map-1 count ["aaa" "bb" "cccc"]) => [3 2 4]
(map-1 first [[1 2] [4] [7 12 28]]) => [1 4 7]
(map-1 zero? [0 2 0 13 4 0])
=> [true false true false false true])

(facts "tails"
(tails [1 2 3 4]) => (just [[1 2 3 4] [2 3 4] [3 4] [4] empty?] :in-any-order)
(tails []) => (just [empty?] :in-any-order)
(tails [1]) => (just [[1] empty?] :in-any-order))
(tails [1 2 3 4]) => (just [[1 2 3 4] [2 3 4] [3 4] [4] []] :in-any-order)
(tails []) => [[]]
(tails [1]) => (just [[1] []] :in-any-order))

(facts "inits"
(inits [1 2 3 4]) => (just [empty? [1] [1 2] [1 2 3] [1 2 3 4]] :in-any-order)
(inits []) => (just [empty?] :in-any-order)
(inits [1]) => (just [empty? [1]] :in-any-order))
(inits [1 2 3 4]) => (just [[] [1] [1 2] [1 2 3] [1 2 3 4]] :in-any-order)
(inits []) => [[]]
(inits [1]) => (just [[] [1]] :in-any-order))

(facts "split-into-monotonics"
(split-into-monotonics [0 1 2 1 0]) => '((0 1 2) (1 0))
(split-into-monotonics [0 5 4 7 1 3]) => '((0 5) (4 7) (1 3)))

(facts "rotations"
(rotations []) => empty?
(rotations []) => []
(rotations [1 2 3]) => (just [[1 2 3] [2 3 1] [3 1 2]] :in-any-order)
(rotations [:a :b]) => (just [[:a :b] [:b :a]] :in-any-order)
(rotations [1 5 9 2]) => (just '(1 5 9 2) '(2 1 5 9)
Expand All @@ -91,21 +98,35 @@
(my-frequencies (un-frequencies {:a 100 :b 10}))
=> {:a 100 :b 10})

(facts "my-take"
(my-take 2 [1 2 3 4]) => '(1 2)
(my-take 4 [:a :b]) => '(:a :b))

(facts "my-drop"
(my-drop 2 [1 2 3 4]) => '(3 4)
(my-drop 4 [:a :b]) => '())

(facts "halve"
(halve [1 2 3 4]) => ['(1 2) '(3 4)]
(halve [1 2 3 4 5]) => ['(1 2) '(3 4 5)]
(halve [1]) => ['() '(1)])

(facts "seq-merge"
(seq-merge [4] [1 2 6 7]) => '(1 2 4 6 7)
(seq-merge [1 5 7 9] [2 2 8 10]) => '(1 2 2 5 7 8 9 10))

(facts "mergesort"
(mergesort []) => empty?
(mergesort [1 2 3]) => '(1 2 3)
(mergesort [5 3 4 17 2 100 1]) => '(1 2 3 4 5 17 100))
(facts "merge-sort"
(merge-sort []) => '()
(merge-sort [1 2 3]) => '(1 2 3)
(merge-sort [5 3 4 17 2 100 1]) => '(1 2 3 4 5 17 100))

(facts "permutations"
(permutations []) => empty?
(permutations []) => []
(permutations [1 5 3])
=> (just [[1 5 3] [5 1 3] [5 3 1] [1 3 5] [3 1 5] [3 5 1]]
:in-any-order))

(facts "powerset"
(powerset []) => '(())
(powerset [1 2 4]) => (just [empty? [4] [2] [2 4] [1] [1 4] [1 2] [1 2 4]] :in-any-order))
(powerset [1 2 4]) => (just [[] [4] [2] [2 4] [1] [1 4] [1 2] [1 2 4]]
:in-any-order))

0 comments on commit 3a590e4

Please sign in to comment.