Last active
December 20, 2015 21:08
-
-
Save aamedina/6194953 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 foldl | |
[f init xs] | |
(println xs) | |
(cond (empty? xs) (f) | |
(nil? init) (recur (partial f (first xs)) nil (rest xs)) | |
:else (recur (partial f init (first xs)) nil (rest xs)))) | |
(foldl + 0 (range 10)) | |
(0 1 2 3 4 5 6 7 8 9) | |
(1 2 3 4 5 6 7 8 9) | |
(2 3 4 5 6 7 8 9) | |
(3 4 5 6 7 8 9) | |
(4 5 6 7 8 9) | |
(5 6 7 8 9) | |
(6 7 8 9) | |
(7 8 9) | |
(8 9) | |
(9) | |
() | |
45 | |
(defn foldr | |
[f init xs] | |
(println xs) | |
(cond (empty? xs) (f init) | |
:else (recur (partial f (last xs)) init (butlast xs)))) | |
(foldr + 0 (range 10)) | |
(0 1 2 3 4 5 6 7 8 9) | |
(0 1 2 3 4 5 6 7 8) | |
(0 1 2 3 4 5 6 7) | |
(0 1 2 3 4 5 6) | |
(0 1 2 3 4 5) | |
(0 1 2 3 4) | |
(0 1 2 3) | |
(0 1 2) | |
(0 1) | |
(0) | |
nil | |
45 | |
(defn unfoldl | |
[f seed] | |
(cons seed (lazy-seq (unfoldl f (f seed))))) | |
(take 10 (unfoldl inc 0)) | |
0 | |
(1 | |
0 2 | |
1 3 | |
2 4 | |
3 5 | |
4 6 | |
5 7 | |
6 8 | |
7 9 | |
8 9) | |
(0 1 2 3 4 5 6 7 8 9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment