@@ -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
@@ -45,7 +46,7 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
45
46
/**
46
47
* Append another NonEmptyList
47
48
*/
48
- def concat (other : NonEmptyList [A ]): NonEmptyList [A ] =
49
+ def concat [ AA >: A ] (other : NonEmptyList [AA ]): NonEmptyList [AA ] =
49
50
NonEmptyList (head, tail ::: other.toList)
50
51
51
52
/**
@@ -82,8 +83,8 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
82
83
/**
83
84
* Left-associative reduce using f.
84
85
*/
85
- def reduceLeft (f : (A , A ) => A ): A =
86
- tail.foldLeft(head)(f)
86
+ def reduceLeft [ AA >: A ] (f : (AA , AA ) => AA ): AA =
87
+ tail.foldLeft[ AA ] (head)(f)
87
88
88
89
def traverse [G [_], B ](f : A => G [B ])(implicit G : Applicative [G ]): G [NonEmptyList [B ]] =
89
90
G .map2Eval(f(head), Always (Traverse [List ].traverse(tail)(f)))(NonEmptyList (_, _)).value
@@ -100,23 +101,23 @@ final case class NonEmptyList[A](head: A, tail: List[A]) {
100
101
NonEmptyList (f(this ), consume(tail))
101
102
}
102
103
103
- def === (o : NonEmptyList [A ])(implicit A : Eq [A ]): Boolean =
104
- (this .head === o.head) && this .tail === o.tail
104
+ def === [ AA >: A ] (o : NonEmptyList [AA ])(implicit AA : Eq [AA ]): Boolean =
105
+ (( this .head: AA ) === o.head) && ( this .tail: List [ AA ]) === o.tail
105
106
106
- def show (implicit A : Show [A ]): String =
107
- toList.iterator.map(A .show).mkString(" NonEmptyList(" , " , " , " )" )
107
+ def show [ AA >: A ] (implicit AA : Show [AA ]): String =
108
+ toList.iterator.map(AA .show).mkString(" NonEmptyList(" , " , " , " )" )
108
109
109
110
override def toString : String = s " NonEmpty $toList"
110
111
111
112
/**
112
113
* Remove duplicates. Duplicates are checked using `Order[_]` instance.
113
114
*/
114
- def distinct (implicit O : Order [A ]): NonEmptyList [A ] = {
115
+ def distinct [ AA >: A ] (implicit O : Order [AA ]): NonEmptyList [A ] = {
115
116
implicit val ord = O .toOrdering
116
117
117
118
val buf = ListBuffer .empty[A ]
118
- tail.foldLeft(TreeSet (head)) { (elementsSoFar, a ) =>
119
- if (elementsSoFar(a )) elementsSoFar else { buf += a ; elementsSoFar + a }
119
+ tail.foldLeft(TreeSet [ AA ] (head)) { (elementsSoFar, b ) =>
120
+ if (elementsSoFar(b )) elementsSoFar else { buf += b ; elementsSoFar + b }
120
121
}
121
122
122
123
NonEmptyList (head, buf.toList)
0 commit comments