Forked from rlucha/exercise 02 project euler solution
Created
November 27, 2017 17:14
-
-
Save jhsu/c180f7e55d9138c0bc55a2cc357bd554 to your computer and use it in GitHub Desktop.
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
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