List comprehension vs. Enum module

I have a question about List comprehensions vs the Enum module. While the Enum module can basically do what the comprehension can do… such as

def my_function(arr) do
  for x <- arr, do: IO.inspect(x)

vs something like

def my_function(arr) do, fn x -> IO.inspect(x) end)

is there any performance gains using one rather than the other or any places where one approach is favorable?

Hello and welcome,

For simple example, it might look the same… but not really, try to do this with Enum :slight_smile:

iex> for x <- 0..3, y <- 2..4, x > 1 and y < 3, do: {x, y}, into: %{}
%{2 => 2, 3 => 2}

comprehension can accept many generators and filters


well then… that made the distinction pretty clear. hah thanks!

1 Like