Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Last active November 18, 2017 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travisbrown/06a80937caf5d29df9f7ae2c12e5184a to your computer and use it in GitHub Desktop.
Save travisbrown/06a80937caf5d29df9f7ae2c12e5184a to your computer and use it in GitHub Desktop.
// Before
import io.circe.Decoder
case class Qux(a: Int, b: String)
val decodeQuxMonadic: Decoder[Qux] = Decoder.instance { c =>
for {
a <- c.get[Int]("a")
b <- c.get[String]("b")
} yield Qux(a, b)
}
// After
import cats.implicits._
import io.circe.Decoder
case class Qux(a: Int, b: String)
val decodeQuxApplicative: Decoder[Qux] = (
Decoder[Int].prepare(_.downField("a")),
Decoder[String].prepare(_.downField("b"))
).mapN(Qux.apply _)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment