Skip to content

Commit

Permalink
Problem 58:Function Composition resolves #1
Browse files Browse the repository at this point in the history
  • Loading branch information
riksa committed Mar 17, 2015
1 parent 1abb4fa commit 23c91c6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/fourclojure/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,7 @@ Write a function which returns the nth row of Pascal's Triangle. "
"Write a function which removes the duplicates from a sequence. Order of the items must be maintained."
([s] (reduce #(if (some #{%2} %1) %1 (conj %1 %2)) [] s)))

(defn p58
"Write a function which allows you to create function compositions. The parameter list should take a variable number
of functions, and create a function applies them from right-to-left."
([& f] (let [[a & b] (reverse f)] (fn [& x] (reduce #(%2 %1) (apply a x) b)))))
7 changes: 7 additions & 0 deletions test/fourclojure/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -505,3 +505,10 @@
(is (= (p56 [:a :a :b :b :c :c]) [:a :b :c]))
(is (= (p56 '([2 4] [1 2] [1 3] [1 3])) '([2 4] [1 2] [1 3])))
(is (= (p56 (range 50)) (range 50)))))

(deftest p158test
(testing "p58"
(is (= [3 2 1] ((p58 rest reverse) [1 2 3 4])))
(is (= 5 ((p58 (partial + 3) second) [1 2 3 4])))
(is (= true ((p58 zero? #(mod % 8) +) 3 5 7 9)))
(is (= "HELLO" ((p58 #(.toUpperCase %) #(apply str %) take) 5 "hello world")))))

0 comments on commit 23c91c6

Please sign in to comment.