Skip to content

Instantly share code, notes, and snippets.

View tpolecat's full-sized avatar
🔥
Dijkstra would not have liked this.

Rob Norris tpolecat

🔥
Dijkstra would not have liked this.
View GitHub Profile
@tpolecat
tpolecat / gist:4771973
Created February 12, 2013 18:15
Example usage of scalaz7's State monad.
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)
@tpolecat
tpolecat / gist:4755770
Created February 11, 2013 17:05
Map wrapper can easily return Some(null)
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) {
@tpolecat
tpolecat / gist:4743224
Last active December 12, 2015 08:18
Inversions exercise from fayimora
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
}
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
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)
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
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))
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
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
@tpolecat
tpolecat / gist:4660668
Last active December 11, 2015 21:09
A way to construct effect environments with private state (potentially impure) and primitive operations.
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