Skip to content

Instantly share code, notes, and snippets.

View yasuabe's full-sized avatar

Yasuyuki Abe yasuabe

View GitHub Profile
package type_classes.partialorder
import cats.PartialOrder
import cats.syntax.partialOrder._
import cats.instances.AllInstances
import cats.kernel.BoundedSemilattice
import cats.kernel.laws.discipline.{BoundedSemilatticeTests, PartialOrderTests}
import org.scalacheck.{Arbitrary, Gen}
import org.specs2.Specification
import org.typelevel.discipline.specs2.Discipline
import cats.MonoidK
import scala.util.Try
import cats.data.Kleisli
import cats.syntax.option._
import cats.syntax.either._
import cats.syntax.compose._
import cats.syntax.foldable._
import cats.syntax.arrow._
import cats.syntax.arrowChoice._
import shapeless.ops.hlist.{Length, Repeat}
import shapeless.{::, Generic, HList, HNil, Nat}
case class IceCream(name: String, numCherries: Int, inCone: Boolean)
def repeat[I <: HList, L <: Nat, O <: HList](in: I, s: String)(
implicit
len: Length.Aux[I, L],
rep: Repeat.Aux[String :: HNil, L, O]
): O = rep(s :: HNil)
@yasuabe
yasuabe / sized_vector_matrix.sc
Last active January 18, 2019 08:12
implement vector and matrix with Shapeless's Sized
import shapeless.Nat._
import shapeless.ops.nat.ToInt
import shapeless.syntax.sized._
import shapeless.{Nat, Sized}
type Vect[N <: Nat] = Sized[IndexedSeq[Int], N]
def vect = Sized.apply[IndexedSeq]
def times[L <: Nat](k: Int, v1: Vect[L]): Vect[L] = v1.map(_ * k)
@yasuabe
yasuabe / accountability.sc
Created March 26, 2018 18:35
analysis patterns knowledge level implemented in type level programming
import shapeless.ops.coproduct.{IsCCons, Selector, Unifier}
import shapeless.{:+:, CNil, Coproduct}
// Party Types -------------------------
sealed trait PartyType
sealed trait Region extends PartyType
sealed trait Division extends PartyType
sealed trait Doctor extends PartyType
@yasuabe
yasuabe / DI_tagless.sc
Last active February 18, 2019 12:54
tagless final
import cats.{Id, Monad}
// domain layer ------------------------------------------------------
case class Movie(id: Int, title: String)
trait MovieRepoSym[F[_]] {
def getMovie(id: Int): F[Option[Movie]]
}
class MovieService[F[_]: Monad](sym: MovieRepoSym[F]) {
@yasuabe
yasuabe / DI_tagless_min_cake.sc
Last active February 18, 2019 13:14
minimal cake + tagless final
import cats.data.Chain
// domain layer -----------------------------------------------------------
case class Movie(id: Int, title: String)
trait MovieRepo[F[_]] {
def getMovie(id: Int): F[Option[Movie]]
}
trait UsesMovieRepo[F[_]] {
val movieRepo: MovieRepo[F]
@yasuabe
yasuabe / DI_free_monad_min_cake.sc
Last active February 18, 2019 13:15
minimal cake + free monad
import cats.free.Free
import cats.free.Free.liftF
import cats.~>
// domain layer -----------------------------------------------------------
case class Movie(id: Int, title: String)
sealed trait Query[A]
case class GetMovie(id: Int) extends Query[Option[Movie]]
@yasuabe
yasuabe / DI_tagless_final_free_monad.sc
Last active February 18, 2019 12:48
tagless final + free monad
import cats.free.Free
import cats.free.Free.liftF
import cats.{Id, Monad, ~>}
import cats.syntax.option._
// domain layer -----------------------------------------------------------
case class Movie(id: Int, title: String)
sealed trait Query[A]
case class GetMovie(id: Int) extends Query[Option[Movie]]
@yasuabe
yasuabe / DI_minimal_cake_tagless_final_free_monad.sc
Last active February 18, 2019 12:59
minimal cake + tagless final + free monad
import cats.free.Free
import cats.free.Free.liftF
import cats.~>
// domain layer -----------------------------------------------------------
case class Movie(id: Int, title: String)
sealed trait Query[A]
case class GetMovie(id: Int) extends Query[Option[Movie]]