Skip to content

Instantly share code, notes, and snippets.

View yasuabe's full-sized avatar

Yasuyuki Abe yasuabe

View GitHub Profile
@yasuabe
yasuabe / Chapter14.scala
Created December 15, 2017 19:13
ch14 change
package fp_tdd
import cats.Apply
import cats.syntax.apply._
import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen, Properties}
object Chapter14 extends Properties("Ch14") {
// ======== TODO ========
// $5 + 10 CHF = $10 if rate is 2:1
@yasuabe
yasuabe / Chapter16.scala
Last active December 15, 2017 21:59
ch15 mixed concurrencies - ch16 abstraction finally
package fp_tdd
import cats.Apply
import cats.syntax.apply._
import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen, Properties}
object Chapter16 extends Properties("ch16") {
// ======== TODO ========
// ======== DONE ========
@yasuabe
yasuabe / RegisterAFP.sc
Created December 19, 2017 06:19
by writer monad
import cats.data.Writer
import cats.instances.vector._
import cats.syntax.writer._
case class CashRegister(total: Int) {
def addCash(toAdd: Int) = CashRegister(total + toAdd)
}
type Purchase = CashRegister => CashRegister
def makePurchase(amount: Int): Purchase = (r: CashRegister) => {
println(s"Purchase in amount: $amount")
@yasuabe
yasuabe / RegisterAFP2.sc
Created December 19, 2017 06:22
by state monad
import cats.data.State
case class CashRegister(total: Int) {
def addCash(toAdd: Int) = CashRegister(total + toAdd)
}
type Purchase = CashRegister => CashRegister
def makePurchase(amount: Int): Purchase = (r: CashRegister) => {
println("Purchase in amount: " + amount)
r addCash amount
}
@yasuabe
yasuabe / RegisterAFP3.sc
Created December 19, 2017 06:23
by eff monad
import cats.data.{State, Writer}
import org.atnos.eff._
import org.atnos.eff.state._
import org.atnos.eff.writer._
import org.atnos.eff.syntax.all._
case class CashRegister(total: Int) {
def addCash(toAdd: Int) = CashRegister(total + toAdd)
}
type Purchase = CashRegister => CashRegister
@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 {
@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_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 / 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 / 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)