Skip to content

Instantly share code, notes, and snippets.

@deep-spaced
Created April 21, 2016 04:19
Show Gist options
  • Save deep-spaced/c07cb17ca276cdc40153c3300133b1e8 to your computer and use it in GitHub Desktop.
Save deep-spaced/c07cb17ca276cdc40153c3300133b1e8 to your computer and use it in GitHub Desktop.
Slow brute force approach, but with processes.
# Snake Case multi-process!
defmodule SnakeCase do
def multi do
Agent.start_link(fn -> 0 end, name: __MODULE__)
ranges = Enum.chunk(0..round(:math.pow(2, 20)), 262144)
tasks = Enum.map ranges, fn range ->
Task.async(fn -> subrange(range) end)
end
for task <- tasks, do: Task.await(task)
Agent.get(__MODULE__, fn x -> IO.puts "Multi process result is #{x}" end)
end
defp subrange(range) do
count = Enum.count range, fn x -> Integer.to_string(x, 2) |> String.replace("0", "") |> String.length == 10 end
Agent.update(__MODULE__, fn(n) -> n + count end)
end
end
IO.inspect :timer.tc fn -> SnakeCase.multi end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment