elm-nonempty-list icon indicating copy to clipboard operation
elm-nonempty-list copied to clipboard

List.Nonempty.filter is misleading

Open MartinSStewart opened this issue 3 years ago • 1 comments

I expected List.Nonempty.filter to have this type signature (a -> Bool) -> Nonempty a -> List a or (a -> Bool) -> Nonempty a -> Maybe (Nonempty a) but instead it has (a -> Bool) -> a -> Nonempty a -> Nonempty a.

I don't think this is a good idea because it's difficult to tell if all the items got filtered. If someone does want that behavior, it's easy for them to write List.Nonempty.filter condition nonempty |> Maybe.withDefault default instead (assuming the second type signature was used for filter).

MartinSStewart avatar Mar 04 '21 14:03 MartinSStewart

I would argue this makes perfect sense, I made the same decision when I was creating the API for my own little non empty list module before I realized I could probably find something third party like this.

The reason I think it makes sense is that in the List.Nonempty "world", we are making operations on lists that can never be empty. One could maybe argue for not providing a filter at all in that case, but I think it actually can be quite helpful. If it returned either a list or a maybe that leaves you with having to pattern match on whether it's empty or not again, so in both cases it's just back in the list world. Maybe (Nonempty a) I would argue is equivalent, but in a more awkward way, to List a, as it's just re-adding the empty case.

I would rather argue that if you want the "normal" filter behaviour it's very easy for someone to write

List.filter condition (List.Nonempty.toList nonempty).

That's my 2 cents anyway, but I have nothing to do with the package, just stumbled on this issue :)

emilgoldsmith avatar Apr 04 '21 13:04 emilgoldsmith