Skip to content

Instantly share code, notes, and snippets.

@chenharryhua
Created March 17, 2021 02:09
Show Gist options
  • Save chenharryhua/323bbe143a32794f33828bbafeabb6fc to your computer and use it in GitHub Desktop.
Save chenharryhua/323bbe143a32794f33828bbafeabb6fc to your computer and use it in GitHub Desktop.
import cats.syntax.all._
import cats.data._
import cats._
val f = Validated.invalid[Eval[String], Eval[Int]](Eval.always { println("eval fail"); "oops" })
val s = Validated.valid[Eval[String], Eval[Int]](Eval.always { println("eval succ"); 1 })
val combine: Validated[Eval[String], Eval[Int]] = f *> s
val res = combine.bimap(_.value, _.value) match {
case Validated.Valid(a) => println(a)
case Validated.Invalid(e) => println(e)
}
@chenharryhua
Copy link
Author

chenharryhua commented Mar 17, 2021

compare to

 val f: Either[Eval[String], Eval[Int]] = Left[Eval[String], Eval[Int]](Eval.always { println("eval fail"); "oops" })
  val s: Either[Eval[String], Eval[Int]] = Right[Eval[String], Eval[Int]](Eval.always { println("eval succ"); 1 })

  val combine = f >> s

  val res = combine.bimap(_.value, _.value) match {
    case Left(a)  => println(a)
    case Right(e) => println(e)
  }

f and s both evaluated in Validated. only f is evaluated in Either

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment