Packages

c

cats.syntax

FoldableOps

final class FoldableOps[F[_], A] extends AnyVal

Source
foldable.scala
Linear Supertypes
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. FoldableOps
  2. AnyVal
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Instance Constructors

  1. new FoldableOps(fa: F[A])

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    Any
  2. final def ##(): Int
    Definition Classes
    Any
  3. final def ==(arg0: Any): Boolean
    Definition Classes
    Any
  4. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  5. def collectFirstSomeM[G[_], B](f: (A) ⇒ G[Option[B]])(implicit 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))
  6. def collectFold[M](f: PartialFunction[A, M])(implicit 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
  7. def contains_(v: A)(implicit 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
  8. def findM[G[_]](p: (A) ⇒ G[Boolean])(implicit 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)
  9. def foldA[G[_], B](implicit F: Foldable[F], ev: <:<[A, G[B]], G: Applicative[G], B: Monoid[B]): G[B]
  10. def foldSmash(prefix: A, delim: A, suffix: A)(implicit 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)
  11. def foldl[B](b: B)(f: (B, A) ⇒ B)(implicit F: Foldable[F]): B
  12. def foldr[B](b: Eval[B])(f: (A, Eval[B]) ⇒ Eval[B])(implicit F: Foldable[F]): Eval[B]
  13. def getClass(): Class[_ <: AnyVal]
    Definition Classes
    AnyVal → Any
  14. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  15. def mkString_(prefix: String, delim: String, suffix: String)(implicit 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[]
  16. def toString(): String
    Definition Classes
    Any

Deprecated Value Members

  1. def collectSomeFold[M](f: (A) ⇒ Option[M])(implicit 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
    Annotations
    @deprecated
    Deprecated

    (Since version 2.1.0-RC1) Use collectFoldSome

Inherited from AnyVal

Inherited from Any

Ungrouped