-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Sync NEL and NEV methods (fixes #1832) #1838
Changes from all commits
f43228c
1bf60ee
d21fa9d
9a5d4ff
7a41804
f18b06f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,19 +66,37 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) { | |
*/ | ||
def size: Int = 1 + tail.size | ||
|
||
def length: Int = size | ||
|
||
/** | ||
* Applies f to all the elements of the structure | ||
*/ | ||
def map[B](f: A => B): NonEmptyList[B] = | ||
NonEmptyList(f(head), tail.map(f)) | ||
|
||
def ++[AA >: A](l: List[AA]): NonEmptyList[AA] = | ||
NonEmptyList(head, tail ++ l) | ||
concat(l) | ||
|
||
def concat[AA >: A](other: List[AA]): NonEmptyList[AA] = | ||
NonEmptyList(head, tail ::: other) | ||
|
||
@deprecated("Use concatNel", since = "1.0.0-RC1") | ||
def concat[AA >: A](other: NonEmptyList[AA]): NonEmptyList[AA] = | ||
concatNel(other) | ||
|
||
/** | ||
* Append another NonEmptyList | ||
*/ | ||
def concatNel[AA >: A](other: NonEmptyList[AA]): NonEmptyList[AA] = | ||
NonEmptyList(head, tail ::: other.toList) | ||
|
||
def flatMap[B](f: A => NonEmptyList[B]): NonEmptyList[B] = | ||
f(head) ++ tail.flatMap(f andThen (_.toList)) | ||
|
||
def ::[AA >: A](a: AA): NonEmptyList[AA] = | ||
prepend(a) | ||
|
||
def prepend[AA >: A](a: AA): NonEmptyList[AA] = | ||
NonEmptyList(a, head :: tail) | ||
|
||
/** | ||
|
@@ -137,12 +155,6 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) { | |
} | ||
} | ||
|
||
/** | ||
* Append another NonEmptyList | ||
*/ | ||
def concat[AA >: A](other: NonEmptyList[AA]): NonEmptyList[AA] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this naming better. We should add |
||
NonEmptyList(head, tail ::: other.toList) | ||
|
||
/** | ||
* Find the first element matching the predicate, if one exists | ||
*/ | ||
|
@@ -405,7 +417,7 @@ private[data] sealed abstract class NonEmptyListInstances extends NonEmptyListIn | |
with Monad[NonEmptyList] with NonEmptyTraverse[NonEmptyList] { | ||
|
||
def combineK[A](a: NonEmptyList[A], b: NonEmptyList[A]): NonEmptyList[A] = | ||
a concat b | ||
a concatNel b | ||
|
||
override def split[A](fa: NonEmptyList[A]): (A, List[A]) = (fa.head, fa.tail) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,10 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal | |
|
||
def tail: Vector[A] = toVector.tail | ||
|
||
def last: A = toVector.last | ||
|
||
def init: Vector[A] = toVector.init | ||
|
||
/** | ||
* Remove elements not matching the predicate | ||
* | ||
|
@@ -60,6 +64,8 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal | |
*/ | ||
def filterNot(f: A => Boolean): Vector[A] = toVector.filterNot(f) | ||
|
||
def collect[B](pf: PartialFunction[A, B]): Vector[B] = toVector.collect(pf) | ||
|
||
/** | ||
* Alias for [[concat]] | ||
*/ | ||
|
@@ -198,6 +204,18 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A]) extends AnyVal | |
*/ | ||
def zipWith[B, C](b: NonEmptyVector[B])(f: (A, B) => C): NonEmptyVector[C] = | ||
NonEmptyVector.fromVectorUnsafe((toVector, b.toVector).zipped.map(f)) | ||
|
||
def reverse: NonEmptyVector[A] = | ||
new NonEmptyVector(toVector.reverse) | ||
|
||
def zipWithIndex: NonEmptyVector[(A, Int)] = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could use this to override There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
new NonEmptyVector(toVector.zipWithIndex) | ||
|
||
def sortBy[B](f: A => B)(implicit B: Order[B]): NonEmptyVector[A] = | ||
new NonEmptyVector(toVector.sortBy(f)(B.toOrdering)) | ||
|
||
def sorted[AA >: A](implicit AA: Order[AA]): NonEmptyVector[AA] = | ||
new NonEmptyVector(toVector.sorted(AA.toOrdering)) | ||
} | ||
|
||
private[data] sealed abstract class NonEmptyVectorInstances { | ||
|
@@ -251,6 +269,9 @@ private[data] sealed abstract class NonEmptyVectorInstances { | |
override def traverse[G[_], A, B](fa: NonEmptyVector[A])(f: (A) => G[B])(implicit G: Applicative[G]): G[NonEmptyVector[B]] = | ||
G.map2Eval(f(fa.head), Always(Traverse[Vector].traverse(fa.tail)(f)))(NonEmptyVector(_, _)).value | ||
|
||
override def zipWithIndex[A](fa: NonEmptyVector[A]): NonEmptyVector[(A, Int)] = | ||
fa.zipWithIndex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is untested, so I guess that means zipWithIndex is currently lawless. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could write a test to check if |
||
|
||
override def foldLeft[A, B](fa: NonEmptyVector[A], b: B)(f: (B, A) => B): B = | ||
fa.foldLeft(b)(f) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why can't this just be
concat
?NonEmptyList
andList
are different types. I'm not sure why we took this route and if there is not a good reason I'd rather change it before 1.0.