Skip to content

Instantly share code, notes, and snippets.

View oluies's full-sized avatar

Örjan Angré (Lundberg) oluies

  • Sweden
  • 06:23 (UTC +02:00)
  • X @oluies
View GitHub Profile
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
}
}
"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"
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)
}
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 */
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) =>
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
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)
@oluies
oluies / stree.scala
Created February 17, 2011 20:19
stree
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")
@oluies
oluies / gist:842870
Created February 24, 2011 20:57
-< Jason Zaugg
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
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] ;