Created
October 16, 2020 07:14
-
-
Save airekans/1b24a8117c3ced436aa8f0bbbbd89912 to your computer and use it in GitHub Desktop.
A recursion version of parallel reduce similar to the iterative version
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 reduce(arr): | |
def impl(i, s): | |
if i >= len(arr): | |
return 0 | |
elif i + s >= len(arr): | |
return arr[i] | |
a = impl(i, s * 2) # spawn | |
b = impl(i + s, s * 2) | |
return a + b | |
return impl(0, 1) | |
print(reduce([1, 2, 3, 4, 5, 7, 8, 9, 10])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment