Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add List.Extra.fromNonEmpty #61

Closed
gampleman opened this issue Jul 25, 2024 · 0 comments · Fixed by #63
Closed

Add List.Extra.fromNonEmpty #61

gampleman opened this issue Jul 25, 2024 · 0 comments · Fixed by #63
Labels
new function Request to add a new function to the library

Comments

@gampleman
Copy link
Collaborator

gampleman commented Jul 25, 2024

List.Extra.fromNonEmpty : ( a, List a ) -> List a

It's a convention to represent a collection of items where we know we have at least one item as a tuple of the first item and the rest. However, this representation is awkward if we need to do additional list processing, as we need to deconstruct the tuple and then :: it, which tends to be awkward in pipelines. Hence this helper can help pipelined code:

myData 
    |> List.Extra.gatherEqualsBy .id
    |> List.map List.Extra.fromNonEmpty
    |> List.map (List.sortBy .value)

Motivating use case

This is explained in the doc above.

Rationale

myData 
    |> List.Extra.gatherEqualsBy .id
    |> List.map (\(first, rest) -> first :: rest)
    |> List.map (List.sortBy .value)

which is not terrible, but certainly not pleasant to type (and autocomplete doesn't help much, but probably copilot can manage).

It's worse when in the middle of a pipeline where you end up with a non-empty list on its own, where you need a bare lambda in a pipeline - which is always a bit awkward looking.

The other option you have is of course Tuple.Extra.apply (::), but that is perhaps too clever to think of on the spot?

Alternatives

  1. Name it List.Extra.cons as the logical opposite of List.Extra.uncons. It's shorter and perhaps less confusing w.r.t. dedicated non-empty list custom types.
  2. Not bother. In general adding relatively trivial functions like this for mere syntactic convenience is something I'd like to not encourage too much of, since I think we've already got the really useful ones covered and the rest tend to be more confusing than helpful. The only reason I'm proposing this one is that we already have a few functions that return non-empty lists (and are proposing more), and I've always disliked working with those as it made further processing more painful.
@gampleman gampleman added the new function Request to add a new function to the library label Jul 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new function Request to add a new function to the library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant