Elixir playground

It’s a known fact that I’m very much into DevOps and system administration. I enjoy building systems but I also enjoy coding. I’m getting very excited about things like performance and security. And here’s what got me going with Elixir:

defmodule Parallel do
      def pmap(collection, func) do
        collection
        |> Enum.map(&(Task.async(fn -> func.(&1) end)))
        |> Enum.map(&Task.await/1)
      end
 end

What’s happening in this small piece of code is that a function is called via async tasks. Those tasks solve a function for a given collection. Below you can see how we can call this function:

iex(3)> result = Parallel.pmap 1..1000, &(&1 * &1)
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324,
361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089,
1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116,
2209, 2304, 2401, 2500, ...]
iex(4)>

It’s really easy to do parallelism with it. It’s built that way and Elixir’s development goes back to how Erlang and OTP came to be. There was the problem of uptime that the Telecom companies had. Eventually they solved it giving birth to those technologies and nowadays we have a realm of good stuff coming from this solution.

I’m sure that I’ll be digging into Elixir some more soon enough.

Categories:

Updated: