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.State // the State[S,A] type itself | |
import scalaz.State._ // primitives like get and put are here | |
object StateExample extends App { | |
// A computation that produces a String | |
val action1: State[Int, String] = | |
for { | |
n <- get | |
_ <- put(n + 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
import scala.collection.convert.Wrappers.JMapWrapper | |
import java.util.Collections | |
object MapTest extends App { | |
val m = new JMapWrapper(Collections.synchronizedMap(new java.util.HashMap[String,String])) | |
new Thread { | |
override def run { | |
while (true) { |
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.io.Source | |
import System.{ currentTimeMillis => now } | |
object Inv extends App { | |
// An int set that knows how many members are greater than a given number. | |
sealed trait GSet { | |
def ngt(n: Int): Int // number greater than `n` | |
def +(n: Int): GSet | |
} |
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
object SJ extends App { | |
trait Getter[T] { | |
def get(s: String): T | |
} | |
object Getter { | |
implicit val getInt = new Getter[Int] { | |
def get(s: String) = s.toInt // unsafe |
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 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) |
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 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 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
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 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 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 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.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 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.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 |