This file contains hidden or 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
| data Tree a = Empty | Node a (Tree a) (Tree a) deriving Show | |
| append :: Ord a => a -> Tree a -> Tree a | |
| append a Empty = (Node a Empty Empty) | |
| append a (Node v left right) | |
| | a > v = Node v left (append a right) | |
| | a < v = Node v (append a left) right | |
| | a == v = Node v left right | |
| nodes = foldr append Empty [8,6,4,1,7,3,1,5] |
This file contains hidden or 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
| trait Num[A] extends Ordering[A] { | |
| def plus(a:A,b:A):A | |
| } | |
| def plus[T: Num](a: T, b: T):T = implicitly[Num[T]].plus(a,b) | |
| def cmp[T:Ordering](a: T,b: T):Int = implicitly[Ordering[T]].compare(a,b) | |
| implicit object DoublePlus extends Num[Double] { | |
| override def plus(a:Double, b:Double) = a + b | |
| override def compare(a:Double, b:Double) = a.compare(b) |
This file contains hidden or 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
| trait Functor[F[_]] { | |
| def map[A, B](x: F[A])(f: A => B): F[B] | |
| } | |
| implicit def optionFunctor: Functor[Option] = new Functor[Option] { | |
| override def map [A, B](x: Option[A])(f: A => B): Option[B] = x map f | |
| } | |
| implicit def tuple2Functor[X] = new Functor[({type Alias[A] = Tuple2[X, A]})#Alias] { | |
| override def map[A, B](t: Tuple2[X, A])(f: A => B): Tuple2[X, B] = (t._1, f(t._2)) |
This file contains hidden or 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 play.api.libs.concurrent.Execution.Implicits.defaultContext | |
| import scala.concurrent.Future | |
| import scalaz._ | |
| import Scalaz._ | |
| type IntState[A] = State[Int, A] | |
| case class Song(name: String, durationInSeconds: Int) |
This file contains hidden or 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._ | |
| import scala.concurrent.Future | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| case class Order(id: Long, desc: String) | |
| case class Vendor(id: Long, name: String) | |
| def delay[A](f: => Future[A], duration: Long, message: String) = { | |
| println(message) |
This file contains hidden or 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._ | |
| import scala.language.higherKinds | |
| import scala.concurrent.Future | |
| import scala.concurrent.ExecutionContext.Implicits.global | |
| trait Session { | |
| def doSomething(): Unit | |
| } | |
This file contains hidden or 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._ | |
| import scala.concurrent.Future | |
| import scalaz.std.scalaFuture._ | |
| import play.api.libs.concurrent.Execution.Implicits._ | |
| val f1 = (s: String) => Option("foo").point[Future] | |
| def f2 = (s: String) => Option(1).point[Future] | |
| type OptionTFuture[T] = OptionT[Future, T] |
This file contains hidden or 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 iomonad.Pure | |
| import iomonad.Pure.IO | |
| val io = | |
| for { | |
| _ <- Pure.println("Starting work now.") | |
| // Do some pure work | |
| x = 1 + 2 + 3 | |
| _ <- Pure.println("All done. Home time.") | |
| } yield x |
This file contains hidden or 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 scala.annotation.tailrec | |
| import scala.math.Numeric.Implicits._ | |
| sealed trait Thunk[A] | |
| case class Done[A](x: A) extends Thunk[A] | |
| case class Cont[A](f: () => Thunk[A]) extends Thunk[A] | |
| def evenT[A: Numeric](i: A)(implicit f: Int => A): Thunk[Boolean] = if (i == 0) Done(true) else Cont(() => oddT(i - 1)) | |
| def oddT[A: Numeric](i: A)(implicit f: Int => A): Thunk[Boolean] = if (i == 0) Done(false) else Cont(() => evenT(i - 1)) |
This file contains hidden or 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 acme; | |
| public class Employee implements Comparable<Employee> { | |
| @Override | |
| public int compareTo(Employee o) { | |
| return 0; | |
| } | |
| } | |