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

Immutable, singly-linked list implementation. #5

Merged
merged 5 commits into from
Oct 2, 2015

Conversation

non
Copy link
Contributor

@non non commented Sep 24, 2015

This code is very similar to scala.List, with a few key differences:

  1. It does not expose any "unsafe" methods.
  2. It is invariant, whereas scala.List is covariant.
  3. It uses subtyping to differentiate non-emptiness.
  4. It provides Cats type class instances.
  5. It does not currently provide a fast (mutable) builder.

The types defined here are as follows:

  • Lst[A] represents a list of zero-or-more elements.
  • Nel[A] represents a list of one-or-more elements.
  • El[A] represents an empty list (exactly zero elements).

(Every Lst[A] is either a Nel[A] or an El[A].)

While it does not (yet) provide every single Scala collection
method, it provides a decent number of them. Type class instances
are provided for both Lst and Nel (for example, Nel has a Comonad
instance, whereas Lst only has a CoflatMap instance).

This code is very similar to scala.List, with a few key differences:

1. It does not expose any "unsafe" methods.
2. It is invariant, whereas scala.List is covariant.
3. It uses subtyping to differentiate non-emptiness.
4. It provides Cats type class instances.
5. It does not currently provide a fast (mutable) builder.

The types defined here are as follows:

 - Lst[A] represents a list of zero-or-more elements.
 - Nel[A] represents a list of one-or-more elements.
 - El[A]  represents an empty list (exactly zero elements).

(Every Lst[A] is either a Nel[A] or an El[A].)

While it does not (yet) provide every single Scala collection
method, it provides a decent number of them. Type class instances
are provided for both Lst and Nel (for example, Nel has a Comonad
instance, whereas Lst only has a CoflatMap instance).
@non
Copy link
Contributor Author

non commented Sep 24, 2015

For what it's worth, I think the names Lst, Nel, and El are probably not great. I couldn't think of anything compelling enough to use instead. I think before this is merged we should figure out what to call these things.

I think it's relatively important that at least Lst and Nel both look like reasonable type names that users won't mind using in their code.

EDIT: Also needs tests and scaladoc comments of course!

@codecov-io
Copy link

Current coverage is 55.25%

Merging #5 into master will decrease coverage by -0.88% as of 42880a7

@@            master      #5   diff @@
======================================
  Files            9      10     +1
  Stmts          367     447    +80
  Branches        21      29     +8
  Methods          0       0       
======================================
+ Hit            206     247    +41
  Partial          0       0       
- Missed         161     200    +39

Review entire Coverage Diff as of 42880a7

Powered by Codecov. Updated on successful CI builds.

@stew
Copy link
Contributor

stew commented Sep 25, 2015

I'm going to write up some tests for this, but I like the idea, It is one I have played around with in the past.

These names aren't terrible. "a list is either a nel or an el" has a nice poetic meter :). I'd also be happy with Cons and End instead of Nel and El

I am able to convince myself that I would be okay with calling the trait List. Which is certainly a bad idea because its going to piss a bunch of people off right out of the gate, so perhaps, nevermind.

case El() => b
}

final def getNonEmpty: Option[Nel[A]] =

Choose a reason for hiding this comment

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

toNel would seem more natural to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea, I'll change it.

@stew
Copy link
Contributor

stew commented Oct 2, 2015

I'm super busy this week, and there is no reason for this thing to sit out on a PR, I'm going to just go ahead and merge for now,

When I'm above water again, I'll try to convert the things in Dequeue to Lst to see how natural that feels and add the rest of the law checking.

stew added a commit that referenced this pull request Oct 2, 2015
Immutable, singly-linked list implementation.
@stew stew merged commit 26bd7d1 into typelevel:master Oct 2, 2015
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