Created
May 18, 2014 07:47
-
-
Save sentenza/2d2abe1f5fe5ba6b5841 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
#!/usr/bin/python3 | |
def mapReduceTR(f, combine, zero, a, b): | |
def loop(a, acc): | |
if (a > b): | |
return acc | |
else: | |
return loop(a + 1, combine(f(a), acc)) | |
return loop(a, zero) | |
print(mapReduceTR(lambda x: x*x, lambda x,y: x+y, 0, 1, 10)) |
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
def mapReduceTR(f: Int => Int, combine: (Int, Int) => Int, zero: Int)(a: Int, b: Int): Int = { | |
@tailrec | |
def loop(a: Int, acc: Int): Int = { | |
if (a > b) | |
acc | |
else | |
loop(a + 1, combine(f(a), acc)) | |
} | |
loop(a, zero) | |
} | |
mapReduce(x => x * x, (x, y) => x + y, 0)(a, b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment