Skip to content

Instantly share code, notes, and snippets.

@slawosz
Created May 21, 2011 20:34
Show Gist options
  • Save slawosz/984872 to your computer and use it in GitHub Desktop.
Save slawosz/984872 to your computer and use it in GitHub Desktop.
Scala examples for studies presentation
var someAnonMethod = (s:String) => println(s)
// Unit is Scala void
def execute(paramMethod: (String) => Unit) {
println("do hard stuff")
paramMethod("our function")
println("close hard stuff")
}
def nonAnonMethod(s:String) {
println(s)
}
println("Call with anonMethod")
execute(someAnonMethod)
println("Call with nonAnonMethod")
execute(nonAnonMethod)
scala> import scala.collection.mutable.HashSet
import scala.collection.mutable.HashSet
scala> var hashSet:HashSet[Any] = new HashSet
hashSet: scala.collection.mutable.HashSet[Any] = Set()
scala> hashSet.+=("foo")
res0: scala.collection.mutable.HashSet[Any] = Set(foo)
scala> hashSet.+=(1)
res1: scala.collection.mutable.HashSet[Any] = Set(1, foo)
scala> hashSet.+=(4)
res2: scala.collection.mutable.HashSet[Any] = Set(1, 4, foo)
scala> var someXML = <lecture><name>Mas</name><dates><date>13 III</date><date test="true">23 III</date></dates></lecture>
someXML: scala.xml.Elem = <lecture><name>Mas</name><dates><date>13 III</date><date test="true">23 III</date></dates></lecture>
scala> someXML \ "dates"
res5: scala.xml.NodeSeq = NodeSeq(<dates><date>13 III</date><date test="true">23 III</date></dates>)
scala> someXML \ "dates" \\ "@test"
res7: scala.xml.NodeSeq = NodeSeq(true)
scala> val dates = someXML \ "dates"
dates: scala.xml.NodeSeq = NodeSeq(<dates><date>13 III</date><date test="true">23 III</date></dates>)
scala> val dates = someXML \ "dates" \ "date"
dates: scala.xml.NodeSeq = NodeSeq(<date>13 III</date>, <date test="true">23 III</date>)
scala> dates(0)
res14: scala.xml.Node = <date>13 III</date>
scala> dates(0).text
res16: String = 13 III
import scala.collection.mutable.HashSet
trait Observable {
private var observers:HashSet[Observer] = new HashSet
private var changed:Boolean = false
def addObserver(o:Observer) {
observers.+=(o)
}
def clearChanged() {
changed = false
}
def countObservers():Int = {
observers.size
}
def deleteObserver(o:Observer) {
observers.-=(o)
}
def deleteObservers() {
observers.clear()
}
def hasChanged():Boolean = {
changed
}
def notifyObservers(arg:Any) {
observers.foreach(o => o.update(this,arg))
}
def notifyObservers() {
notifyObservers(null)
}
def setChanged() {
changed = true
}
}
trait Observer {
def update(o:Observable, arg:Any)
}
class Employee extends Observable {
var salary = 100
def updateSalary(newSalary:Int) {
salary = newSalary
notifyObservers
}
}
class LoudEmployee extends Employee {
override def notifyObservers() {
println("I will notify my observers now!")
super.notifyObservers
}
}
class Accountant extends Observer {
def update(o:Observable, arg:Any) {
println("New tax will be applied")
}
}
class HR extends Observer {
// error: class HR needs to be abstract, since method
// update in trait Observer of type (o: this.Observable,arg: Any)Unit is not defined
def update(o:Observable, arg:Any) {
println("Employee data updated")
}
}
val employee = new Employee
val accountant = new Accountant
val hr = new HR
employee.addObserver(accountant)
employee.addObserver(hr)
employee.updateSalary(120)
println(employee.countObservers())
employee.deleteObserver(hr)
employee.updateSalary(120)
val loudEmployee = new LoudEmployee
loudEmployee.addObserver(accountant)
loudEmployee.addObserver(hr)
loudEmployee.updateSalary(150)
Call with anonMethod
do hard stuff
our function
close hard stuff
Call with nonAnonMethod
do hard stuff
our function
close hard stuff
Employee data updated
New tax will be applied
2
New tax will be applied
I will notify my observers now!
Employee data updated
New tax will be applied
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment