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:4537247
Created January 15, 2013 08:37
Equivalent of https://gist.github.com/4507698 using scalaz7, modulo some renaming and unwrapping.
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]
@tpolecat
tpolecat / gist:4639981
Created January 26, 2013 03:40
i have been playing around with defining the monad for State[S,_] entirely ad-hoc, defining an alias State[S,A] => S => (S,A) but it may not be possible because the monad is defined for a lambda type and i can't figure out how to pimp a lambda type to add implicits in a way that the compiler will actually pick up and use
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]
}
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)
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) =
@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
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
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
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))
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
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)