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

Infix operator (:||) for NonEmptyList cons' #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

milesfrain
Copy link
Contributor

@milesfrain milesfrain commented Jan 13, 2021

Description of the change

Motivation for this change is to give users a more ergonomic alternative to:

-- | groupBy (\a b -> odd a && odd b) (1 : 3 : 2 : 4 : 3 : 3 : Nil) ==
-- | (NonEmptyList (NonEmpty 1 (3 : Nil))) : (NonEmptyList (NonEmpty 2 Nil)) : (NonEmptyList (NonEmpty 4 Nil)) : (NonEmptyList (NonEmpty 3 (3 : Nil))) : Nil

We can currently do a little better with NonEmpty's :| operator:

(NonEmptyList (1 :| 3 : Nil)) : (NonEmptyList (2 :| Nil)) : (NonEmptyList (4 :| Nil)) : (NonEmptyList (3 :| (3 : Nil))) : Nil

But it's even nicer with this change, which introduces the :|| operator for cons'.

(1 :|| 3 : Nil) : (2 :|| Nil) : (4 :|| Nil) : (3 :|| 3 : Nil) : Nil

I'd ideally like to reuse the :| operator, but that leads to complications since we then can't use that operator for pattern matching anymore (it's equivalent to NonEmptyList (NonEmpty x xs). The new version is no longer a simple data constructor of 2 arguments. We'd likely need to enable Pattern Synonyms for that to work.


Checklist:

  • Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • Linked any existing issues or proposals that this pull request should close
  • Updated or added relevant documentation
  • Added a test for the contribution (if applicable)


snoc :: forall a. NonEmptyList a -> a -> NonEmptyList a
snoc (NonEmptyList (x :| xs)) y = NonEmptyList (x :| L.snoc xs y)
snoc (NonEmptyList (x :| xs)) y = x :|| L.snoc xs y
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we could get pattern matching to work, and reuse :|, we could rewrite this (and friends) as:

snoc (x :| xs) y = x :| L.snoc xs y

@milesfrain milesfrain force-pushed the non-empty-cons-infix branch from e64a80f to f0a589c Compare January 13, 2021 00:42
@milesfrain milesfrain changed the title Infix operator (:||) for non-empty cons' Infix operator (:||) for NonEmptyList cons' Jan 13, 2021
@hdgarrood
Copy link
Contributor

Adding this would be non-breaking so I'd suggest leaving this until after v0.14.0.

@JordanMartinez
Copy link
Contributor

@hdgarrood Why wait to merge this if it builds and is otherwise acceptable?

@hdgarrood
Copy link
Contributor

Because we should be focusing on getting v0.14.0 out - it's been in the pipeline for far too long already.

@Masynchin
Copy link
Contributor

What is the current status of this proposal? It is planned to be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants