@@ -12,7 +12,7 @@ import scala.collection.mutable.ListBuffer
12
12
* A data type which represents a non empty list of A, with
13
13
* single element (head) and optional structure (tail).
14
14
*/
15
- final case class NonEmptyList [A ](head : A , tail : List [A ]) {
15
+ final case class NonEmptyList [+ A ](head : A , tail : List [A ]) {
16
16
17
17
/**
18
18
* Return the head and tail into a single list
@@ -25,13 +25,14 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
25
25
def map [B ](f : A => B ): NonEmptyList [B ] =
26
26
NonEmptyList (f(head), tail.map(f))
27
27
28
- def ++ (l : List [A ]): NonEmptyList [A ] =
28
+ def ++ [ AA >: A ] (l : List [AA ]): NonEmptyList [AA ] =
29
29
NonEmptyList (head, tail ++ l)
30
30
31
31
def flatMap [B ](f : A => NonEmptyList [B ]): NonEmptyList [B ] =
32
32
f(head) ++ tail.flatMap(f andThen (_.toList))
33
33
34
- def :: (a : A ): NonEmptyList [A ] = NonEmptyList (a, head :: tail)
34
+ def :: [AA >: A ](a : AA ): NonEmptyList [AA ] =
35
+ NonEmptyList (a, head :: tail)
35
36
36
37
/**
37
38
* Remove elements not matching the predicate
@@ -68,7 +69,7 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
68
69
/**
69
70
* Append another NonEmptyList
70
71
*/
71
- def concat (other : NonEmptyList [A ]): NonEmptyList [A ] =
72
+ def concat [ AA >: A ] (other : NonEmptyList [AA ]): NonEmptyList [AA ] =
72
73
NonEmptyList (head, tail ::: other.toList)
73
74
74
75
/**
@@ -105,8 +106,8 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
105
106
/**
106
107
* Left-associative reduce using f.
107
108
*/
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)
110
111
111
112
def traverse [G [_], B ](f : A => G [B ])(implicit G : Applicative [G ]): G [NonEmptyList [B ]] =
112
113
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]) {
123
124
NonEmptyList (f(this ), consume(tail))
124
125
}
125
126
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
128
129
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(" , " , " , " )" )
131
132
132
133
override def toString : String = s " NonEmpty $toList"
133
134
134
135
/**
135
136
* Remove duplicates. Duplicates are checked using `Order[_]` instance.
136
137
*/
137
- def distinct (implicit O : Order [A ]): NonEmptyList [A ] = {
138
+ def distinct [ AA >: A ] (implicit O : Order [AA ]): NonEmptyList [A ] = {
138
139
implicit val ord = O .toOrdering
139
140
140
141
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 }
143
144
}
144
145
145
146
NonEmptyList (head, buf.toList)
0 commit comments