Skip to content

Instantly share code, notes, and snippets.

@retronym
retronym / generalised-type-constraints.scala
Created November 8, 2009 08:02
Demo of generalised type constraints in Scala 2.8
// scala 2.7 simple type constraint. This can only constrain a type parameter of this function.
// Below, in ListW.sumint28, we can't use this approach because we want to constrain T,
// a type param of the enclosing trait.
def sumint27A[T <: Int](l: List[T]) : Int = l.reduceLeft((a: Int, b: Int) => a + b)
trait IntLike[X] extends (X => Int)
object IntLike {
implicit val intIntLike: IntLike[Int] = new IntLike[Int] { def apply(x: Int) = identity(x) }
}
@retronym
retronym / type-bounds.scala
Created December 16, 2009 11:17
Tour of Scala Type Bounds
class A
class A2 extends A
class B
trait M[X]
//
// Upper Type Bound
//
def upperTypeBound[AA <: A](x: AA): A = x
/*
This fails to compile with:
type mismatch;
found : Iterable[T]
required: CC[T]
else if (predicate(xs.head)) xs.drop(1)
^
type mismatch;
found : Iterable[T]
@arnolddevos
arnolddevos / gist:574873
Created September 11, 2010 05:36
Idea for generator and suspendable wrapper
import scala.util.continuations._
class Generator[A] extends Iterator[A] with (A => Unit @ suspendable) {
private var a: Option[A] = None
private var k: Option[Unit => Unit] = None
def next = {
val a0 = a.get
val k0 = k.get
a = None
//scalac -P:continuations:enable
import scala.util.continuations._
import scala.collection._
object Generator {
type susp = cps[Any]
def make[A](body: (A => Unit @susp) => Unit @susp): Iterator[A] = new Iterator[A] {
val yields: A => Unit @susp = {v =>
shift{k: (Unit => Any) => (k, v):Any }
}
@javouhey
javouhey / sbt on cygwin
Created November 29, 2010 12:10
Getting sbt to work in cygwin 1.77
dir=`dirname $0`
stty -icanon min 1 -echo > /dev/null 2>&1
java -Djline.terminal=jline.UnixTerminal -Xmx512M -jar `cygpath -w $dir`/sbt-launch-0.7.4.jar "$@"
stty icanon echo > /dev/null 2>&1
@oxbowlakes
oxbowlakes / 3nightclubs.scala
Created May 13, 2011 15:14
A Tale of 3 Nightclubs
/**
* Part Zero : 10:15 Saturday Night
*
* (In which we will see how to let the type system help you handle failure)...
*
* First let's define a domain. (All the following requires scala 2.9.x and scalaz 6.0)
*/
import scalaz._
import Scalaz._
@milessabin
milessabin / gist:1164885
Created August 23, 2011 11:19
Boxed lazy values
// Is this even faintly novel? The closest I've seen is
//
// http://stackoverflow.com/questions/2618891/using-lazy-evaluation-functions-in-varargs
//
// which is a bit clunky by comparison. But this is so trivial someone must have
// done it this way before.
// UPDATE:
// Thanks to the Twittersphere (@etorreborre, @pchiusano and @loverdos) for a few sightings of related things,
//
@mslinn
mslinn / play
Created November 24, 2011 04:53
Play Framework 2.0 start script for Cygwin (should work on all *nix variants)
#! /usr/bin/env bash
control_c() {
if $cygwin; then rm "$USERPROFILE"/play.boot.properties; fi
exit 1
}
case "`uname`" in
CYGWIN*) cygwin=true;;
esac
@raichoo
raichoo / gist:2345414
Created April 9, 2012 18:50
Monads vs. implicit values in Scala
case class Config(host: String, port: Int) {
def prettyPrint(prefix: String, msg: String): String =
List(prefix, ": ", msg, " on ", host, ":", port.toString).mkString
}
/**
* Passing a configuration with implicits is like
* working in the state monad. You can "put" a
* new configuration even though we don't want that
* to happen in this particular example. We don't