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
import scalaz._, Scalaz._ | |
case class Happy[A](a: A) | |
case class NewYear(year: Int) | |
implicit val happyInstance = new Applicative[Happy] { | |
def point[A](a: ⇒ A) = Happy(a) | |
def ap[A, B](fa: ⇒ Happy[A])(f: ⇒ Happy[A => B]) = | |
f match { | |
case Happy(f) ⇒ fa match { |
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
import Control.Applicative | |
data NewYear = NewYear Int | |
deriving (Eq, Show) | |
data Happy a = Happy a | |
deriving (Eq, Show) | |
instance Functor Happy where | |
fmap f (Happy a) = Happy $ f a |
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
case class Reader[T, +A](run: T ⇒ A) { | |
def map[B](f: A ⇒ B): Reader[T, B] = | |
Reader((r: T) ⇒ f(run(r))) | |
def flatMap[B](f: A ⇒ Reader[T, B]): Reader[T, B] = | |
Reader((r: T) ⇒ f(run(r)).run(r)) | |
def &&&[B](x: Reader[T, B]): Reader[T, (A, B)] = | |
for { | |
a ← this |
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
import scalaz._, Scalaz._ | |
case class Writer[W: Monoid, +A](run: (A, W)) { | |
def map[B](f: A ⇒ B): Writer[W, B] = | |
Writer { | |
val (a, w) = run | |
(f(a), w) | |
} | |
def flatMap[B](f: A ⇒ Writer[W, B]): Writer[W, B] = |
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
case class State[S, +A](run: S ⇒ (A, S)) { | |
def map[B](f: A => B): State[S, B] = | |
State(s ⇒ { | |
val (a, t) = run(s) | |
(f(a), t) | |
}) | |
def flatMap[B](f: A ⇒ State[S, B]): State[S, B] = | |
State(s ⇒ { | |
val (a, t) = run(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
case class T() // your data type that you want to "implicitly" thread through | |
case class TReader[+A](run: T ⇒ A) { | |
def map[B](f: A ⇒ B): TReader[B] = | |
TReader((r: T) ⇒ f(run(r))) | |
def flatMap[B](f: A ⇒ TReader[B]): TReader[B] = | |
TReader((r: T) ⇒ f(run(r)).run(r)) | |
def &&&[B](x: TReader[B]): TReader[(A, B)] = |
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
class Monad m => Ref r m where | |
ref :: a -> m (r a) | |
(*) :: r a -> m a | |
(*=) :: r a -> a -> m () | |
(*$) :: r a -> (a -> a) -> m () | |
r *$ f = (r *) >>= (*=) r . f | |
instance Ref IORef IO where | |
ref = newIORef | |
(*) = readIORef |
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
\documentclass{tufte-handout} | |
%\geometry{showframe}% for debugging purposes -- displays the margins | |
\usepackage{amsmath} | |
% Set up the images/graphics package | |
\usepackage{graphicx} | |
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio} | |
\graphicspath{{graphics/}} |
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
$ ghci | |
GHCi, version 7.4.2: http://www.haskell.org/ghc/ :? for help | |
Loading package ghc-prim ... linking ... done. | |
Loading package integer-gmp ... linking ... done. | |
Loading package base ... linking ... done. | |
Prelude> :l hello-ski.hs | |
[1 of 1] Compiling Main ( hello-ski.hs, interpreted ) | |
Ok, modules loaded: Main. | |
*Main> main | |
Hello world! |
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
> console | |
[info] Starting scala interpreter... | |
[info] | |
Welcome to Scala version 2.9.2 (OpenJDK 64-Bit Server VM, Java 1.6.0_24). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> import scala.xml._ | |
import scala.xml._ |