3 ms·
There's a different syntax available for sequences utilizing trailing closure syntax, in my opinion it's much more succinct, 13 lines vs. 22 let Fibbonacci =
by fleitz 12y ago
There's a different syntax available for sequences utilizing trailing closure syntax, in my opinion it's much more succinct, 13 lines vs. 22
let Fibbonacci = SequenceOf {
var current = -1, last = 0
return GeneratorOf {
if current == -1 {
current = 0
} else if current == 0 {
current = 1
} else {
var next = last + current
last = current
current = next
}
return current
}
}
- ghusbands 12y agoAnd they'd both be five lines shorter if the fibonacci implementation weren't overcomplicated, making your gain even more significant (8 lines vs 17). let Fibbonacci = SequenceOf { var current = 0, next = 1 return GeneratorOf { var ret = current current = next next = next + ret return ret } }
- remon 12y agoIt's interesting how that sort of thing actually hurts the article. When I skimmed through that code I got stuck on "Why is this Fibonacci implementation so verbose?" rather than the actual topic of the paragraph in question.
- rdc12 12y agoAnd branch-less to boot
- fleitz 12y agoGood point, I was thinking that myself but wanted to keep the core algorithm to show specifically the boiler plate involved in solving in an OO style.