ScalaMulti
ScalaMulti
Programming in Scala
Adapted from presentation by
H. C. Cunningham and J. C.
Church University of
Mississipi
What is Multiparadigm
Programming?
Definition:
A multiparadigm programming language
provides “a framework in which
programmers can work in a variety of
styles, freely intermixing constructs from
different paradigms.” [Tim Budd]
Programming paradigms:
imperative versus declarative (e.g., functional,
logic)
other dimensions – object-oriented, component-
oriented, concurrency-oriented, etc.
CS3180 (Prasad) ScalaMulti 2
Why Learn Multiparadigm
Programming?
Tim Budd:
“Research results from the psychology of
programming indicate that expertise in
programming is far more strongly related to
the number of different programming styles
understood by an individual than it is the
number of years of experience in
programming.”
over Java
Local type inference
Fewer errors
No Null Pointer problems
More flexibility
As many public classes per source file as you
want
Operator overloading
6
Scala References
Website http://www.scala-lang.org
• Martin Odersky. Scala Tutorial for Java
Programmers.
• Martin Odersky. Scala By Example.
Books on Scala:
http://www.scala-lang.org/node/959
CS3180 (Prasad) ScalaMulti 7
Scala object system
Class-based
Single inheritance
Can define singleton objects easily
(no need for static which is not
really OO)
Traits, compound types, and views
allow for more flexibility
8
Basic Scala
Use var to declare variables:
var x = 3;
x += 4;
Use val to declare values (final vars)
val y = 3;
y += 4; // error
Notice no types, but it is statically typed
var x = 3;
x = “hello world”; // error
Type annotations:
var x : Int = 3;
9
Functional Scala
Defining lambdas – nameless functions
val f = x :Int => x + 42; f : mapping :int ->
int
Closures! A way to haul around state
var y = 3;
val g = {x : Int => y += 1; x+y; }
Maps (and a cool way to do some functions)
List(1,2,3).map(_+10).foreach(println)
Filtering (and ranges!)
1 to 100 filter (_ % 7 == 3) foreach (println)
(Feels a bit like UNIX pipes?)
10
Defining Hello World
object HelloWorld {
def main(args: Array[String]){
println("Hey world!")
}
}
Singleton object named HelloWorld (also replaces
static methods and variables)
Method main defined (procedure)
Parameter args of type Array[String]
Array is generic class with type parameter
scala> Timer.main(null)
Welcome to CS3180!
Welcome to CS3180!
Welcome to CS3180!
…
CS3180 (Prasad) ScalaMulti 16
Anonymous Functions
object Timer {
def oncePerSecond(callback:() => Unit){
while (true) {
callback(); Thread sleep 1000
}
}
def main(args: Array[String]) {
oncePerSecond(
() => println("Welcome to CS3180!") )
}
}
scala> x.toString
res0: java.lang.String = 1.0-3.0i
Sum(Const(7),Var("y")))
val env: Environ =
{ case "x" => 5 case "y" => 7 }
println("Expression: " + exp)
println("Evaluation with x=5, y=7: " +
eval(exp,env))
println("Derivative relative to x:\n " +
derive(exp, "x"))
println("Derivative relative to y:\n " +
derive(exp, "y"))
}
}
CS3180 (Prasad) ScalaMulti 23
Execute Expression Trees
scala> :load Expressions.scala
Loading Expressions.scala...
…
scala> Expressions.main(null)
Expression:
Sum(Sum(Var(x),Var(x)),Sum(Const(7),Var(y)))
Evaluation with x=5, y=7: 24
Derivative relative to x:
Sum(Sum(Const(1),Const(1)),Sum(Const(0),Const(0)))
Derivative relative to y:
Sum(Sum(Const(0),Const(0)),Sum(Const(0),Const(1)))
scala> z(3)
res2: Int = 4
foldLeft, foldRight
reduceLeft, reduceRight
takeWhile, dropWhile
span, break
foreach
...
myActor ! myMessageObject
Thread.sleep(1000)
}
}
}
...
SillyActor.start() // Begins acting
I'm acting!
I caught a tomato!
I'm acting!
I caught a lettuce!
I'm acting!
I caught a tomato!
I'm acting!
I caught a lettuce!
I'm acting!
I caught a tomato!
I caught a lettuce!
I caught a tomato!
I caught a lettuce!
I caught a tomato!
I caught a lettuce!
CS3180 (Prasad) ScalaMulti 51