Skip to content

Instantly share code, notes, and snippets.

@stepankuzmin
Created October 10, 2012 15:48
Show Gist options
  • Save stepankuzmin/3866474 to your computer and use it in GitHub Desktop.
Save stepankuzmin/3866474 to your computer and use it in GitHub Desktop.
Sleep sort in Erlang
-module(sleepsort).
-export([sort/1]).
sort(List) ->
sort(List, [], []).
sort([], NewList, []) ->
lists:reverse(NewList);
sort(List, [], []) ->
Pids = lists:map(fun(Element) -> spawn(sleepsort, sleep, [self(), Element]) end, List),
sort([], [], Pids);
sort([], NewList, Pids) ->
receive
{Pid, Element} -> sort([], [Element|NewList], Pids--[Pid])
end.
sleep(Pid, Element) ->
timer:sleep(Element),
Pid ! {self(), Element}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment