Skip to content

Instantly share code, notes, and snippets.

@stephen-lazaro
Last active November 9, 2018 18:50
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 stephen-lazaro/15aff2965d61346c4646af03771deb24 to your computer and use it in GitHub Desktop.
Save stephen-lazaro/15aff2965d61346c4646af03771deb24 to your computer and use it in GitHub Desktop.
implicit val alternativeForRowDecoder =
new Alternative[RowDecoder] {
  // Kind of like true, same as in our Monoidal above
  def pure[A](x: A): RowDecoder[A] =
Monoidal[RowDecoder].pure(x)
// Kind of like AND, same as in our Monoidal abaove
  def product[A, B](fa: RowDecoder[A], fb: RowDecoder[B]): RowDecoder[(A, B)] =
Monoidal[RowDecoder].product(fa, fb)
// Kind of like false but for decoders
  def empty[A]: F[A] =
new RowDecoder[A] {
  def apply(s: String) = Left(new RuntimeError("no dice"))
  }
// Kind of like XOR but for decoders
  def combineK[A](x: F[A], y: F[A]): F[A] =
  RowDecoder.from(z =>
  x.decode(z) match {
  case Left(_) => y.decode(z)
  case Right(v) => Right(v)
  })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment