Skip to content

Instantly share code, notes, and snippets.

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(_)))
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))
@chirlo
chirlo / Game.scala
Last active December 23, 2015 01:59
TicTacToe with Either
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