Skip to content

Instantly share code, notes, and snippets.

@vsuharnikov
Created April 7, 2019 08:24
Show Gist options
  • Save vsuharnikov/3c6c0971196f2eb21eb9e01cfa0371df to your computer and use it in GitHub Desktop.
Save vsuharnikov/3c6c0971196f2eb21eb9e01cfa0371df to your computer and use it in GitHub Desktop.
cats - foldLeftM - short circuit
val xs: List[Int] = List(1, 1, 2, 1, 1)
val result = xs.foldLeftM(0) {
case (r, x) =>
println(s"r=$r, x=$x")
if (x % 2 == 0) Left("even")
else Right(r + x)
}
println(result)
/*
r=0, x=1
r=1, x=1
r=2, x=2
Left(even)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment