The solution with filters and map look about right.
Re: IO.iodata_to_binary thing - i can’t just put things together this way, but overall it looks good, just need to add |> Enum.filter(fn(x) -> !is_nil(x) end), i.e.
Also because nobody has mentioned this yet, the “ugly elixir version” is not actually an elixir version. It would not work at all because out is immutable.
We do not have any guarantee of ordering in a map. So in theory the begs pour of this snippet is non deterministic, while in the OPs version we do have an explicit ordering of the keys and their transformation/concatenation into the result.
I know people love |> but you don’t have to use it:
:maps.fold(fn (k,v,acc) when v > 0 -> acc ++ [output_texts[k]]
(_,_,acc) -> acc
end, [], data)
There is probably a function equivalent to fold in Enum but it is called something else and I could not immediately see it, hence using the one in :maps.
Yeah, in my use-case ordering guarantees are required so maps don’t work (but for someone else it could be better approach). also there are other non-related keys as well (which should be filtered out)
plain [..., ..., ...] |> remove nils works just fine - ordering granaries, short and easy to read code
I’m aware that we have no left or right in a map but we do have more data structures which in fact do have. And for some operations it is important to know if we are folding from the left or the right.
Yes, for many structures you definitely need to know whether you are folding left or right. That is one reason why calling them foldl and foldr is better than just reduce. The :maps module just has fold as there is no defined ordering.