This file contains 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 SUG_Monads { | |
trait Functor[F[_]] { | |
def map[A, B](fa: F[A])(f: A => B): F[B] | |
def distribute[A, B](fab: F[(A, B)]): (F[A], F[B]) = | |
(map(fab)(_._1), map(fab)(_._2)) | |
def codistribute[A, B](e: Either[F[A], F[B]]): F[Either[A, B]] = | |
e fold (map(_)(Left(_)), map(_)(Right(_))) |
This file contains 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 ch6_new_state { | |
object State { | |
def unit[S, A](a: A): State[S, A] = State(s => (a, s)) | |
def sequence[S, A](fs: List[State[S, A]]): State[S, List[A]] = | |
fs.foldRight(unit[S, List[A]](List[A]()))((x, y) => x.map2(y)(_ :: _)) | |
def get[S]: State[S, S] = State(s => (s, s)) |
This file contains 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
sealed trait Result | |
case object X extends Result | |
case object O extends Result | |
case object Tie extends Result | |
object Game { | |
type Player = (Result, Set[Int]) | |
private val emptySet: Set[Int] = Set() | |
private val availableSquares = (1 to 9).toSet |