Skip to content

Commit

Permalink
Added collectFirstSome to Chain and NonEmptyChain. Overrided collectF…
Browse files Browse the repository at this point in the history
…irst and collectFirstSome in the Foldable instances of Chain and NonEmptyChain.
  • Loading branch information
LMnet committed Apr 18, 2019
1 parent 92fc8b3 commit e78385d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
15 changes: 12 additions & 3 deletions core/src/main/scala/cats/data/Chain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ sealed abstract class Chain[+A] {
}

/**
* Finds the first element of this `Chain` for which the given partial
* function is defined, and applies the partial function to it.
*/
* 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 =>
Expand All @@ -162,6 +162,13 @@ sealed abstract class Chain[+A] {
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] =
collectFirst(Function.unlift(f))

/**
* Remove elements not matching the predicate
*/
Expand Down Expand Up @@ -577,6 +584,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: 15 additions & 3 deletions core/src/main/scala/cats/data/NonEmptyChain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,17 @@ 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.
*/
* 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 @@ -479,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

0 comments on commit e78385d

Please sign in to comment.