Skip to content

Instantly share code, notes, and snippets.

@cm-kazup0n
Created February 17, 2024 08:23
Show Gist options
  • Save cm-kazup0n/d20e8b0cd8e9e1442cef33ef8a457b89 to your computer and use it in GitHub Desktop.
Save cm-kazup0n/d20e8b0cd8e9e1442cef33ef8a457b89 to your computer and use it in GitHub Desktop.
//> using dep com.lihaoyi::pprint:0.8.1
//> using dep io.circe::circe-core::0.14.6
//> using dep io.circe::circe-generic::0.14.6
import io.circe._
import io.circe.generic.semiauto._
import io.circe.Json._
import pprint._
final case class Cat(name: String)
object Cat:
given decoder: Decoder[Cat] = deriveDecoder
given deriveSafeDecoder[A: Decoder]: Decoder[List[Either[DecodingFailure, A]]] =
Decoder[List[Json]].map(_.map(_.as[A]))
type Result[A] = (List[DecodingFailure], List[A])
given partitioned[A: Decoder]: Decoder[Result[A]] =
deriveSafeDecoder[A].map(
_.foldLeft((List.empty[DecodingFailure], List.empty[A])) {
case ((ls, rs), Left(l)) => (l :: ls, rs)
case ((ls, rs), Right(r)) => (ls, r :: rs)
}
)
val input = arr(
obj("name" -> fromString("nyan")),
obj("name" -> fromString("meow")),
obj("name" -> fromInt(1234)),
obj("name" -> fromString("miaou"))
)
pprintln(partitioned[Cat].apply(input.hcursor))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment