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))
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
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.collectSomeFold(f) res0: Int = 6
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
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)
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)
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[]