Last active
August 29, 2015 14:06
-
-
Save bstro/1f3f8ea55106dfcd3772 to your computer and use it in GitHub Desktop.
euler2 with generators and shitty transducers
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
/** @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