Skip to content

Instantly share code, notes, and snippets.

@marshall-lee
Last active December 26, 2015 11:59
Show Gist options
  • Save marshall-lee/7147548 to your computer and use it in GitHub Desktop.
Save marshall-lee/7147548 to your computer and use it in GitHub Desktop.
Sleep Sort in Erlang
-module(sleep_sort).
-export([sort/1, sort_better/1]).
sort(List) ->
Self = self(),
lists:foreach(fun(X) -> spawn(fun() -> timer:sleep(X), Self ! X end) end, List),
[receive X -> X end || _ <- lists:seq(1, length(List))].
sort_better(List) ->
Self = self(),
lists:foreach(fun(X) -> erlang:send_after(X, Self, X) end, List),
[receive X -> X end || _ <- lists:seq(1, length(List))].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment