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 collectFirst to the NonEmptyCollection interface #4275

Merged
merged 1 commit into from
Jul 28, 2022
Merged
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
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 @@ -40,6 +40,7 @@ private[cats] trait NonEmptyCollection[+A, U[+_], NE[+_]] extends Any {
def filter(f: A => Boolean): U[A]
def filterNot(p: A => Boolean): U[A]
def collect[B](pf: PartialFunction[A, B]): U[B]
def collectFirst[B](pf: PartialFunction[A, B]): Option[B]
def find(p: A => Boolean): Option[A]
def exists(p: A => Boolean): Boolean
def forall(p: A => Boolean): Boolean
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,16 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec
tail.collect(pf)
}

/**
* Find the first element matching the partial function, if one exists
*/
def collectFirst[B](pf: PartialFunction[A, B]): Option[B] =
if (pf.isDefinedAt(head)) {
Some(pf.apply(head))
} else {
tail.collectFirst(pf)
}

/**
* Find the first element matching the predicate, if one exists
*/
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/NonEmptySeq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ final class NonEmptySeq[+A] private (val toSeq: Seq[A]) extends AnyVal with NonE

def collect[B](pf: PartialFunction[A, B]): Seq[B] = toSeq.collect(pf)

def collectFirst[B](pf: PartialFunction[A, B]): Option[B] = toSeq.collectFirst(pf)

/**
* Alias for [[concat]]
*/
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyVector.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ final class NonEmptyVector[+A] private (val toVector: Vector[A])

def collect[B](pf: PartialFunction[A, B]): Vector[B] = toVector.collect(pf)

def collectFirst[B](pf: PartialFunction[A, B]): Option[B] = toVector.collectFirst(pf)

/**
* Alias for [[concat]]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ abstract class NonEmptyCollectionSuite[U[+_], NE[+_], NEC[x] <: NonEmptyCollecti
}
}

test("collectFirst is consistent with iterator.toList.collectFirst") {
forAll { (is: NE[Int], pf: PartialFunction[Int, String]) =>
assert(is.collectFirst(pf) === is.iterator.toList.collectFirst(pf))
}
}

test("find is consistent with iterator.toList.find") {
forAll { (is: NE[Int], pred: Int => Boolean) =>
assert(is.find(pred) === (is.iterator.toList.find(pred)))
Expand Down