Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scanLeft/scanLeftTail to all non-empty collections #4595

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/src/main/scala-2.13+/cats/data/NonEmptyLazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
final def foldRight[B](z: B)(f: (A, B) => B): B =
toLazyList.foldRight(z)(f)

final def scanLeft[B](b: B)(f: (B, A) => B): NonEmptyLazyList[B] =
create(toLazyList.scanLeft(b)(f))

final def scanLeftTail[B](b: B)(f: (B, A) => B): NonEmptyLazyList[B] =
create(toLazyList.scanLeft(b)(f).tail)

/**
* Left-associative reduce using f.
*/
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyChain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ class NonEmptyChainOps[A](private val value: NonEmptyChain[A])
final def foldLeft[B](b: B)(f: (B, A) => B): B =
toChain.foldLeft(b)(f)

final def scanLeft[B](b: B)(f: (B, A) => B): NonEmptyChain[B] = {
var current = b
var result = Chain.one[B](b)
val iter = toChain.iterator
while (iter.hasNext) {
current = f(current, iter.next())
result = result :+ current
}
create(result)
}

final def scanLeftTail[B](b: B)(f: (B, A) => B): NonEmptyChain[B] =
create(scanLeft(b)(f).tail)

/**
* Right-associative fold using f.
*/
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ private[cats] trait NonEmptyCollection[+A, U[+_], NE[+_]] extends Any {
def forall(p: A => Boolean): Boolean

def foldLeft[B](b: B)(f: (B, A) => B): B
def scanLeft[B](b: B)(f: (B, A) => B): NE[B]
def scanLeftTail[B](b: B)(f: (B, A) => B): NE[B]
def reduce[AA >: A](implicit S: Semigroup[AA]): AA

def zipWith[B, C](b: NE[B])(f: (A, B) => C): NE[C]
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
def foldRight[B](lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
Foldable[List].foldRight(toList, lb)(f)

def scanLeft[B](b: B)(f: (B, A) => B): NonEmptyList[B] =
NonEmptyList(b, tail.scanLeft(f(b, head))(f))

def scanLeftTail[B](b: B)(f: (B, A) => B): NonEmptyList[B] =
NonEmptyList.fromListUnsafe(tail.scanLeft(f(b, head))(f))

/**
* Left-associative reduce using f.
*/
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/NonEmptySeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ final class NonEmptySeq[+A] private (val toSeq: Seq[A]) extends AnyVal with NonE
def foldLeft[B](b: B)(f: (B, A) => B): B =
toSeq.foldLeft(b)(f)

def scanLeft[B](b: B)(f: (B, A) => B): NonEmptySeq[B] =
new NonEmptySeq(toSeq.scanLeft(b)(f))

def scanLeftTail[B](b: B)(f: (B, A) => B): NonEmptySeq[B] =
new NonEmptySeq(toSeq.scanLeft(b)(f).tail)

/**
* Right-associative fold using f.
*/
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
def foldLeft[B](b: B)(f: (B, A) => B): B =
toVector.foldLeft(b)(f)

def scanLeft[B](b: B)(f: (B, A) => B): NonEmptyVector[B] =
NonEmptyVector.fromVectorUnsafe(toVector.scanLeft(b)(f))

def scanLeftTail[B](b: B)(f: (B, A) => B): NonEmptyVector[B] =
NonEmptyVector.fromVectorUnsafe(tail.scanLeft(f(b, head))(f))

/**
* Right-associative fold using f.
*/
Expand Down
Loading