Skip to content

Instantly share code, notes, and snippets.

View robcd's full-sized avatar

Rob Dickens robcd

View GitHub Profile
@robcd
robcd / gist:3313553
Created August 10, 2012 11:19
Get vs State
// In the following code, Get is a type which has previously been called State - a name which
// the author found to be confusing, since instances aren't actually states!
//
// Instances are in fact functions which obtain (get) a value that depends on some state.
//
// The following code demonstrates using a Get to set as well as get!
//
// claim: even this is less confusing than using the name State!
//
// N.B. Here, 'State' is the type of the er, state.
@robcd
robcd / gist:2787106
Created May 25, 2012 10:08
Variation on sample program of SftI Ex 22.5, for future reference!
import scala.util.continuations._
object Main extends App {
val a = "Mary was a little lamb".split(" ")
type Next = (Boolean => String)
var next: Next = null
val firstResultOfShiftBlock = reset {
var i = 0
while (i < a.length) {
@robcd
robcd / gist:2507381
Created April 27, 2012 08:19
A solution to SftI Ex 21.3
trait Factorial {
def ! : BigInt
// ^ space required!
}
object Factorial {
implicit def Int2Factorial(n: Int): Factorial = new Factorial {
def ! = {
if (n < 0) throw new RuntimeException("unable to compute factorial of negative number")
@annotation.tailrec
@robcd
robcd / gist:2028683
Created March 13, 2012 13:13
A solution to SftI Ex 19.9
import util.parsing.combinator.RegexParsers
class RegexProgRunner extends RegexParsers {
val number = "-?[0-9]+".r
val varName = "[a-zA-Z_][a-zA-Z_0-9]*".r
var vars = Map[String, Double]()
def start: Parser[Any] = prog(true)
def prog(b: Boolean): Parser[Any] = rep1(statement(b))
@robcd
robcd / 3nightclubs.scala
Created September 26, 2011 08:29 — forked from oxbowlakes/3nightclubs.scala
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._
object Sobriety extends Enumeration {