Skip to content

Latest commit

 

History

History
85 lines (54 loc) · 1.54 KB

default.md

File metadata and controls

85 lines (54 loc) · 1.54 KB

Scala cats patterns

date: 24.01.2021 16:25

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.

Use .collectFold instead of .collect + .sum

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
}

Use .foldMap instead of .map + .sum

Example:

val listOfStrings = List("In", "cats", "we", "trust")

listOfStrings.map(_.length).sum

listOfStrings.foldMap(_.length)

Use .combineAll instead of .foldLeft + .combine

Example:

val listOfTuples = List((1, 0), (2, 2), (3, 4))

listOfTuples.foldLeft((0, 0))(_ |+| _)

listOfTuples.combineAll

Use .collectFoldSome instead of .flatMap + .sum

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)

Use Either.fromOption instead of pattern matching on Option

Example:

Option(1) match {
  case Some(v) => Right(v)
  case None    => Left("error")
}

Either.fromOption(Option(1), "error")

Use Either.fromTry instead of pattern matching on Try

Try("12".toInt) match {
  case Failure(exception) => Left(exception.getMessage)
  case Success(v)         => Right(v)
}

Either.fromTry(Try("12".toInt)).leftMap(_.getMessage)