Skip to content

Instantly share code, notes, and snippets.

@pchiusano
Last active January 12, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchiusano/3e67334a85f6d8aad06605ea4deb78a8 to your computer and use it in GitHub Desktop.
Save pchiusano/3e67334a85f6d8aad06605ea4deb78a8 to your computer and use it in GitHub Desktop.
map reduce on as many nodes as you want in 10 lines of Unison code
-- note, syntax here is Unison, just using .hs extension to get some syntax highlighting
-- The mapping function may spawn subcomputations on different nodes,
-- and the reduction function is done in parallel, building a balanced tree
-- of operations. Computation fails if any step takes longer than timeout
mapreduce : forall a b
. Duration -> (a -> Remote b) -> b -> (b -> b -> b) -> Vector a
-> Remote b
mapreduce timeout f z op vs = do Remote
futures := Remote.traverse (f `and-then` Remote.start timeout) vs
Vector.fold-balanced (Remote.parallel-map2 timeout op)
(Remote.pure z)
futures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment