Skip to content

Instantly share code, notes, and snippets.

View yasuabe's full-sized avatar

Yasuyuki Abe yasuabe

View GitHub Profile
@yasuabe
yasuabe / arrow_tutorial03.sc
Created December 30, 2017 15:59
arrow tutorial '3. some arrow operations'
import cats.arrow.Arrow
sealed case class SimpleFunc[A, B](runF: A => B)
trait SimpleFuncInstances {
implicit val simpleFuncArrow: Arrow[SimpleFunc] = new Arrow[SimpleFunc] {
def lift[A, B](f: A => B): SimpleFunc[A, B] = SimpleFunc(f)
def first[A, B, C](fa: SimpleFunc[A, B]): SimpleFunc[(A, C), (B, C)] =
SimpleFunc { case (a, c) => (fa runF a , c) }
@yasuabe
yasuabe / arrow_tutorial01.sc
Last active December 30, 2017 16:04
arrow tutorial '01 the arrow'
import cats.arrow.Arrow
import cats.data.{Cokleisli, NonEmptyList}
import cats.instances.function._
import cats.syntax.compose._
val f1 = Arrow[Function].lift(1d / (_: Int))
f1(10)
val A = Arrow[Function]
@yasuabe
yasuabe / Rational.scala
Last active December 26, 2017 06:05
product_code(redefine+cats+scalacheck)
package ratio1
import cats.Eq
import eu.timepit.refined.refineV
import eu.timepit.refined.api.Refined
import eu.timepit.refined.boolean.Not
import eu.timepit.refined.generic.Equal
import eu.timepit.refined.{W, refineMV}
import shapeless.Nat._0
@yasuabe
yasuabe / RationalSpec.scala
Last active December 26, 2017 06:04
property(redefine+cats+scalacheck)
package ratio1
import cats.Apply
import cats.instances.int._
import cats.syntax.all._
import eu.timepit.refined.cats._
import eu.timepit.refined.refineMV
import eu.timepit.refined.scalacheck.numeric.chooseRefinedNum
import org.scalacheck.Gen._
import org.scalacheck.Prop.forAll
@yasuabe
yasuabe / ex01_ProbMonadSpec.scala
Last active February 7, 2019 05:17
discipline for Prob Monad
package ex01
import cats.Eq
import cats.laws.discipline.MonadTests
import ex01.Prob.Event
import ex01.ProbInstances._
import org.scalacheck.{Gen, _}
import org.scalatest.FunSuite
import org.typelevel.discipline.scalatest.Discipline
@yasuabe
yasuabe / ex01_Prob.scala
Created December 23, 2017 18:12
Scala version of Prob Monad (from LYAHFGG)
object ProbInstances {
implicit def probMonad: Monad[Prob] = new Monad[Prob] {
def flatMap[A, B](pa: Prob[A])(f: A => Prob[B]): Prob[B] = pa flatMap f
def tailRecM[A, B](a: A)(f: A => Prob[Either[A, B]]): Prob[B] = {
val buf = List.newBuilder[(B, Rational)]
@tailrec def go(pes: List[(Prob[Either[A, B]], Rational)]): Unit = pes match {
case (Prob(e :: es), r0) :: tail => e match {
case (Right(b), r) => buf += (b -> r * r0) ; go(Prob(es) -> r0 :: tail)
case (Left(a2), r) => go(f(a2) -> r :: Prob(es) -> r :: tail)
@yasuabe
yasuabe / ex01_package.scala
Created December 23, 2017 17:19
(A,A) =f=> (R,R) where f:A=>R
package object ex01 {
implicit class T2mapper[A](val t: (A, A)) extends AnyVal {
def map[R](f: A => R): (R, R) = (f(t._1), f(t._2))
def foldMap[R, B](f: A => R, g: (R, R) => B): B = g.tupled(t map f)
}
}
@yasuabe
yasuabe / RegisterAFP2_Akka.sc
Created December 19, 2017 06:26
by akka typed
// program 中のregisterの持ち回りを避ける
import akka.typed._
import akka.typed.scaladsl.Actor
import akka.typed.scaladsl.Actor.immutable
import cats.data.{ReaderT, WriterT}
import cats.effect.IO
import cats.instances.vector._
trait Command
@yasuabe
yasuabe / RegisterAFP2_Free.sc
Created December 19, 2017 06:25
by free monad
import cats.data.StateT
import cats.effect.IO
import cats.free.Free
import cats.free.Free.liftF
import cats.{Id, ~>}
import cats.instances.vector._
import cats.syntax.foldable._
sealed trait Console[A]
case class PrintLn(s: String) extends Console[Unit]
@yasuabe
yasuabe / RegisterAFP2_IO.sc
Created December 19, 2017 06:25
by io monad
// program 中のregisterの持ち回りを避ける
import cats.data.StateT
import cats.effect.IO
case class CashRegister(total: Int) {
def addCash(toAdd: Int) = CashRegister(total + toAdd)
}
type Purchase = CashRegister => IO[CashRegister]
def makePurchase(amount: Int): Purchase = (r: CashRegister) => for {