Last active
August 29, 2015 14:19
-
-
Save saik0/9e3e2ad0fdeca031862b to your computer and use it in GitHub Desktop.
Cumulative map
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 cmap | |
"lazy cumulative map" | |
([f coll] (cmap f (f) coll)) | |
([f val coll] | |
(cons val | |
(if (empty? coll) | |
nil | |
(lazy-seq (cmap f | |
(f val (first coll)) | |
(rest coll)))))) | |
(cmap + 0 '(1 1 1 1 1)) | |
;; => (0 1 2 3 4 5) | |
(cmap + '(1 1 1 1 1)) | |
;; => (0 1 2 3 4 5) | |
(cmap + 1 '(10 100 1000 10000 100000)) | |
;; => (1 11 111 1111 11111 111111) | |
(cmap * '(10 10 2 0 99)) | |
;; => (1 10 100 200 0 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment