Skip to content

Commit d3d1635

Browse files
author
Luka Jacobowitz
committed
Add groupByNem to NonEmptyList
1 parent 5811a50 commit d3d1635

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,21 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) {
375375
case (k, v) => (k, NonEmptyList.fromListUnsafe(v.result))
376376
} : TreeMap[B, NonEmptyList[A]]
377377
}
378+
379+
/**
380+
* Groups elements inside this `NonEmptyList` according to the `Order`
381+
* of the keys produced by the given mapping function.
382+
*
383+
* {{{
384+
* scala> import cats.data._
385+
* scala> import cats.instances.boolean._
386+
* scala> val nel = NonEmptyList.of(12, -2, 3, -5)
387+
* scala> nel.groupByNem(_ >= 0)
388+
* res0: NonEmptyMap[Boolean, NonEmptyList[Int]] = Map(false -> NonEmptyList(-2, -5), true -> NonEmptyList(12, 3))
389+
* }}}
390+
*/
391+
def groupByNem[B](f: A => B)(implicit B: Order[B]): NonEmptyMap[B, NonEmptyList[A]] =
392+
NonEmptyMap.fromMapUnsafe(groupBy(f))
378393
}
379394

380395
object NonEmptyList extends NonEmptyListInstances {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import scala.collection.immutable._
2626
private[data] trait Newtype2 { self =>
2727
type Base
2828
trait Tag extends Any
29-
type Type[A, B] <: Base with Tag
29+
type Type[A, +B] <: Base with Tag
3030
}
3131

3232

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package object data {
1111
def NonEmptyStream[A](head: A, tail: A*): NonEmptyStream[A] =
1212
OneAnd(head, tail.toStream)
1313

14-
type NonEmptyMap[K, A] = NonEmptyMapImpl.Type[K, A]
14+
type NonEmptyMap[K, +A] = NonEmptyMapImpl.Type[K, A]
1515
val NonEmptyMap = NonEmptyMapImpl
1616

1717
type ReaderT[F[_], A, B] = Kleisli[F, A, B]

0 commit comments

Comments
 (0)