Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 0.62 KB | None | 0 0
  1. -module(pmap_any_tmt).
  2. -compile(export_all).
  3.  
  4. fib(1) -> 1;
  5. fib(2) -> 1;
  6. fib(N) -> fib(N-1) + fib(N-2).
  7.  
  8. pmap_any_tagged_max_time(F, L, MaxTime) ->
  9.     S = self(),
  10.     smap(fun(I) ->
  11.                 spawn(fun() -> do_f(S, F, I) end)
  12.         end, L),
  13.     timer:send_after(MaxTime, self(), finish),
  14.     gather(length(L), []).
  15.  
  16. smap(_, []) ->
  17.     [];
  18. smap(F, [H|T]) ->
  19.     [F(H) | smap(F, T)].
  20.  
  21. do_f(Parent, F, I) ->
  22.     Parent ! {I, F(I)}.
  23.  
  24. gather(0, L) ->
  25.     lists:reverse(L);
  26. gather(Left, L) ->
  27.     receive
  28.         {Tag, Ret} ->
  29.             gather(Left-1, [{Tag, Ret}|L]);
  30.         finish ->
  31.             gather(0, L)
  32.     end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement