Created
October 12, 2015 22:01
-
-
Save gpad/d025facd628adeb4696c to your computer and use it in GitHub Desktop.
Elixir meetup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule QSv1 do | |
| def sort([]), do: [] | |
| def sort([pivot | rest]) do | |
| {smaller, bigger} = Enum.partition(rest, &(&1 < pivot)) | |
| sort(smaller) ++ [pivot] ++ sort(bigger) | |
| end | |
| end | |
| defmodule QSv2 do | |
| def sort([]), do: [] | |
| def sort([pivot | rest]) do | |
| {smaller, bigger} = Enum.partition(rest, &(&1 < pivot)) | |
| sort(smaller) ++ [pivot] ++ sort(bigger) | |
| end | |
| def partition(list, pivot), do: partition list, pivot, [], [] | |
| defp partition([h|tail], pivot, smaller, larger) do | |
| if h < pivot do | |
| partition tail, pivot, [h|smaller], larger | |
| else | |
| partition tail, pivot, smaller, [h | larger] | |
| end | |
| end | |
| defp partition([], _, smaller, larger), do: {smaller, larger} | |
| end | |
| defmodule PQS do | |
| def random_list(how_many) do | |
| (1..how_many) |> Enum.map fn _ -> :rand.uniform(how_many * 100) end | |
| end | |
| def sort(how_many) do | |
| (1..how_many) | |
| |> DBG.tap(fn _ -> IO.puts "prepare data ..." end) | |
| |> Enum.reduce([], fn _, acc -> acc ++ [random_list(how_many)] end) | |
| |> DBG.tap(fn _ -> IO.puts "start sorting ..." end) | |
| |> Enum.map(fn l -> Task.async(fn -> QSv2.sort(l) end) end) | |
| |> DBG.tap(fn _ -> IO.puts "start awaiting ..." end) | |
| |> DBG.tap(&IO.inspect/1) | |
| |> PTask.awaitAll() | |
| end | |
| end | |
| defmodule PTask do | |
| def awaitAll tasks do | |
| Enum.map tasks, fn t -> Task.await(t) end | |
| end | |
| end | |
| defmodule DBG do | |
| def tap(v, f) do | |
| f.(v) | |
| v | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment