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

Added collectFirst to Chain and NonEmptyChain #2796

Merged
merged 3 commits into from
May 1, 2019
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
35 changes: 35 additions & 0 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,39 @@ sealed abstract class Chain[+A] {
else acc
}

/**
* Finds the first element of this `Chain` for which the given partial
* function is defined, and applies the partial function to it.
*/
final def collectFirst[B](pf: PartialFunction[A, B]): Option[B] = {
var result: Option[B] = None
foreachUntil { a =>
// trick from TraversableOnce, used to avoid calling both isDefined and apply (or calling lift)
val x = pf.applyOrElse(a, sentinel)
if (x.asInstanceOf[AnyRef] ne sentinel) {
result = Some(x.asInstanceOf[B])
true
} else false
}
result
}

/**
* Like `collectFirst` from `scala.collection.Traversable` but takes `A => Option[B]`
* instead of `PartialFunction`s.
*/
final def collectFirstSome[B](f: A => Option[B]): Option[B] = {
var result: Option[B] = None
foreachUntil { a =>
val x = f(a)
if (x.isDefined) {
result = x
true
} else false
}
result
}

/**
* Remove elements not matching the predicate
*/
Expand Down Expand Up @@ -560,6 +593,8 @@ sealed abstract private[data] class ChainInstances extends ChainInstances1 {
override def forall[A](fa: Chain[A])(p: A => Boolean): Boolean = fa.forall(p)
override def find[A](fa: Chain[A])(f: A => Boolean): Option[A] = fa.find(f)
override def size[A](fa: Chain[A]): Long = fa.length
override def collectFirst[A, B](fa: Chain[A])(pf: PartialFunction[A, B]): Option[B] = fa.collectFirst(pf)
override def collectFirstSome[A, B](fa: Chain[A])(f: A => Option[B]): Option[B] = fa.collectFirstSome(f)

def coflatMap[A, B](fa: Chain[A])(f: Chain[A] => B): Chain[B] = {
@tailrec def go(as: Chain[A], res: ListBuffer[B]): Chain[B] =
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/scala/cats/data/NonEmptyChain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,18 @@ class NonEmptyChainOps[A](private val value: NonEmptyChain[A]) extends AnyVal {
*/
final def collect[B](pf: PartialFunction[A, B]): Chain[B] = toChain.collect(pf)

/**
* Finds the first element of this `NonEmptyChain` for which the given partial
* function is defined, and applies the partial function to it.
*/
final def collectFirst[B](pf: PartialFunction[A, B]): Option[B] = toChain.collectFirst(pf)

/**
* Like `collectFirst` from `scala.collection.Traversable` but takes `A => Option[B]`
* instead of `PartialFunction`s.
*/
final def collectFirstSome[B](f: A => Option[B]): Option[B] = toChain.collectFirstSome(f)

/**
* Filters all elements of this chain that do not satisfy the given predicate.
*/
Expand Down Expand Up @@ -473,6 +485,12 @@ sealed abstract private[data] class NonEmptyChainInstances extends NonEmptyChain

override def toNonEmptyList[A](fa: NonEmptyChain[A]): NonEmptyList[A] =
fa.toNonEmptyList

override def collectFirst[A, B](fa: NonEmptyChain[A])(pf: PartialFunction[A, B]): Option[B] =
fa.collectFirst(pf)

override def collectFirstSome[A, B](fa: NonEmptyChain[A])(f: A => Option[B]): Option[B] =
fa.collectFirstSome(f)
}

implicit def catsDataOrderForNonEmptyChain[A: Order]: Order[NonEmptyChain[A]] =
Expand Down