Skip to content

Instantly share code, notes, and snippets.

@stew
stew / gist:2310098
Created April 5, 2012 11:33
use state + traverse
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)
| }
@stew
stew / foo.scala
Created July 1, 2012 11:44
Reader Monad + Validation
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] = {
@stew
stew / curry.scala
Created August 31, 2012 02:53
example of currying
// 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))
}
@stew
stew / FutureInstances.scala
Created October 16, 2012 17:29
scalaz instances for an Akka Future
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
}
@stew
stew / fizzbuzz.scala
Created November 14, 2012 20:55
fizzbuzz in the scala type system
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
@stew
stew / gist:4122692
Created November 21, 2012 02:33
output of javap -p *.class for case class Square
$ 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();
@stew
stew / kleisliexample.scala
Created January 16, 2013 14:07
example of kleisli in kleisli
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]
@stew
stew / Uncozip.scala
Created March 9, 2013 05:04
uncozip
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 {
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] = ???
}
@stew
stew / MonadTFromTransformer.scala
Last active December 17, 2015 19:49
You can create a generic monad transformer as long as the "inner" monad has a Traverse instance
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]]