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
scala> import scalaz._ | |
import scalaz._ | |
scala> import Scalaz._ | |
import Scalaz._ | |
scala> case class FooState(partialResult: List[Int], result: List[List[Int]]) { | |
| def newPartial(n:Int) = FooState(n::partialResult, result) | |
| def newResult = FooState(List(), partialResult.reverse :: result) | |
| } |
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._ | |
import Scalaz._ | |
object Foo { | |
type Result[A] = Validation[String,A] | |
type Reader[A] = Int => Result[A] | |
implicit val readerBind: Bind[Reader] = new Bind[Reader] { | |
def map[A,B](fa: Reader[A])(f: A=>B) : Reader[B] = x => fa(x) map f | |
def bind[A,B](fa: Reader[A])(f: A => Reader[B]) : Reader[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
// you have items | |
case class Item(name: String, price: Double) | |
// their prices can be adjusted. To adjust a price, pass a function | |
// which takes the old price, and gives a new price | |
def adjustPrice(i: Item, discountFunction: Double => Double) = { | |
// return a new copy with the price changed | |
i.copy(price = discountFunction(i.price)) | |
} |
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 akka.dispatch.ExecutionContext | |
import akka.dispatch.Future | |
import scalaz._ | |
import Scalaz._ | |
object FutureInstances { | |
implicit val futureFunctor : Functor[Future] = new Functor[Future] { | |
override def map[A,B](fa: Future[A])(f: A=>B) = fa map f | |
} |
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
package fizzbuzz | |
// This is church encoding of natural numbers | |
sealed trait Num { | |
def toInt : Int | |
override def toString = toInt.toString | |
} | |
final case object Z extends Num { | |
def toInt : Int = 0 |
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
$ javap -p *.class | |
Compiled from "foo.scala" | |
public class Square implements scala.Product,scala.Serializable { | |
private final int width; | |
private final java.lang.String name; | |
public static final <A extends java/lang/Object> scala.Function1<java.lang.Object, A> andThen(scala.Function1<Square, A>); | |
public static final <A extends java/lang/Object> scala.Function1<A, Square> compose(scala.Function1<A, java.lang.Object>); | |
public scala.collection.Iterator<java.lang.Object> productIterator(); | |
public scala.collection.Iterator<java.lang.Object> productElements(); | |
public int width(); |
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._ | |
import Scalaz._ | |
case class Person(name: String, score: Int) | |
object Main extends App { | |
type Environment = Map[String, Person] | |
type EnvReader[+A] = Kleisli[Option,Environment,A] | |
type UserReader[+A] = Kleisli[EnvReader,String,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
object Uncozip { | |
implicit val wtf = language.higherKinds | |
trait Sum2[F[_, _]] { | |
def cata[A, B, X](a: A ⇒ X, b: B ⇒ X)(fab: F[A, B]): X | |
} | |
implicit val sumEither: Sum2[Either] = new Sum2[Either] { | |
def cata[A, B, X](a: A ⇒ X, b: B ⇒ X)(fab: Either[A, B]): X = { | |
fab 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
class OptionMonad extends Monad[Option] { | |
def point[A](a: A): Option[A] = ??? | |
def map[A,B](fa: Option[A], f: A=>B) : Option[B] = ??? | |
def flatMap[A,B](fa: Option[A]: f: A=>Option[B]): Option[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
import scalaz._ | |
import Scalaz._ | |
object MonadT { | |
implicit def monadTransformerFromTraverse[M[_]: Monad, N[_]: Monad: Traverse]: Monad[({type MN[A]=M[N[A]]})#MN] = new Monad[({type MN[A]=M[N[A]]})#MN] { | |
def point[A](a: => A): M[N[A]] = a.point[N].point[M] | |
def bind[A,B](fa: M[N[A]])(f: A=>M[N[B]]) : M[N[B]] = { | |
val M = implicitly[Monad[M]] | |
val NT = implicitly[Traverse[N]] | |
val N = implicitly[Monad[N]] |
OlderNewer