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))
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
, named as mkString_
to avoid conflict
Make a string using Show
, 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[]