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.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) } |
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.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] |
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 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 |
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 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 |
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 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 |
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) |
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
// 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
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 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 { |