This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ======== |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |