Skip to content

Instantly share code, notes, and snippets.

View yasuabe's full-sized avatar

Yasuyuki Abe yasuabe

View GitHub Profile
@yasuabe
yasuabe / profunctor_ex01.sc
Created January 8, 2018 01:45
profunctor exercise 1
import cats.arrow.Profunctor
import cats.data.Kleisli
import eu.timepit.refined.api.Refined
import eu.timepit.refined.boolean.Not
import eu.timepit.refined.generic.Equal
import eu.timepit.refined.string.MatchesRegex
import eu.timepit.refined.{W, refineMV, refineV}
import shapeless.Nat._0
import cats.instances.function._
import cats.syntax.profunctor._
@yasuabe
yasuabe / eval.sc
Last active January 5, 2018 18:55
eval tarai
def measure(f: => Int): (Int, Long) = {
val start = System.currentTimeMillis()
val result = f
val millis = System.currentTimeMillis() - start
(result, millis)
}
def tarai(x: Int, y: Int, z: Int): Int =
if (x <= y) y else tarai(tarai(x - 1, y, z),
tarai(y - 1, z, x),
@yasuabe
yasuabe / shiritori.sc
Created January 2, 2018 19:10
HList type chain
import shapeless._
import shapeless.ops.hlist._
object typeChainCat {
trait Id[A] {
type Out
def apply(l: A): Out
}
object Id {
type Aux[L, O] = Id[L] { type Out = O }
@yasuabe
yasuabe / DI_freeT2.sc
Last active February 19, 2019 01:07
oop to fp - free & reader monads
import cats.data.{ReaderT, Writer}
import cats.free.Free
import cats.~>
// domain layer -----------------------------------------------------------
case class Movie(id: Int, title: String)
sealed trait Query[A]
case class GetMovie(id: Int) extends Query[Option[Movie]]
type QueryF[T] = Free[Query, T]
@yasuabe
yasuabe / DI_free.sc
Last active February 19, 2019 00:57
oop to fp - free monad
import cats.free.Free.liftF
import cats.free.Free
import cats.{Id, ~>}
// 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_reader.sc
Last active April 23, 2019 17:22
oop to fp - reader monad
import cats.syntax.option._
// domain layer -----------------------------------------------------------
import monix.eval.Task
import cats.data.ReaderT
case class Movie(id: Int, title: String)
trait MovieRepo {
def getMovie(id: Int): Task[Option[Movie]]
@yasuabe
yasuabe / DI_min_cake.sc
Last active February 19, 2019 00:40
oop to fp - minimum cake
import cats.syntax.option._
// domain layer -----------------------------------------------------------
import monix.eval.Task
case class Movie(id: Int, title: String)
trait MovieRepo {
def getMovie(id: Int): Task[Option[Movie]]
}
@yasuabe
yasuabe / DI_cake.sc
Last active February 19, 2019 00:31
oop to fp - original cake
import cats.syntax.option._
// domain layer -----------------------------------------------------------
import monix.eval.Task
case class Movie(id: Int, title: String)
trait MovieRepoComponent {
trait MovieRepo {
def getMovie(id: Int): Task[Option[Movie]]
}
@yasuabe
yasuabe / arrow_tutorial06.sc
Created December 30, 2017 16:10
arrow tutorial '6. a teaser'
import cats.Monoid
import cats.arrow.Arrow
import cats.data.Kleisli
sealed case class SimpleFunc[A, B](runF: A => B)
import cats.syntax.arrow._
import cats.syntax.compose._
def arr[F[_, _]: Arrow, A, B](f: A => B): F[A, B] = Arrow[F] lift f
@yasuabe
yasuabe / arrow_tutorial05.sc
Created December 30, 2017 16:07
arrow tutorial '5. kleisli arrows'
import cats.arrow.Arrow
import cats.data.Kleisli
sealed case class SimpleFunc[A, B](runF: A => B)
import cats.syntax.arrow._
import cats.syntax.compose._
def arr[F[_, _]: Arrow, A, B](f: A => B): F[A, B] = Arrow[F] lift f
def split[F[_, _]: Arrow, A]: F[A, (A, A)] = arr(x => (x, x))