From 88a62da4ddd236295dc0feed71af4624700dd3a1 Mon Sep 17 00:00:00 2001 From: Yui Pham Date: Mon, 6 Jan 2020 11:44:09 -0500 Subject: [PATCH 1/3] add List#scanLeftNel --- core/src/main/scala/cats/syntax/list.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index d1f1e8b7b8..cbfbe00902 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -50,6 +50,12 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { implicit val ordering: Ordering[B] = B.toOrdering toNel.fold(SortedMap.empty[B, NonEmptyList[A]])(_.groupBy(f)) } + + /** Produces a `NonEmptyList` containing cumulative results of applying the + * operator going left to right. + */ + def scanLeftNel[B](b: B)(f: (B, A) => B): NonEmptyList[B] = + NonEmptyList.fromListUnsafe(la.scanLeft(b)(f)) } private[syntax] trait ListSyntaxBinCompat0 { From fde2c9050f67762699d6bbfc0c326d3f7aa781b5 Mon Sep 17 00:00:00 2001 From: enzief Date: Wed, 8 Jan 2020 23:15:01 -0500 Subject: [PATCH 2/3] PR comments --- core/src/main/scala/cats/syntax/list.scala | 39 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index cbfbe00902..350df39448 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -1,9 +1,10 @@ package cats package syntax -import scala.collection.immutable.SortedMap import cats.data.{NonEmptyChain, NonEmptyList} +import scala.collection.immutable.SortedMap + trait ListSyntax { implicit final def catsSyntaxList[A](la: List[A]): ListOps[A] = new ListOps(la) } @@ -52,10 +53,44 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { } /** Produces a `NonEmptyList` containing cumulative results of applying the - * operator going left to right. + * operator going left to right. + * + * Example: + * {{{ + * scala> import cats.data.NonEmptyList + * scala> import cats.implicits._ + * + * scala> val result1: List[Int] = List(1, 2) + * scala> result1.scanLeftNel(100)(_ + _) + * res0: NonEmptyList[Int] = NonEmptyList(100, 101, 102) + * + * scala> val result2: List[Int] = List.empty[Int] + * scala> result2.scanLeftNel(1)(_ + _) + * res1: NonEmptyList[Int] = NonEmptyList(1) + * }}} */ def scanLeftNel[B](b: B)(f: (B, A) => B): NonEmptyList[B] = NonEmptyList.fromListUnsafe(la.scanLeft(b)(f)) + + /** Produces a `NonEmptyList` containing cumulative results of applying the + * operator going right to left. + * + * Example: + * {{{ + * scala> import cats.data.NonEmptyList + * scala> import cats.implicits._ + * + * scala> val result1: List[Int] = List(1, 2) + * scala> result1.scanRightNel(100)(_ + _) + * res0: NonEmptyList[Int] = NonEmptyList(102, 101, 100) + * + * scala> val result2: List[Int] = List.empty[Int] + * scala> result2.scanRightNel(1)(_ + _) + * res1: NonEmptyList[Int] = NonEmptyList(1) + * }}} + */ + def scanRightNel[B](b: B)(f: (A, B) => B): NonEmptyList[B] = + NonEmptyList.fromListUnsafe(la.scanRight(b)(f)) } private[syntax] trait ListSyntaxBinCompat0 { From 0a14b8fba3da67633e218cf3fe46915540f092a4 Mon Sep 17 00:00:00 2001 From: enzief Date: Thu, 9 Jan 2020 00:44:24 -0500 Subject: [PATCH 3/3] fix doc test --- core/src/main/scala/cats/syntax/list.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/syntax/list.scala b/core/src/main/scala/cats/syntax/list.scala index 350df39448..0891a2f1bf 100644 --- a/core/src/main/scala/cats/syntax/list.scala +++ b/core/src/main/scala/cats/syntax/list.scala @@ -62,7 +62,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * * scala> val result1: List[Int] = List(1, 2) * scala> result1.scanLeftNel(100)(_ + _) - * res0: NonEmptyList[Int] = NonEmptyList(100, 101, 102) + * res0: NonEmptyList[Int] = NonEmptyList(100, 101, 103) * * scala> val result2: List[Int] = List.empty[Int] * scala> result2.scanLeftNel(1)(_ + _) @@ -82,7 +82,7 @@ final class ListOps[A](private val la: List[A]) extends AnyVal { * * scala> val result1: List[Int] = List(1, 2) * scala> result1.scanRightNel(100)(_ + _) - * res0: NonEmptyList[Int] = NonEmptyList(102, 101, 100) + * res0: NonEmptyList[Int] = NonEmptyList(103, 102, 100) * * scala> val result2: List[Int] = List.empty[Int] * scala> result2.scanRightNel(1)(_ + _)