-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
;; https://4clojure.oxal.org/#/problem/060 | ||
;; Write a function which behaves like reduce, but returns | ||
;; each intermediate value of the reduction. Your function | ||
;; must accept either two or three arguments, and the return | ||
;; sequence must be lazy. | ||
|
||
(ns ex.q060) | ||
|
||
(defn ans | ||
"Problem 60, Sequence Reductions. restriction: reductions" | ||
([f coll] | ||
(lazy-seq | ||
(if-let [s (seq coll)] | ||
(ans f (first s) (rest s)) | ||
(list (f))))) | ||
([f val coll] | ||
(cons val | ||
(lazy-seq | ||
(when-let [s (seq coll)] | ||
(ans f (f val (first s)) (rest s))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
;; https://4clojure.oxal.org/#/problem/060 | ||
|
||
(ns ex.q060-test | ||
(:require [clojure.test :as t] | ||
[ex.q060 :as sut])) | ||
|
||
(t/deftest test-q60 | ||
(t/testing "Problem 60, Sequence Reductions" | ||
(t/is (= [0 1 3 6 10] (take 5 (sut/ans + (range))))) | ||
(t/is (= [[1] [1 2] [1 2 3] [1 2 3 4]] (sut/ans conj [1] [2 3 4]))) | ||
(t/is (= 120 (reduce * 2 [3 4 5]) (last (sut/ans * 2 [3 4 5])))))) |