Last active
August 29, 2015 14:02
-
-
Save pslusarz/98b19d41eee49e8bfb2e to your computer and use it in GitHub Desktop.
Functional: Sum Even Fibonacci < 4,000,000
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
def fibSeed = [1,1] | |
def nextFib = { l -> l[-2]+l[-1] } | |
def nextFibSeed = {l -> (l << nextFib(l)).tail()} | |
def upTo = {max, l -> l[-1] > max} | |
def even = {l -> l[-2]%2 ? l[-2] : 0} | |
def sumGoodUntilDone = {series, next, done, good -> done(series) ? good(series) : good(series) + call( next(series), next, done, good)} | |
def sumFib = sumGoodUntilDone.curry(fibSeed, nextFibSeed) | |
println sumFib(upTo.curry(4000000), even) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment