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 1 commit
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
Prev Previous commit
Add scanLeftTail to NonEmptyCollection.
Considering doing a scan on a non-empty collection yields a collection
with at least _two_ elements, this method helps users discover that
fact.
  • Loading branch information
Prillan committed May 3, 2024
commit 3b18c23a726062a78ff15569c35227ad97501860
3 changes: 3 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 @@ -277,6 +277,9 @@ class NonEmptyLazyListOps[A](private val value: NonEmptyLazyList[A])
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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyChain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ class NonEmptyChainOps[A](private val value: NonEmptyChain[A])
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
1 change: 1 addition & 0 deletions core/src/main/scala/cats/data/NonEmptyCollection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private[cats] trait NonEmptyCollection[+A, U[+_], NE[+_]] extends Any {

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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/NonEmptySeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ final class NonEmptySeq[+A] private (val toSeq: Seq[A]) extends AnyVal with NonE
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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])
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