Help: Check My Work - Fetching Basic Presence Details With Ecto - Advice Welcome

Flat map is also a way to filter and transform at the same time, which is what the original function was doing.

Enum.reduce(ids, [], fn x, agg -> 
  case Integer.parse(x) do
     {i,""} -> [i|agg]
     _ -> agg
  end
end)

Is equivalent to

Enum.flat_map(ids, fn x -> 
  case Integer.parse(x) do
     {i,""} -> [i]
     _ -> [] 
  end
end)

Empty lists will be removed during the flatten.

EDIT: actually, the reduce version will reverse the list while flat_map will preserve the original order. So if you want to transform and filter flat_map is actually preferable.

2 Likes