Skip to content

Instantly share code, notes, and snippets.

@sentenza
Created May 18, 2014 07:47
Show Gist options
  • Save sentenza/2d2abe1f5fe5ba6b5841 to your computer and use it in GitHub Desktop.
Save sentenza/2d2abe1f5fe5ba6b5841 to your computer and use it in GitHub Desktop.
#!/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))
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