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
//crash-course into scala: select the following defs and issue "run selection" | |
class Num (val i:Int) { | |
def + (other:Num) = new Num (this.i + other.i) // operators | |
override def toString : String = "Numero " + i.toString // inherit Java methods | |
override def equals (other:Any) = other match { // or override them | |
case n:Num => n.i == i | |
case _ => false // wildcard serves like a default case | |
} | |
} |
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
//============== 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] { |
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
//============== 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 | |
} | |
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> 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 |
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
//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] |
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
def wpar6 = """ | |
par { | |
seq { | |
inc | |
log($0) | |
} | |
seq { | |
inc | |
log($0) | |
} |
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 razie.wfs._ | |
val workflow = wfs strict seq { | |
par { | |
seq { | |
println ("he he - definition time") | |
later { _ + "runtime-a" } | |
} | |
later { _ + "runtime-b" } | |
} | |
sort[String] (_ < _) |
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
v(c) (c ? P | c ! Q) |
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
/** 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 */ |
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
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 | |
} |