Skip to content

Instantly share code, notes, and snippets.

class OrderedSequence[A: Order](v: FingerTree[A => Boolean, A]) extends NewType[FingerTree[A => Boolean, A]] {
val value = v
def +(a: A) = {
val spl = v.split(x => x(a))
new OrderedSequence(spl._1 <++> (a <+: spl._2))
}
override def toString = v.toString
}
object OrderedSequence {
@runarorama
runarorama / higher-rank polymorphic function with type bounds.scala
Created August 11, 2011 14:55 — forked from mads-hartmann/higher-rank polymorphic function with type bounds.scala
Attempt to create a higher-rank polymorphic function with type bounds
/*
I want to use a higher-rank polymorphic function when transforming an AST to generalize the
'traversal' so it can be separated from the actual transformation of each node.
This snippet of code doesn't quite capture my use case but it provokes the same compile error
as I get here: https://gist.github.com/1139579
*/
trait ~>[F[_],G[_],C[_]] {
def apply[A](a: F[A])(implicit evidence: C[F[A]]): G[A]
@runarorama
runarorama / gist:1140578
Created August 11, 2011 19:51
ADTs in Scala
trait Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B): B
}
object Just {
def apply[A](a: => A): Maybe[A] = new Maybe[A] {
def apply[B](just: (=> A) => B, nothing: => B) = just(a)
}
def unapply[A](a: Maybe[A]): Option[A] = a(Some(_), None)
}
@runarorama
runarorama / DBSkeisli.scala
Created October 17, 2011 04:54 — forked from corruptmemory/DBSkeisli.scala
Silly example of Kleisli composition of DB operations
/**
* A silly example using Kleisli composition of DB operations
* Based on an idea from Runar Bjarnason found here:
* https://groups.google.com/d/msg/scala-debate/xYlUlQAnkmE/FteqYKgo2zUJ
*
* Uses Scalaz7
*
* @author <a href="mailto:jim@corruptmemory.com">Jim Powers</a>
*/
object Monadic {
@runarorama
runarorama / gist:1292001
Created October 17, 2011 05:39
Regional database connections
object Regional {
case class IOM[M, A](unwrap: IO[A])
case class Q[M](c: Connection)
def iomMonad[M]: Monad[({type λ[α] = IOM[M, α]})#λ] =
new Monad[({type f[a] = IOM[M, a]})#f] {
def pure[A](a: => A) = IOM[M, A](a.point[IO])
def bind[A, B](m: IOM[M, A], f: A => IOM[M, B]) =
IOM(m.unwrap >>= (x => f(x).unwrap))
def lineToOffer(line: String, headers: List[String]) : Validation[NonEmptyList[String], Offer] = {
({commaSplit(_)} andThen {extractFields(_: List[String], headers)} apply line) :->
{(v) => Offer(v._1, v._2, v._3, v._4)}
}
def commaSplit(l: String): String => List[String] = l.split(",").toList
def extractFields(x: List[String], headers: List[String]): Validation[NonEmptyList[String], (String, String, String, String)] = {
notEmpty(x(headers.indexOf("offer_name")), "No offer name specified on line:" + x.mkString(","))) <|***|> (
notEmpty(x(headers.indexOf("email")), "No email specified on line:" + x.mkString(",")),

== Jumping on Trampolines ==

Trampolining is a practical everyday programming technique where a program is organized into discrete steps that can be executed independently. This can be used to avoid StackOverflowErrors in recursive programs, for stepping through code and adding breakpoints (without an IDE), cooperative multitasking, and even limited detection of infinite loops. We will discuss how to extend this technique for turning any call whatsoever into a tail call, and how to generalize trampolines to yield advanced techniques "for free"--Techniques like coroutines, "iteratees", polymorphic effect systems, and a solution to the infamous Expression Problem.

== Simulating Effect Polymorphism With Higher-Kinded Types ==

An effect system helps us specify the side-effects of a program in a machine-verifiable way. We discuss a straightforward method of using Scala's higher-kinded types to write effect specifications. Methods can be polymorphic in the effect they may have, and the types can specify "regio

object Util {
def BenchmarkCycles = 100000000
def time[Z](label: String)(f: => Z): Z = {
val startTime = System.currentTimeMillis
val result = f
val endTime = System.currentTimeMillis
object ScalazActorTest extends App {
import scalaz._
import Scalaz._
val running = new AtomicBoolean(false)
object Actor {
var count = 0
def act() {
val b = running.get()
@runarorama
runarorama / .zshrc
Created January 19, 2012 18:28
My .zshrc file
# ALIASES
# ls
alias ls='ls --color=auto'
alias ll='ls --color=auto -lh'
alias lll='ls --color=auto -lh | less'
# grep
export GREP_COLOR=31
alias grep='grep --color=auto'