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
import scalaz.{ Free, Coyoneda, Monad, ~>, State, NonEmptyList }
import scalaz.std.function._
import scalaz.syntax.monad._
import scalaz.effect.IO
import scala.util.Random
object freecoyo extends App {
// An algebra of primitive operations in the context of a random number generator
def mergeRanges(ns: List[Int]): List[Range] =
ns.foldRight(List.empty[Range]) {
case (n, r :: rs) if (n == r.min - 1) => (n to r.max) :: rs
case (n, rs) => (n to n) :: rs
}

doobie experiment - postgres NOTIFY as scalaz-stream

One of the cool things PostgreSQL gives you is a simple notification system that you can use to let clients know that something interesting has happened. For instance you can set up rules that broadcast notifications when a table is updated, and client applications can update displays in response. The JDBC driver provides access to this API so I thought I would see what it would look like in doobie.

The program below constructs a Process[ConnectionIO, PGNotification] that registers for events, polls for them periodically (this is the best we can do with current driver support), and unregisters when the stream terminates for any reason. We use a Transactor to replace ConnectionIO with Task which gives us something we can actually run.

package doobie.example

import doobie.imports._
@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