Skip to content

Instantly share code, notes, and snippets.

@bstro
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bstro/1f3f8ea55106dfcd3772 to your computer and use it in GitHub Desktop.
Save bstro/1f3f8ea55106dfcd3772 to your computer and use it in GitHub Desktop.
euler2 with generators and shitty transducers
/** @jsx React.DOM */
// EULER 2
var isEven = val => val % 2 === 0;
var concat = (acc, cur) => acc.concat(cur)
var sum = (a, b) => a + b;
function* fibonacci() {
var [prev, curr] = [1, 0]
while (prev+curr < 4000000) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
function reduceSeqInto(seq, reducer, acc) {
for (var x of seq) acc = reducer(acc, x);
return acc
}
function filtering(pred) {
return function(step) {
return function(acc, cur) {
return (pred(cur) ? acc : step(acc, cur))
}
}
}
var fibs = reduceSeqInto(fibonacci(), concat, Im.Vector())
var evenFibs = fibs.reduce(filtering(isEven)(sum), 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment