Created
March 23, 2016 03:04
-
-
Save rlucha/abb21bde408c111557b7 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
function* fib(a, b) { | |
let c = 0 | |
yield a | |
yield b | |
while (true) { | |
c = a + b | |
a = b | |
b = c | |
yield c | |
} | |
} | |
function take(n, iterable) { | |
let result = [], i = 0 | |
while (i < n) { | |
result.push(iterable.next().value) | |
i++ | |
} | |
return result | |
} | |
function takeWhile(conditionFn, iterable) { | |
let result = [] | |
let value = take(1, iterable) | |
while (conditionFn(value)) { | |
result = result.concat(value) | |
value = take(1, iterable) | |
} | |
return result | |
} | |
function sum(a,b) { return a + b } | |
function isEven(n) { return n % 2 === 0} | |
const solution = takeWhile(n => n < 4000000, fib(1,2)).filter(isEven).reduce(sum) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment