This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { |