Created
April 30, 2022 22:42
-
-
Save boraseoksoon/c0146ccf99310c8e2ceb642709d970e1 to your computer and use it in GitHub Desktop.
[algorithm] new way to implement Fibonacci using closure for any iterator and any sequence.
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
let res4 = Array( | |
AnySequence { () -> AnyIterator<Int> in | |
var t1 = (0, 1) | |
return AnyIterator<Int> { | |
let (a, b) = t1 | |
t1 = (b, a + b) | |
return b | |
} | |
} | |
.prefix(10) | |
) | |
res4 | |
// [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment