Skip to content

Instantly share code, notes, and snippets.

View razie's full-sized avatar

Razvan Cojocaru razie

View GitHub Profile
@razie
razie / tc.scala
Created September 15, 2011 20:09
type classes
//============== the Type Class version
trait Eq[A] { // the type trait
def areTheSame(a: A, b: A): Boolean
}
case class Student(name: String) // better decoupling - extends nothing
object EqImplementations { // the implementations for different classes
implicit object EqStudent extends Eq[Student] {
@razie
razie / tcoo.scala
Created September 15, 2011 20:05
OO type class
//============== the OO version
trait Eq[A] { // the type trait
def sameAs(b:A) : Boolean // so many name clashes with eq/equals etc
}
case class Student (name:String) extends Eq[Student] { // classic OO implementation
override def sameAs(b:Student) = this.name == b.name
}
@razie
razie / nulloption.scala
Created August 23, 2011 14:42
When Option is null...
scala> class A { val x = Some(1); println (x) }
defined class A
scala> class B extends A { override val x = Some(2); println(x) }
defined class B
scala> new B
null
Some(2)
res1: B = B@48b89bc5
@razie
razie / oomonad.scala
Created July 26, 2011 16:10
OO friendly monad
//Tony's monad
trait Monad[F[_]] {
def point[A](a: => A): F[A]
def bind[A, B](f: A => F[B]): F[A] => F[B]
}
//a Scala and OO-friendly monad
trait ScalaMonad[A] {
def flatMap[B](f: A => ScalaMonad[B]): ScalaMonad[B] // checkout the return value compared to the bind()
def map[B](f: A => B): ScalaMonad[B]
def wpar6 = """
par {
seq {
inc
log($0)
}
seq {
inc
log($0)
}
import razie.wfs._
val workflow = wfs strict seq {
par {
seq {
println ("he he - definition time")
later { _ + "runtime-a" }
}
later { _ + "runtime-b" }
}
sort[String] (_ < _)
v(c) (c ? P | c ! Q)
@razie
razie / gist:1070245
Created July 7, 2011 18:50
AttrAccess#1
/** simple Java access interface - needs sync-ing with some Java typical interfaces, probably Properties */
trait JavaAttrAccess {
/** set the value of the named attribute + the name can be of the form name:type */
def setValue(name: String, value: AnyRef, t: AttrType): Unit
def setValue(name: String, value: AnyRef): Unit
def getValue(name: String): AnyRef
def getType(name: String): AttrType
}
/** simple name-value pairs with optional type */
@razie
razie / CmdOptions.scala
Created January 21, 2011 22:44
scala dsl sample: realistic -options
package razie.learn.dsl.fs1
case class Flag (val c:Char)
object r extends Flag('r')
object f extends Flag('f')
class Cmd_rm (var flags:List[Flag]) {
def - (f:Flag) = { flags = f :: flags; this }
def apply (s:String) = "removing " + s
}
v(c) (c ? P | c ! Q)