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
val theRange = 1 to 100 | |
def mapper(x:Int):String = { | |
require(x>0) | |
(x % 3, x % 5) match { | |
case (0,0) => "FizzBuzz" | |
case (0,_) => "Fizz" | |
case (_,0) => "Buzz" | |
case _ => x.toString | |
} | |
} |
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
"99 bottles of beer" matches "(\\d+) bottles of beer" | |
"cab" matches "(?<!a)b" | |
"b" matches "(?<!a)b" | |
"cab" matches "(?<!a)b" | |
"bed" matches "(?<!a)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
trait Randoms { | |
import scala.util.Random | |
private val rand = new Random | |
implicit def seq2RichRandom[K](seq: Seq[K]) = new RichRandom(seq) | |
protected class RichRandom[K](seq: Seq[K]) { | |
def random: K = seq((rand.nextFloat * seq.length).toInt) | |
} | |
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
object Alea{ | |
private val rnd = new scala.util.Random | |
/* Throw a die | |
@return Int in range 1 to 6 */ | |
def akta = rnd.nextInt(6) + 1 | |
/* Throw a die | |
@param number number of die | |
@return Int sum of ints */ |
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
class HeartMonitor(millisUntilDeclaredDead: Long) extends Actor with FSM[Health, Long] { | |
import System.{currentTimeMillis => now} | |
val nextTest = 1000L | |
notifying { | |
case Transition(Stale, Alive) => | |
log.info("HeartMonitor received initial heartbeat") | |
case Transition(Dead, Alive) => | |
log.info("HeartMonitor noticed we are back alive again") | |
case Transition(_, Dead) => |
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 SqlParser extends scala.util.parsing.combinator.RegexParsers { | |
// ignore ordinary whitespace, line comments, and inline comments between combinators | |
override val whiteSpace = "(?sm)(\\s*(?:--.*?$|/\\*((?!\\*/).)*\\*/)\\s*|\\s+)+".r | |
def sqlStatement: Parser[Statement] = | |
opt(whiteSpace) ~> positioned( procedureCallStatement | |
| insertStatementCustom | |
| insertStatement | |
| updateStatement |
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
scala> // Problem: Some java apis return nulls. | |
scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME") | |
res0: java.lang.String = null | |
scala> System.getenv("PROBABLY_NOT_DEFINED_RUBISH_NAME").split(",") | |
java.lang.NullPointerException | |
at .<init>(<console>:7) | |
at .<clinit>(<console>) | |
at RequestResult$.<init>(<console>:9) |
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
sealed abstract class Tree[A] | |
case class Leaf[A](elem : A) extends Tree[A] | |
case class Node[A](left : Tree[A], right : Tree[A]) extends Tree[A] | |
val t = Node(Leaf(11), Node(Leaf(13),Leaf(15) )) | |
def sum(t: Tree[Int]):Int = t match { | |
case Leaf(i) => i | |
case Node(l,r) => sum(l) + sum(r) | |
case _ => error("FUBAR tree") |
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
case class Person(last: String, first: String) | |
val ps = List(Person("Smith", "Bob"), Person("Smith", "Bill")) | |
val psS = ps.sortBy(p => (p.last, p.first)) | |
implicit val o = Ordering.by((p: Person) => (p.last, p.first)) | |
ps.sorted | |
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
scala> trait Baz | |
defined trait Baz | |
scala> class Foo[Coll[_ <: Baz]] | |
defined class Foo | |
scala> class Bar[A <: Baz] | |
defined class Bar | |
scala> type Test = Foo[Bar] ; |
OlderNewer