Skip to content

Instantly share code, notes, and snippets.

@StoneCypher
Created April 22, 2014 17:25
Show Gist options
  • Save StoneCypher/11187594 to your computer and use it in GitHub Desktop.
Save StoneCypher/11187594 to your computer and use it in GitHub Desktop.
found this cleaning up editor windows, huhu
chunk(List) ->
part(List, []).
chunk([], Acc) ->
lists:reverse(Acc);
chunk([H], Acc) ->
lists:reverse([[H]|Acc]);
chunk([H1,H2|T], Acc) ->
part(T, [[H1,H2]|Acc]).
map_reduce(Functor, Workload) ->
Targets = nodes(),
NodeCount = length(Targets),
WorkLength = length(Workload),
WorkSeq = lists:seq(1, WorkLength),
AnnotedWork = lists:zip(WorkSeq, Workload),
ChunkSize = WorkLength div NodeCount,
AnnotedWorkChunks = chunk(AnnotedWork),
[ spawn(Tgt, fun() -> Functor(AWC) end) || {Tgt,AWC} = lists:zip(Targets, AnnotedWorkChunks) ],
receive_work(WorkLength, []).
receive_work(Length, Work) when length(Work) < Length ->
receive
{ workitem, Id, Item } ->
receive_work(Length, [{Id, Item}] ++ Work)
end;
receive_work(Length, Work) ->
[ WorkItem || {_Id, WorkItem} <- lists:keysort(1, Work) ].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment