Uses cases? for proposed new lists functions

foldlwhile would be somewhat useful, but what @josevalim said :wink: I donā€™t think we need shortcuts for just about anything.


groupby, for me at least, is in the same line. It can be done with a fold almost as easy but with more flexibility. A bit more verbose, yes, but again, IMO there is no need to shortcut everything.


transpose, I never needed that and canā€™t imagine that I ever will.


uniq however is interesting (but I agree with @michalmuskala that we should use an unabbreviated name).

The ambiguity @juhlig pointed out exists, but that can be documented like ā€œthe first is kept, subsequent duplicates will be removedā€. There are many functions in the lists module doing that already, eg. delete/2. It is what people probably expect to happen, anyway.

Anyway, to make this function really useful, there should be a uniq/2 variant, accepting an unary function that maps an element to some other term. This way, something like deduplicating a list of key-value tuples would be possible, like this:

lists:uniq(
    fun ({Key, _Value}) -> Key end,
    [{a, 1}, {b, 2}, {a, 3}]
)

ā€¦ which would result in [{a, 1}, {b, 2}]: only the first a-tuple is kept, even though the second a-tuple is a different term, ie not identical to the first one.

Equally, it would be possible to not deduplicate certain elements even if they are identical, like this:

lists:uniq(
    fun
        (keep_me) -> make_ref();
        (Other) -> Other
    end,
    [a, keep_me, b, keep_me, a]
)

ā€¦ which would result in [a, keep_me, b, keep_me]: the duplicate a at the end is removed, but the duplicate keep_mes are still there.

4 Likes