FoldableOps

final class FoldableOps[F[_], A](fa: F[A]) extends AnyVal
class AnyVal
trait Matchable
class Any

Value members

Concrete methods

def collectFirstSomeM[G[_], B](f: A => G[Option[B]])(F: Foldable[F], G: Monad[G]): G[Option[B]]

Monadic version of collectFirstSome.

Monadic version of collectFirstSome.

If there are no elements, the result is None. collectFirstSomeM short-circuits, i.e. once a Some element is found, no further effects are produced.

For example:

scala> import cats.implicits._
scala> def parseInt(s: String): Either[String, Int] = Either.catchOnly[NumberFormatException](s.toInt).leftMap(_.getMessage)
scala> val keys1 = List("1", "2", "4", "5")
scala> val map1 = Map(4 -> "Four", 5 -> "Five")
scala> keys1.collectFirstSomeM(parseInt(_) map map1.get)
res0: scala.util.Either[String,Option[String]] = Right(Some(Four))

scala> val map2 = Map(6 -> "Six", 7 -> "Seven")
scala> keys1.collectFirstSomeM(parseInt(_) map map2.get)
res1: scala.util.Either[String,Option[String]] = Right(None)

scala> val keys2 = List("1", "x", "4", "5")
scala> keys2.collectFirstSomeM(parseInt(_) map map1.get)
res2: scala.util.Either[String,Option[String]] = Left(For input string: "x")

scala> val keys3 = List("1", "2", "4", "x")
scala> keys3.collectFirstSomeM(parseInt(_) map map1.get)
res3: scala.util.Either[String,Option[String]] = Right(Some(Four))
def collectFold[M](f: PartialFunction[A, M])(F: Foldable[F], M: Monoid[M]): M

Tear down a subset of this structure using a PartialFunction.

Tear down a subset of this structure using a PartialFunction.

scala> import cats.implicits._
scala> val xs = List(1, 2, 3, 4)
scala> xs.collectFold { case n if n % 2 == 0 => n }
res0: Int = 6
def contains_(v: A)(ev: Eq[A], F: Foldable[F]): Boolean

test if F[A] contains an A, named contains_ to avoid conflict with existing contains which uses universal equality

test if F[A] contains an A, named contains_ to avoid conflict with existing contains which uses universal equality

Example:

scala> import cats.implicits._

scala> val l: List[Int] = List(1, 2, 3, 4)
scala> l.contains_(1)
res0: Boolean = true
scala> l.contains_(5)
res1: Boolean = false
def findM[G[_]](p: A => G[Boolean])(F: Foldable[F], G: Monad[G]): G[Option[A]]

Find the first element matching the effectful predicate, if one exists.

Find the first element matching the effectful predicate, if one exists.

If there are no elements, the result is None. findM short-circuits, i.e. once an element is found, no further effects are produced.

For example:

scala> import cats.implicits._
scala> val list = List(1,2,3,4)
scala> list.findM(n => (n >= 2).asRight[String])
res0: Either[String,Option[Int]] = Right(Some(2))

scala> list.findM(n => (n > 4).asRight[String])
res1: Either[String,Option[Int]] = Right(None)

scala> list.findM(n => Either.cond(n < 3, n >= 2, "error"))
res2: Either[String,Option[Int]] = Right(Some(2))

scala> list.findM(n => Either.cond(n < 3, false, "error"))
res3: Either[String,Option[Int]] = Left(error)
def foldA[G[_], B](F: Foldable[F], ev: A <:< G[B], G: Applicative[G], B: Monoid[B]): G[B]
def foldSmash(prefix: A, delim: A, suffix: A)(A: Monoid[A], F: Foldable[F]): A

Intercalate with a prefix and a suffix

Intercalate with a prefix and a suffix

Example:

scala> import cats.implicits._

scala> val l: List[String] = List("1", "2", "3")
scala> l.foldSmash("List(", ",", ")")
res0: String = List(1,2,3)
def foldl[B](b: B)(f: (B, A) => B)(F: Foldable[F]): B
def foldr[B](b: Eval[B])(f: (A, Eval[B]) => Eval[B])(F: Foldable[F]): Eval[B]
def mkString_(prefix: String, delim: String, suffix: String)(A: Show[A], F: Foldable[F]): String

Make a string using Show, prefix, delimiter, and suffix.

Make a string using Show, prefix, delimiter, and suffix.

Named as mkString_ to avoid conflict.

Example:

scala> import cats.implicits._

scala> val l: List[Int] = List(1, 2, 3)
scala> l.mkString_("L[", ";", "]")
res0: String = L[1;2;3]
scala> val el: List[Int] = List()
scala> el.mkString_("L[", ";", "]")
res1: String = L[]

Deprecated methods

@deprecated("Use collectFoldSome", "2.1.0-RC1")
def collectSomeFold[M](f: A => Option[M])(F: Foldable[F], M: Monoid[M]): M

Tear down a subset of this structure using a A => Option[M].

Tear down a subset of this structure using a A => Option[M].

scala> import cats.implicits._
scala> val xs = List(1, 2, 3, 4)
scala> def f(n: Int): Option[Int] = if (n % 2 == 0) Some(n) else None
scala> xs.collectFoldSome(f)
res0: Int = 6
Deprecated