Here I have a list of patterns that can be simplified with Scala's Cats library.
Examples that are given here are purely for demonstration.
Example:
val list: List[Int] = List(1, 2, 3, 4, 5)
val yearSum = list.collect {
case i if i % 2 == 0 => i
}.sum
val yearSum1 = list.collectFold {
case i if i % 2 == 0 => i
}
Example:
val listOfStrings = List("In", "cats", "we", "trust")
listOfStrings.map(_.length).sum
listOfStrings.foldMap(_.length)
Example:
val listOfTuples = List((1, 0), (2, 2), (3, 4))
listOfTuples.foldLeft((0, 0))(_ |+| _)
listOfTuples.combineAll
Example:
val listInts = List(1, 2, 3, 5)
listInts.flatMap(x => if (x % 2 == 0) Some(x + 1) else None).sum
listInts.collectFoldSome(x => if (x % 2 == 0) Some(x + 1) else None)
Example:
Option(1) match {
case Some(v) => Right(v)
case None => Left("error")
}
Either.fromOption(Option(1), "error")
Try("12".toInt) match {
case Failure(exception) => Left(exception.getMessage)
case Success(v) => Right(v)
}
Either.fromTry(Try("12".toInt)).leftMap(_.getMessage)