Skip to content

Instantly share code, notes, and snippets.

@rlucha
Created March 23, 2016 03:04
Show Gist options
  • Save rlucha/abb21bde408c111557b7 to your computer and use it in GitHub Desktop.
Save rlucha/abb21bde408c111557b7 to your computer and use it in GitHub Desktop.
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