Skip to content

Instantly share code, notes, and snippets.

@mathhun
Forked from travisbrown/EitherBench.scala
Created August 8, 2016 02:54
Show Gist options
  • Save mathhun/996716cffdb018ae80b82647367713f8 to your computer and use it in GitHub Desktop.
Save mathhun/996716cffdb018ae80b82647367713f8 to your computer and use it in GitHub Desktop.
package cats.bench
import cats.data.Xor
import cats.instances.either._
import cats.syntax.all._
import org.openjdk.jmh.annotations.{ Benchmark, Scope, State }
@State(Scope.Benchmark)
class EitherBench {
val ea: Either[String, Int] = Right(1)
val eb: Either[String, Int] = Right(2)
val ec: Either[String, Int] = Right(3)
val xa: Xor[String, Int] = Xor.right(1)
val xb: Xor[String, Int] = Xor.right(2)
val xc: Xor[String, Int] = Xor.right(3)
@Benchmark def flatMapsEither(): Either[String, Int] = for {
a <- ea; b <- eb; c <- ec
} yield a + b + c
@Benchmark def flatMapsXor(): Xor[String, Int] = for {
a <- xa; b <- xb; c <- xc
} yield a + b + c
@Benchmark def cartesianEither(): Either[String, Int] = (ea |@| eb |@| ec).map(_ + _ + _)
@Benchmark def cartesianXor(): Xor[String, Int] = (xa |@| xb |@| xc).map(_ + _ + _)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment