Which is more idiomatic Elixir construct

You mean something like:

Enum.flat_map([1,2,3], &List.wrap(if rem(&1,2) == 1, do: &1*10)) 

I’d prefer this:

Enum.flat_map([1,2,3], fn x ->
    case rem(x,2) do
        1 -> [x*10]
        _ -> []
    end
end)

Longer, but easier on my eyes.

3 Likes

I’d probably put it in an fn instead of a short lambda.

Enum.flat_map([1,2,3], fn x ->
  List.wrap(if rem(x,2) == 1, do: x*10)
end)

That’s how I name my conditional function head matches too :slight_smile:

1 Like