3 ms·
The Fibonacci generator hurts my eyes! Here is a cleaner version. def fib(): a, b = 0, 1 while True: yield a a,
by riobard 16y ago
The Fibonacci generator hurts my eyes! Here is a cleaner version.
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a+b
Anyway, the order of the pipeline-like operation in Python sometimes really bugs me. This one addresses part of it.
Recently I'm playing with Scala and it is really neat to do these stuff:
// A lazy Fibonacci generator (a Scala Stream)
lazy val fib: Stream[Long] = Stream.cons(0, Stream.cons(1,
fib.zip(fib.tail).map(pair => pair._1 + pair._2)))
fib filter(_ % 2 == 0) takeWhile (_ < 4000000) sum
// 4613732