I also brought up the example of min_max. Is that something that is needed so often that calling {Enum.min(list), Enum.max(list)} became cumbersome? I’d say I need to remove nils from a list more often than I need min_max, and to implement min_max with the existing min and max functions is absurdly easy. Also I wasn’t suggesting there’s no need for uniq, merely that if the issue is bloat, uniq_by(list, & &1) is identical to uniq.
I’m not suggesting that anything needs to be removed from the language, but if the arguments against adding anything is bloat, it’s hard to say there’s not things in Enum already that are arguably bloat, yet they got approved.
The reason for needing compact, is because I would say there’s probably no other single value more frequently returned from an Elixir function than nil(true, and false might be the only other contenders). nil also happens to be a value that is quite often not needed when it’s in a list of other, non-nil values, hence the need to compact. Also I would agree that a compact function is likely bloat because it’s no different than reject(&is_nil/1). My proposal is only for a compact_map function that maps keeping only non-nils.
My argument for codifying it is because it more closely aligns with the way I logically think about that operation, and maybe others do too. If I’m mapping over a list of numbers, adding some value, then keeping anything greater than 5, that’s two clear logical steps in my head, a map, then a filter. But to me “for each item in the list, fetch the result of calling some function that’s not nil” is more how I think of it, than “fetch the result of the function for each item, then filter out the nils”. Surely a lot of the functions in Enum that can be implemented with other functions are there because they more closely align with how we think of something. random is probably how people think of fetching a random element, even if list |> shuffle() |> List.first() is functionally the same. Most people don’t think of a random item as being the first item of a shuffled list, they just think get a random element.






















