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 scala.language.higherKinds | |
import scala.annotation.tailrec | |
import scalaz._ | |
import scalaz.Free._ | |
sealed trait MapOp[+A] | |
case class Put[A](k: String, v: String, q: Option[String] => A) extends MapOp[A] | |
case class Get[A](k: String, q: Option[String] => A) extends MapOp[A] | |
case class Del[A](k: String, q: Option[String] => A) extends MapOp[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
import language.higherKinds | |
object Stuff { | |
trait Monad[M[_]] { | |
def map[A, B](ma: M[A], f: A => B): M[B] | |
def bind[A, B](m: M[A], f: A => M[B]): M[B] | |
def point[A](a: A): M[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
def unfold[A](a: A)(f: A => Option[A]): Stream[A] = | |
f(a).map(a => a #:: unfold(a)(f)).getOrElse(Stream.empty) | |
def hailstone(i: Int) = i #:: unfold(i) { n => | |
if (n == 1) None else Some(if (n % 2 == 0) n / 2 else n * 3 + 1) | |
} | |
def main(args: Array[String]) { | |
println(hailstone(27).toList) | |
val (n, len) = (1 to 100000).map(n => (n, hailstone(n).length)).maxBy(_._2) |
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 tiny.env | |
object FoldStuff extends App { | |
trait MyList[A] { outer => | |
def fold[B](k: Option[(A, B)] => B): B | |
def ::(a: A) = new MyList[A] { | |
def fold[B](k: Option[(A, B)] => 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.Free._ | |
import scalaz.std.function._ | |
// IO-like effect world; like State[S,A] where S is private to the implementation | |
trait Effects[World] { | |
final class Action[+A] private[Effects] (private val t: World => Trampoline[(World, A)]) extends (World => A) { | |
def apply(w: World): A = run(w)._2 | |
def run(w: World): (World, A) = t(w).run |
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.effect._ | |
import scalaz.effect.Effect._ | |
import scalaz.effect.IO._ | |
import scalaz.syntax.monad._ | |
import ST._ | |
object StTest extends App { | |
// ST actions must be parameterized by their thread "S", which always remains free |
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 language.higherKinds | |
import java.sql.Connection | |
import scalaz.{ Monad, StateT, Functor, State } | |
import scalaz.State._ | |
import scalaz.MonadState._ | |
import scalaz.std.tuple._ | |
import scalaz.syntax.monad._ | |
import scalaz.syntax.std.tuple._ | |
import scalaz.effect.IO |
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 Box[A] { | |
private var waiters = Queue[A => Unit]() | |
def watch(f:A => Unit) = waiters = waiters :+ f | |
def put(a:A) = waiters.foreach(_(a)) | |
} | |
val b = new Box[String] | |
for (i <- 1 to 5) | |
b.watch(s => println(s + " " + i)) |
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
trait X | |
trait Y | |
implicit val x: X = ??? | |
def foo(implicit x: X) = ??? | |
def foo(implicit y: Y) = ??? | |
foo // ambiguous (but it's not because there's no implicit Y |
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 examples | |
object SJ extends App { | |
trait Getter[T] { | |
def get(s: String): Option[T] | |
} | |
def get[T: Getter](s: String, default: T): T = | |
implicitly[Getter[T]].get(s).getOrElse(default) |
OlderNewer