Skip to content

Instantly share code, notes, and snippets.

View machisuji's full-sized avatar

Markus Kahl machisuji

  • OpenProject GmbH
  • Cardiff, Wales, UK
View GitHub Profile
@machisuji
machisuji / desired.scala
Created February 2, 2012 15:39
case and call-by-name?
receive("hallo") {
case "hallo" => "selber" // selber should not be evaluated at this point
} // result Some(selber)
scala> def translate(coord: (Int, Int), str: String) = (coord._1 + 1, coord._2 - 1) -> str
translate: (coord: (Int, Int), str: String)((Int, Int), String)
scala> (1, 1) -> "test" :: Nil map translate
<console>:9: error: type mismatch;
found : ((Int, Int), String) => ((Int, Int), String)
required: ((Int, Int), java.lang.String) => ?
(1, 1) -> "test" :: Nil map translate
... problem.scala:6: type mismatch;
[error] found : ((Int, Int), scawtor.serial.Cell) => Boolean
[error] required: ((Int, Int), scawtor.serial.Cell) => Boolean
[error] def neighboursDo(x: Int, y: Int)(fun: ((Int, Int), Cell) => Boolean): Unit = random.shuffle(neighboursFor(x, y)).exists(fun)
@machisuji
machisuji / wubs.scala
Created January 17, 2012 13:05
I take every opportunity to avoid actual work.
trait Wubs {
def WUB(wub: Wubs = this) = this
def W(wub: Wubs = this) = this
def WEEOOOOO(wub: Wubs = this) = this
def -(wub: Wubs = this) = this
}
object WUB extends Wubs
object W extends Wubs
WUB WUB WUB WEEOOOOO W-W-W-W-WUB WUB WUB
def seqPlusHead[S <: Seq[T], T](seq: S): (S, T) = seq -> seq.head
seqPlusHead[List[String], String](List("Hallo", "Welt"))
// -> (List[String], String) = (List(Hallo, Welt),Hallo)
def onlySeqPlusHead[Seq[T], T](seq: Seq[T]) = seqPlusHead[Seq[T], T](seq)
// error: type arguments [Seq[T],T] do not conform to method seqPlusHead's type parameter bounds [S <: Seq[T],T]
// def onlySeqPlusHead[Seq[T], T](seq: Seq[T]) = seqPlusHead[Seq[T], T](seq)
@machisuji
machisuji / seqReader.scala
Created January 11, 2012 13:03
A SequenceReader should enable reading sequences of elements who are readable themselves.
implicit def seqReader[Seq[T], T](implicit reader: TwpReadable[T]): TwpReadable[Seq[T]] =
new SequenceReader[Seq[T], T] {
def map(in: Input) = reader read in
def canMap(in: Input) = reader.isDefinedAt(in)
}
@machisuji
machisuji / Unions.scala
Created January 5, 2012 10:10
My personal Scala lesson of the day - Unions
describe("Unions") {
trait TermUnion[T]
trait Expression { /* ... */ }
object TermUnion {
implicit object DoubleWitness extends Union[Double]
implicit object ExpressionWitness extends Union[Expression]
}
/** Only accepts union types Double and Expression. */
[33.37] failure: identifier expected
any defined by operation parameters;
^
trait MessageCompanion[S <: Message, T] /* ... */ {
def unapply(in: Input): Option[T] = {
if (isDefinedAt(in)) {
val result = Some(read(in))
checkComplete(in)
result
} else None
}
def read(in: Input): T
@machisuji
machisuji / changed.scala
Created December 12, 2011 13:39
I don't want to write Changed.tag.msg myself!
type Path = Seq[String]
class Changed(val path: Path, val fileName: String) extends Message {
def write = Changed.tag.msg #:: path #:: fileName #:: Output
}
object Changed extends MessageCompanion[(Path, String)] {
def tag = 0
def apply(path: Path, fileName: String) = new Changed(path, fileName)
def read(implicit in: Input) = (sequence[Path], string)