Skip to content

Commit 3da61e9

Browse files
edmundnobleEdmund Noble
authored and
Edmund Noble
committed
Make NonEmptyList covariant
1 parent 468e753 commit 3da61e9

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

core/src/main/scala/cats/data/NonEmptyList.scala

+14-13
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import scala.collection.mutable.ListBuffer
1212
* A data type which represents a non empty list of A, with
1313
* single element (head) and optional structure (tail).
1414
*/
15-
final case class NonEmptyList[A](head: A, tail: List[A]) {
15+
final case class NonEmptyList[+A](head: A, tail: List[A]) {
1616

1717
/**
1818
* Return the head and tail into a single list
@@ -25,13 +25,14 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
2525
def map[B](f: A => B): NonEmptyList[B] =
2626
NonEmptyList(f(head), tail.map(f))
2727

28-
def ++(l: List[A]): NonEmptyList[A] =
28+
def ++[AA >: A](l: List[AA]): NonEmptyList[AA] =
2929
NonEmptyList(head, tail ++ l)
3030

3131
def flatMap[B](f: A => NonEmptyList[B]): NonEmptyList[B] =
3232
f(head) ++ tail.flatMap(f andThen (_.toList))
3333

34-
def ::(a: A): NonEmptyList[A] = NonEmptyList(a, head :: tail)
34+
def ::[AA >: A](a: AA): NonEmptyList[AA] =
35+
NonEmptyList(a, head :: tail)
3536

3637
/**
3738
* Remove elements not matching the predicate
@@ -68,7 +69,7 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
6869
/**
6970
* Append another NonEmptyList
7071
*/
71-
def concat(other: NonEmptyList[A]): NonEmptyList[A] =
72+
def concat[AA >: A](other: NonEmptyList[AA]): NonEmptyList[AA] =
7273
NonEmptyList(head, tail ::: other.toList)
7374

7475
/**
@@ -105,8 +106,8 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
105106
/**
106107
* Left-associative reduce using f.
107108
*/
108-
def reduceLeft(f: (A, A) => A): A =
109-
tail.foldLeft(head)(f)
109+
def reduceLeft[AA >: A](f: (AA, AA) => AA): AA =
110+
tail.foldLeft[AA](head)(f)
110111

111112
def traverse[G[_], B](f: A => G[B])(implicit G: Applicative[G]): G[NonEmptyList[B]] =
112113
G.map2Eval(f(head), Always(Traverse[List].traverse(tail)(f)))(NonEmptyList(_, _)).value
@@ -123,23 +124,23 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
123124
NonEmptyList(f(this), consume(tail))
124125
}
125126

126-
def ===(o: NonEmptyList[A])(implicit A: Eq[A]): Boolean =
127-
(this.head === o.head) && this.tail === o.tail
127+
def ===[AA >: A](o: NonEmptyList[AA])(implicit AA: Eq[AA]): Boolean =
128+
((this.head: AA) === o.head) && (this.tail: List[AA]) === o.tail
128129

129-
def show(implicit A: Show[A]): String =
130-
toList.iterator.map(A.show).mkString("NonEmptyList(", ", ", ")")
130+
def show[AA >: A](implicit AA: Show[AA]): String =
131+
toList.iterator.map(AA.show).mkString("NonEmptyList(", ", ", ")")
131132

132133
override def toString: String = s"NonEmpty$toList"
133134

134135
/**
135136
* Remove duplicates. Duplicates are checked using `Order[_]` instance.
136137
*/
137-
def distinct(implicit O: Order[A]): NonEmptyList[A] = {
138+
def distinct[AA >: A](implicit O: Order[AA]): NonEmptyList[A] = {
138139
implicit val ord = O.toOrdering
139140

140141
val buf = ListBuffer.empty[A]
141-
tail.foldLeft(TreeSet(head)) { (elementsSoFar, a) =>
142-
if (elementsSoFar(a)) elementsSoFar else { buf += a; elementsSoFar + a }
142+
tail.foldLeft(TreeSet[AA](head)) { (elementsSoFar, b) =>
143+
if (elementsSoFar(b)) elementsSoFar else { buf += b; elementsSoFar + b }
143144
}
144145

145146
NonEmptyList(head, buf.toList)

core/src/main/scala/cats/data/package.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cats
22

33
package object data {
44
type NonEmptyStream[A] = OneAnd[Stream, A]
5-
type ValidatedNel[E, A] = Validated[NonEmptyList[E], A]
5+
type ValidatedNel[+E, +A] = Validated[NonEmptyList[E], A]
66

77
def NonEmptyStream[A](head: A, tail: Stream[A] = Stream.empty): NonEmptyStream[A] =
88
OneAnd(head, tail)

0 commit comments

Comments
 (0)