Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
Created April 30, 2022 22:42
Show Gist options
  • Save boraseoksoon/c0146ccf99310c8e2ceb642709d970e1 to your computer and use it in GitHub Desktop.
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.
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