Skip to content

Instantly share code, notes, and snippets.

@airekans
Created October 16, 2020 07:14
Show Gist options
  • Save airekans/1b24a8117c3ced436aa8f0bbbbd89912 to your computer and use it in GitHub Desktop.
Save airekans/1b24a8117c3ced436aa8f0bbbbd89912 to your computer and use it in GitHub Desktop.
A recursion version of parallel reduce similar to the iterative version
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