-
-
Save amalloy/b549634b4d855cde88c2 to your computer and use it in GitHub Desktop.
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
(defn unfold | |
"Next and done? are functions that operate on a seed. next should return a pair, [value new-seed]; the value half of the pair is inserted into the resulting list, while the new-seed is used to continue unfolding. Notably, the value is never passed as an argument to either next or done?." | |
[next done? seed] | |
(map first | |
(rest | |
(take-while (comp (complement done?) second) | |
(iterate (comp next second) | |
[nil seed]))))) | |
(defn fibs [] | |
(unfold (fn [[a b]] [a [b (+ a b)]]) (constantly false) [0 1])) | |
user> (take 10 (fibs)) | |
(0 1 1 2 3 5 8 13 21 34) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment