§It was released publicly on the Java platform in January 2004, and on the .NET platform in June the same year. A second version of the language was released in March 2006.
§General purpose
§Express common programming patterns in a concise, elegant, and type-safe way
§Stands for “scalable language.”
–Designed to grow with the demands of its users.
–Wide range of programming tasks, from small scripts to building large systems.
§Smoothly integrates features of object-oriented and functional languages.
§It is both a scripting language and a compiled one.
§Imutability and lack of side effects are key to distributed paralleism which are features of functional languages – a modern issue facing IT today with multi core process.
–Don’t need surrounding braces as it’s a single statement
Recall – Java Case statements
// checks if a number between 1 and 10 is prime
int number = 4;
switch(number){
case 1:returntrue;
case 2:returntrue;
case 3:returntrue;
case 5:returntrue;
case 7:returntrue;
default:returnfalse;
}
Recall – Java Case statements
§If each case statement doesn’t return, need to also include break statements to prevent unwanted fall through
switch(number){
case 1:object.operation1(number);
break;
case 2:object.operation2(number);
break;
case 3:object.operation3(number);
break;
case 5:object.operation4(number);
break;
case 7:object.operation5(number);
break;
default:object.defaultOp(number);
}
Pattern Matching in Scala – Java case statements on steroids
§Generalised form of case statement
–But more powerfull
§No ‘fall through’ (common case)
// checks if a number between 1 and 10 is prime
numbermatch{
case 1 =>returntrue;
case 2 =>returntrue;
case 3 =>returntrue;
case 5 =>returntrue;
case 7 =>returntrue;
case_=>returnfalse; //similar to Java’s default case
}
Case statements in Scala
§But we can do better…
§Drop the semicolons, of course
§Drop the return statements
–Again, scala’s type inference kicks in – return value of function is simply last resolved statement
// checks if a number between 1 and 10 is prime
numbermatch{
case 1 =>true
case 2 =>true
case 3 =>true
case 5 =>true
case 7 =>true
case_=>false
}
Clean eh?
Pattern matching
§List constructor – the ‘cons’ notation –“::”
–head :: tail// matches 1 or more (can also use foo :: bar )
–foo :: 2// 2 element list where 2nd element is integer 2
–head :: Nil // Nil (is an object – extends List) represents an empty list – matches 1 element list
–x :: xs // matches 1 or more
§Nil and :: are actually case Classes!
–remember, there is nearly no restriction on characters used for class and method names etc
–Any method which takes a single parameter can be used as an infix operator – but :: (cons) when used in pattern matching is a special case
•That is, the infix operator “::” here, is treated as a constructor pattern ::(foo, bar)
–From scala-lang.org: Case classes are regular classes which export their constructor parameters and which provide a recursive decomposition mechanism via pattern matching.
–Checks the entire tree of the objects match
–Can do things like: caseFoo(x, Bar(y)) ifx==y => true
numbersmatch {
casehead::Nil => head* 2
case _=> {…}
}
Recursion in Functional Scala
§Who can describe what a recursive function is?
–Recursion in computer programming defines a function in terms of itself.
–Recursion: *see Recursion.
defloveReduce(…)…{
…
… loveReduce(g(x))
}
Our reduce function
§For g(x) we need to return a list one element smaller in order for our recursion to work (i.e. reduce)
–Step 1
numbers.zip(numbers.tail)
–Step 2
.map{x=> x._1 + x._2 }
§Nicer syntax – using a match expresion in place of a function literal
.map{ case (a,b)=> a + b }
–“A match expression can be used anywhere a function literal can be used. Essentially, a match expression is a function literal, only more general.”
Recursion in Functional Scala
loveReduce(numbers.zip(numbers.tail)
.map{case(a,b)=>a+b})
§Each call, calls loveReduce, with a list one element smaller (due to the zip call)
Putting it all together -> Recursion
defloveReduce(numbers:List[Int]):Int =
numbersmatch{
casehead::Nil=>head* 2
case_=>loveReduce(numbers.zip(numbers.tail)
.map{case(a,b)=>a+b})
}
NB: Don’t need surrounding curly braces because it’s a single statement
System.out.println(”length in centimeters is ” + lengthInCentimeters.getEstimatedValue());
VS
varlen=50.centimeters+25.meters
println(”lengthincentimetersis“+len)
More Scala to come…
§Class properties VS instance variables
–Static?
§Currying
–the technique of transforming a function that takes multiple arguments into a function that takes a single argument (the other arguments having been specified by the curry).
§Advanced Pattern Matching and Case Classes
§Actors Library
–concurrent processes that communicate by exchanging messages.
Combinator Parsing
§Define language parsers out of context free grammar definitions
–So – all static members are now grouped in their own ‘object’
§Scala’s “companion ‘object’” (singleton object)
More and more and more….
§Nested Functions
§By-name parameters
–Lazy evaluation
–Logging –no more stupid ( if ( log.isEnabled() ) protection! )
§Private packages
§LiftWeb (Rails/Grails like web app for Scala)
§The Option type – optional values –Some or None
§First-class Properties coming…
–Properties are objects (OO sense)
–Govern access to a (possibly calculated) field.
–Properties can have other methods in addition to get/set logic, such as registering event listeners.
§Writing new control structures
valfile=newFile(“date.txt”)
withPrintWriter(file){
writer=>writer.println(newjava.util.Date)
}
More Technologies are out there!
§Expand your mind!
§Online Ajax HelpRequestList program in 9 lines!!
–Groovy on Grails
§Maven the modular build system Connoisseur!
§Groovy Builder and G-Strings
–Groovy Command Line Interpreter
–Groovy Wicket builder
§Polygot programming
–a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independently of the programming language used to compile or interpret it.
[...] more presenting and teaching – I am still eager to make anyone who wants to learn, sit through my Scala presentation , even if I haven’t used Scala since I wrote the [...]
[...] more presenting and teaching – I am still eager to make anyone who wants to learn, sit through my Scala presentation
, even if I haven’t used Scala since I wrote the [...]
Pingback by Aaaaand we’re done. « Stubbisms - Tony’s Weblog — December 25, 2008 @ 4:09 am