Skip to content

Instantly share code, notes, and snippets.

View vhutov's full-sized avatar

Vladyslav Hutov vhutov

View GitHub Profile
@vhutov
vhutov / FunctorUse.scala
Created June 23, 2020 22:41
Functor use
Decoder[FailedMessage].inj[Messages]
@vhutov
vhutov / Functor.scala
Created June 23, 2020 22:39
Functor + Inject
implicit class InjectableFunctor[F[_], A](fa: F[A]) {
def inj[B](implicit F: Functor[F], I: Inject[A, B]): F[B] = {
fa.map(I.inj)
}
}
Decoder[FailureResponse].map(Inject[FailedResponse, Messages].inj)
@vhutov
vhutov / Decoder.scala
Last active June 23, 2020 22:34
Decoder
Decoder[FailedResponse].map(m ⇒ Right(Left(m))
type Messages = SuccessResposne :=: FailedResponse :+: QuotaRequest
type A = Int :+: String :+: Double
type B = (Int :+: (String :+: Double))
type C = Either[Int, Either[String, Double]]
// A === B === C
@vhutov
vhutov / TypeAlias.scala
Created June 23, 2020 22:27
type alias
type :+:[A, B] = Either[A, B]
abstract class Inject[A, B] {
def inj: A ⇒ B
def prj: B ⇒ Option[A]
}
import cats.Inject
type B = Either[Int, Either[String, Double]]
val i = Inject[Int, B].inj(1) // Left(1)
val s = Inject[String, B].inj("hi") // Right(Left(hi))
val d = Inject[Double, B].inj(1.0) // Right(Right(1.0))
Inject[String, B].prj(i) // None
Inject[String, B].prj(s) // Some(hi)
@vhutov
vhutov / Parent.scala
Last active June 23, 2020 22:26
Third type
import io.circe.{Encoder, Decoder}
import cats.effect.Concurrent
import cats.syntax.either._
case class JmsMessage[T](value: T, metadata: Metadata)
class JmsParentController[
F[_] : Concurrent,
Request : Encoder,
SuccessResponse : Decoder,