Created
October 2, 2016 02:05
-
-
Save HerbM/9e9638b4f618874cea6a91e7fe3894f5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(defn rducts | |
([f aseq] (lazy-seq (rducts f (f (first aseq)) (rest aseq)))) | |
([f accum aseq] | |
(lazy-seq (cons accum (let [fs (first aseq) rs (rest aseq)] | |
(if (not (seq rs)) | |
(list (f accum fs)) | |
(rducts f (f accum fs) rs))))))) | |
(take 5 (rducts + (range))) | |
(rducts conj [1] [2 3 4]) | |
(last (rducts * 2 [3 4 5])) | |
(= (take 5 (rducts + (range))) [0 1 3 6 10]) | |
(= (rducts conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]]) | |
(= (last (rducts * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120) | |
This file contains hidden or 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
(defn rducts | |
([f aseq] (lazy-seq (rducts f (f (first aseq)) (rest aseq)))) | |
([f accum aseq] | |
(lazy-seq (cons accum (let [fs (first aseq) rs (rest aseq)] | |
(if (not (seq rs)) | |
(list (f accum fs)) | |
(rducts f (f accum fs) rs))))))) | |
(take 5 (rducts + (range))) | |
(rducts conj [1] [2 3 4]) | |
(last (rducts * 2 [3 4 5])) | |
(= (take 5 (rducts + (range))) [0 1 3 6 10]) | |
(= (rducts conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]]) | |
(= (last (rducts * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment