SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Downloaden Sie, um offline zu lesen
Meet scala
Wojciech Pituła
For AMG.net
Agenda
 What is it?
 Old features done better
 New features
 Why scala?
What is it?
 JVM language
 Object-functional
 Scripting
 Java compatible
 CONCISE
Old features done better
 Syntax
 Classes and Objects
 Basic Types and Operations
 Built-in Control Structures(if, while, switch)
 Packages and Imports
 Assertions and Unit Testing
 Collections
 Type Parameterization(Generics)
Syntax
def max(ints: List[Int]): Int = {
def max(x:Int, y:Int) : Int = if(x>y) x else y
ints.fold(Int.MinValue)(max(_,_))
}
val maxVar = max(List(1,2,3,4))
 vals and vars
 name: Type[typeParam]
 def method(param1:pType, param2:pType): mType = { … }
 array(2) instead of array[2]
 not mandatory semicolon(;)
 operators
 Alternative method call “object method arg”
 Type inference
 Named parameters
Integer max(List<Integer> ints){
Integer max = Integer.MIN_VALUE;
for(Integer intEl : ints)
if(intEl > max)
max = intEl;
return max;
}
Classes and Objects
 Singleton companion objects
 Default constructor
 Mandatory override modifier
 One namespace for fields and methods
class Rational(n: Int, d: Int) {
require(d != 0)
private val g = gcd(n.abs, d.abs)
val numer = n / g
val denom = d / g
def this(n: Int) = this(n, 1)
def + (that: Rational): Rational =
new Rational(
numer * that.denom + that.numer * denom,
denom * that.denom
)
def * (that: Rational): Rational = new Rational(numer * that.numer, denom * that.denom)
override def toString = numer +"/"+ denom
private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
}
 Evaluated at construction time
 Default public modifier
 No get/set convention
 Complicated access modifiers
you don’t want to know about
Basic Types and Operations
 All the Java literals
 (”””str”””) raw, multiline string(escapes doesn’t work)
 Operators are methods, (1).+(2)
 Object equality checked by ==, !=, not by .equals()
 Reference equality checked by eq() method
 foo(bar) is equal to foo.apply(bar)
 Rich primitive types by implicit conversion
 Any, AnyRef, Nothing
Built-in Control Structures(if, while, switch)
 They return values!
 Complicated for expression
 Producing collections with for and yield
 Catch with pattern matching
 match – switch on steroids
 No break and continue
println(if (!args.isEmpty) args(0) else "default.txt")
def grep(pattern: String) =
for (
file <- filesHere
if file.getName.endsWith(".scala");
line <- fileLines(file)
if line.trim.matches(pattern)
) yield (file +": "+ line.trim)
try {
val f = new FileReader("input.txt")
// Use and close file
} catch {
case ex: FileNotFoundException => // Handle missing file
case ex: IOException => // Handle other I/O error
}
def describe(x: Any) = x match {
case 5 => "five"
case true => "truth"
case "hello" => "hi!"
case Nil => "the empty list"
case _ => "something else"
}
Packages and Imports
 Java-like or C#-like packages
 Imports can be declared anywhere
 And are much more powerful
package bobsrockets {
package navigation {
// In package bobsrockets.navigation
class Navigator
package tests {
// In package bobsrockets.navigation.tests
class NavigatorSuite
}
}
}
// easy access to Fruit
import bobsdelights.Fruit
// easy access to all members of bobsdelights
import bobsdelights._
// easy access to all members of Fruits
import bobsdelights.Fruits._
//import only Apple and Orange
import Fruits.{Apple, Orange}
//import all Fruits but Orange and rename Apple to McIntosh
import Fruits.{Apple => McIntosh, Orange =>_, _}
Assertions and Unit Testing
 Static assert() and non-static ensuring()
 JUnit, TestNG, ScalaTest, specs
import org.specs._
import Element.elem
object ElementSpecification extends Specification {
"A UniformElement" should {
"have a width equal to the passed value" in {
elem('x', 2, 3).width must be_==(2)
}
"have a height equal to the passed value" in {
elem('x', 2, 3).height must be_==(3)
}
"throw an IAE if passed a negative width" in {
elem('x', -2, 3) must throwA[IllegalArgumentException]
}
}
}
Collections
 Defaults are immutable!
 Tuples
 Sequences(Arrays, Lists, ArrayBuffers, ListBuffers)
 map, flatMap, filter, collect, fold, …
 Set, Map, MutableSet, MutableMap
val tuple = (1, “hello”, 3.1)
val (integer, str, dbl) = tuple
Array(1.0, 2.0, 3.0)
List(1, 2, 3, 4)
Set(‘a’, ‘b’, ‘c’)
Map("hi" -> 2, "there" -> 5)
Type Parameterization(Generics)
 Types and type parameters are guessed if possible(target typing)
 No raw types
 Generic types have by default nonvariant (or, “rigid”) subtyping
 Lower and upper bounds
scala> val q = Queue(1, 2, 3)
q: Queue[Int] = Queue(1, 2, 3)
def doesNotCompile(q: Queue) {}
//variance
trait Q[T] {…} // nonvariant, Q[String] is not instance of Q[AnyRef]
trait Q[+T] {…} // covariant, Q[String] is instance of Q[AnyRef]
trait Q[-T] {…} // contravariant, Q[AnyRef] is instance of Q[String]
class Queue[+T] (private val leading: List[T], private val trailing: List[T] ) {
def enqueue[U >: T](x: U) = new Queue[U](leading, x :: trailing) // ...
}
def orderedMergeSort[T <: Ordered[T]](xs: List[T]): List[T] = {…}
New features
 Scripting
 Functional/immutable objects
 Functions and Closures
 Traits
 Case Classes, Pattern Matching and Extractors
 Implicit Conversions and Parameters
 Working with XML
 Actors and Concurrency
Scripting
 Scala console
 Script files
 Good debug/testing tool
#!/bin/sh
exec scala "$0" "$@"
!#
// Say hello to the first argument
println("Hello, "+ args(0) +"!")
$ scala
Welcome to Scala version 2.8.1.
Type in expressions to have them evaluated.
Type :help for more information.
scala> 1 + 2
res0: Int = 3
scala> res0 * 3
res1: Int = 9
Functional/immutable objects
 Matter of convention
 No mutable state(vals of immutable types only)
 “Mutating” methods return new object
 Mutable alternatives(builders)
 + Easier to reason about
 + Pass freely around the system
 + Thread safe
 + Safe hash keys
 - COPY
Functions and Closures
 Methods
 Nested/local functions
 Function literals and values(first-class and higher-class)
 Placeholder syntax
 Closures
 Repeated parameters
 Named arguments
 Default values
 Tail recursion – no stack overflow and overhead
scala> var increase = (x: Int) => x + 1
increase: (Int) => Int = <function1>
scala> increase(10)
res0: Int = 11
val someNumbers = List(11, 10, 5, 0, 5, 10)
someNumbers.foreach((x: Int) => println(x))
someNumbers.filter(x => x > 0)
someNumbers.map(_ * 2)
val f = (_: Int) + (_: Int) ; f(5, 10)
def sum(a: Int, b: Int, c: Int) = a + b + c
val a = sum _ //partially applied function
// also correct: val a = sum
val b = sum(1, _: Int, 3)
def makeIncreaser(more: Int) = (x: Int) => x + more //closure
val inc1 = makeIncreaser(1) // inc1(10) = 11
val inc9999 = makeIncreaser(9999) //inc9999(-9) = 9990
def approximate(guess: Double): Double =
if (isGoodEnough(guess)) guess
else approximate(improve(guess)) // last call in body is recursive
def div(x :Int, y :Int=1) { x*y }
div( y=2 , x=3)
div(3)
scala> var increase = (x: Int) => x + 1
increase: (Int) => Int = <function1>
scala> increase(10)
res0: Int = 11
scala> var increase = (x: Int) => x + 1
increase: (Int) => Int = <function1>
scala> increase(10)
res0: Int = 11
scala> var increase = (x: Int) => x + 1
increase: (Int) => Int = <function1>
scala> increase(10)
res0: Int = 11
scala> var increase = (x: Int) => x + 1
increase: (Int) => Int = <function1>
scala> increase(10)
res0: Int = 11
Traits
 Interfaces on steroids
 Multiple inheritance
 Linear order
 Order of mixins is important!
abstract class IntQueue {
def get(): Int
def put(x: Int)
}
class BasicIntQueue extends IntQueue {
private val buf = new ArrayBuffer[Int]
def get() = buf.remove(0)
def put(x: Int) { buf += x }
}
trait Doubling extends IntQueue {
abstract override def put(x: Int) { super.put(2 * x) }
}
trait Incrementing extends IntQueue {
abstract override def put(x: Int) { super.put(x + 1) }
}
trait Filtering extends IntQueue {
abstract override def put(x: Int) {
if (x >= 0) super.put(x)
}
}
val queue = (new BasicIntQueue with Incrementing with Filtering)
• If the behavior will not be reused, then make it a concrete class
• If it might be reused in multiple, unrelated classes, make it a trait.
• If you want to inherit from it in Java code, use an abstract class.
• If you plan to distribute it in compiled form … its more complicated
• If efficiency is very important, lean towards using a class.
• If you still do not know, start with a trait.
Case Classes, Pattern Matching and Extractors
abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
case class UnOp(operator: String, arg: Expr) extends Expr
case class BinOp(operator: String, left: Expr, right: Expr) extends Expr
 A bit like enums
 Automatically added factory method,
toString, hashCode, equals, copy
 Deep matches
 Sealed classes
 Subclasses in the same file
 Matching coverage check
 Extractors – allows you to treat class like a case class
def simplifyAll(expr: Expr): Expr = expr match {
case UnOp("", UnOp("", e)) => simplifyAll(e) // ‘’is its own inverse
case BinOp("+", e, Number(0)) => simplifyAll(e) // ‘0’ is a neutral element for ‘+’
case BinOp("*", e, Number(1)) => simplifyAll(e) // ‘1’ is a neutral element for ‘*’
case UnOp(op, e) => UnOp(op, simplifyAll(e))
case BinOp(op, l, r) => BinOp(op, simplifyAll(l), simplifyAll(r))
case _ => expr
}
def describe(x: Any) = x match {
case 5 => "five"
case true => "truth"
case "hello" => "hi!"
case Nil => "the empty list"
case _ => "something else"
}
object EMail {
// The injection method (optional)
def apply(user: String, domain: String) = user +"@"+ domain
// The extraction method (mandatory)
def unapply(str: String): Option[(String, String)] = {
val parts = str split "@"
if (parts.length == 2) Some(parts(0), parts(1)) else None
}
}
Implicit Conversions and Parameters
 Implicit conversion
 Only definitions marked implicit are available
 An inserted implicit conversion must be in scope as a single identifier, or
be associated with the source or target type of the conversion.
 One-at-a-time Rule: Only one implicit is tried
 Whenever code type checks as it is written, no implicits are attempted.
 Implicit parameters
implicit def function2ActionListener(f: ActionEvent => Unit) =
new ActionListener {
def actionPerformed(event: ActionEvent) = f(event)
}
class PreferredPrompt(val preference: String)
object Greeter {
def greet(name: String)(implicit prompt: PreferredPrompt) {
println("Welcome, "+ name +". The system is ready.")
println(prompt.preference)
}
}
object JoesPrefs {
implicit val prompt = new PreferredPrompt("Yes, master> ")
}
import JoesPrefs._
Greeter.greet("Joe")
Working with XML
 XML literals
 Compiled Xpath-like syntax
 Pattern matching
scala> val yearMade = 1955
scala> <a> { if (yearMade < 2000) <old>{yearMade+”r.”}</old>
else xml.NodeSeq.Empty }
</a>
scala> <a><b><c>hello</c></b></a>  "b"
res10: scala.xml.NodeSeq = <b><c>hello</c></b>
scala> <a><b><c d="world">hello</c></b></a> 
"c"  "@d"
res12: scala.xml.NodeSeq = world
abstract class CCTherm {
...
def toXML =
<cctherm>
<description>{description}</description>
<yearMade>{yearMade}</yearMade>
<dateObtained>{dateObtained}</dateObtained>
<bookPrice>{bookPrice}</bookPrice>
<purchasePrice>{purchasePrice}</purchasePrice>
<condition>{condition}</condition>
</cctherm>
}
abstract class CCTherm {
...
def fromXML(node: scala.xml.Node): CCTherm =
new CCTherm {
val description = (node  "description").text
val yearMade = (node  "yearMade").text.toInt
val dateObtained = (node  "dateObtained").text
val bookPrice = (node  "bookPrice").text.toInt
val purchasePrice = (node  "purchasePrice").text.toInt
val condition = (node  "condition").text.toInt
}}
Actors and Concurrency
 Anti „synchronized” way
 Actor – thread + mailbox + actions
 Good style:
 Actors should not block
 Communicate only via messages
 Prefer immutable messages
 Akka
 Typesafe actors comming
object NameResolver extends Actor {
import java.net.{InetAddress, UnknownHostException}
def act() {
react {
case (name: String, actor: Actor) =>
actor ! getIp(name)
act()
case "EXIT" =>
println("Name resolver exiting.")
// quit
case msg =>
println("Unhandled message: "+ msg)
act()
}
}
def getIp(name: String): Option[InetAddress] = {
try {
Some(InetAddress.getByName(name))
} catch {
case _:UnknownHostException => None
}
}
}
scala> NameResolver.start()
res0: scala.actors.Actor = NameResolver$@90d6c5
scala> NameResolver ! ("www.scalalang.org", self)
scala> self.receiveWithin(0) { case x => x }
res2: Any = Some(www.scalalang.org/128.178.154.102)
scala> NameResolver ! ("wwwwww.scalalang.org", self)
scala> self.receiveWithin(0) { case x => x }
res4: Any = None
Why scala?
 Conciseness
 Immutability
 Modernity
 Convenience
References
 Programming in Scala
 Free at http://www.artima.com/pins1ed/
 Scala for Java Programmers Tutorial
 http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html

Weitere ähnliche Inhalte

Was ist angesagt?

Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSigmoid
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and MonoidsHugo Gävert
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Derek Chen-Becker
 
Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? DataWorks Summit
 
Functional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedFunctional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedSusan Potter
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011Mandi Walls
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaDerek Chen-Becker
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Jonas Bonér
 
SparkSQL and Dataframe
SparkSQL and DataframeSparkSQL and Dataframe
SparkSQL and DataframeNamgee Lee
 
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLabApache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLabCloudxLab
 
Scalding: Twitter's Scala DSL for Hadoop/Cascading
Scalding: Twitter's Scala DSL for Hadoop/CascadingScalding: Twitter's Scala DSL for Hadoop/Cascading
Scalding: Twitter's Scala DSL for Hadoop/Cascadingjohnynek
 

Was ist angesagt? (18)

Why Haskell
Why HaskellWhy Haskell
Why Haskell
 
Spark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. JyotiskaSpark Dataframe - Mr. Jyotiska
Spark Dataframe - Mr. Jyotiska
 
Scala
ScalaScala
Scala
 
Collection
CollectionCollection
Collection
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010Starting with Scala : Frontier Developer's Meetup December 2010
Starting with Scala : Frontier Developer's Meetup December 2010
 
Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch? Should I Use Scalding or Scoobi or Scrunch?
Should I Use Scalding or Scoobi or Scrunch?
 
Functional Algebra: Monoids Applied
Functional Algebra: Monoids AppliedFunctional Algebra: Monoids Applied
Functional Algebra: Monoids Applied
 
Meetup slides
Meetup slidesMeetup slides
Meetup slides
 
R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011R for Pirates. ESCCONF October 27, 2011
R for Pirates. ESCCONF October 27, 2011
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Stepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to ScalaStepping Up : A Brief Intro to Scala
Stepping Up : A Brief Intro to Scala
 
Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)Pragmatic Real-World Scala (short version)
Pragmatic Real-World Scala (short version)
 
scala
scalascala
scala
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
SparkSQL and Dataframe
SparkSQL and DataframeSparkSQL and Dataframe
SparkSQL and Dataframe
 
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLabApache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
Apache Spark - Key-Value RDD | Big Data Hadoop Spark Tutorial | CloudxLab
 
Scalding: Twitter's Scala DSL for Hadoop/Cascading
Scalding: Twitter's Scala DSL for Hadoop/CascadingScalding: Twitter's Scala DSL for Hadoop/Cascading
Scalding: Twitter's Scala DSL for Hadoop/Cascading
 

Andere mochten auch

Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...
Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...
Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...Łukasz Miądowicz
 
Intel Business Challenge Europe 2014 Pitching Hacks
Intel Business Challenge Europe 2014 Pitching HacksIntel Business Challenge Europe 2014 Pitching Hacks
Intel Business Challenge Europe 2014 Pitching HacksŁukasz Miądowicz
 
Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...
Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...
Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...Łukasz Miądowicz
 
NeurOn input EEG powered touchless keyboard
NeurOn input EEG powered touchless keyboard NeurOn input EEG powered touchless keyboard
NeurOn input EEG powered touchless keyboard Łukasz Miądowicz
 
Continous delivery with sbt
Continous delivery with sbtContinous delivery with sbt
Continous delivery with sbtWojciech Pituła
 

Andere mochten auch (6)

Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...
Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...
Konferencja Intel Business Challenge 2014 w Gdańskim Parku Naukowo-Technologi...
 
Intel Business Challenge Europe 2014 Pitching Hacks
Intel Business Challenge Europe 2014 Pitching HacksIntel Business Challenge Europe 2014 Pitching Hacks
Intel Business Challenge Europe 2014 Pitching Hacks
 
Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...
Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...
Doktorat, a własny startup ? Jak zacząć budować startup na uczelni w ujęciu l...
 
NeurOn input EEG powered touchless keyboard
NeurOn input EEG powered touchless keyboard NeurOn input EEG powered touchless keyboard
NeurOn input EEG powered touchless keyboard
 
Continous delivery with sbt
Continous delivery with sbtContinous delivery with sbt
Continous delivery with sbt
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 

Ähnlich wie Meet scala

(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala Knoldus Inc.
 
Scala or functional programming from a python developer's perspective
Scala or functional programming from a python developer's perspectiveScala or functional programming from a python developer's perspective
Scala or functional programming from a python developer's perspectivegabalese
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldWerner Hofstra
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.jstimourian
 

Ähnlich wie Meet scala (20)

(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Functions In Scala
Functions In Scala Functions In Scala
Functions In Scala
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Scala or functional programming from a python developer's perspective
Scala or functional programming from a python developer's perspectiveScala or functional programming from a python developer's perspective
Scala or functional programming from a python developer's perspective
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala Introduction
Scala IntroductionScala Introduction
Scala Introduction
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Scala for curious
Scala for curiousScala for curious
Scala for curious
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Underscore.js
Underscore.jsUnderscore.js
Underscore.js
 
Scala Paradigms
Scala ParadigmsScala Paradigms
Scala Paradigms
 

Kürzlich hochgeladen

Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 

Kürzlich hochgeladen (20)

Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 

Meet scala

  • 2. Agenda  What is it?  Old features done better  New features  Why scala?
  • 3. What is it?  JVM language  Object-functional  Scripting  Java compatible  CONCISE
  • 4. Old features done better  Syntax  Classes and Objects  Basic Types and Operations  Built-in Control Structures(if, while, switch)  Packages and Imports  Assertions and Unit Testing  Collections  Type Parameterization(Generics)
  • 5. Syntax def max(ints: List[Int]): Int = { def max(x:Int, y:Int) : Int = if(x>y) x else y ints.fold(Int.MinValue)(max(_,_)) } val maxVar = max(List(1,2,3,4))  vals and vars  name: Type[typeParam]  def method(param1:pType, param2:pType): mType = { … }  array(2) instead of array[2]  not mandatory semicolon(;)  operators  Alternative method call “object method arg”  Type inference  Named parameters Integer max(List<Integer> ints){ Integer max = Integer.MIN_VALUE; for(Integer intEl : ints) if(intEl > max) max = intEl; return max; }
  • 6. Classes and Objects  Singleton companion objects  Default constructor  Mandatory override modifier  One namespace for fields and methods class Rational(n: Int, d: Int) { require(d != 0) private val g = gcd(n.abs, d.abs) val numer = n / g val denom = d / g def this(n: Int) = this(n, 1) def + (that: Rational): Rational = new Rational( numer * that.denom + that.numer * denom, denom * that.denom ) def * (that: Rational): Rational = new Rational(numer * that.numer, denom * that.denom) override def toString = numer +"/"+ denom private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b) }  Evaluated at construction time  Default public modifier  No get/set convention  Complicated access modifiers you don’t want to know about
  • 7. Basic Types and Operations  All the Java literals  (”””str”””) raw, multiline string(escapes doesn’t work)  Operators are methods, (1).+(2)  Object equality checked by ==, !=, not by .equals()  Reference equality checked by eq() method  foo(bar) is equal to foo.apply(bar)  Rich primitive types by implicit conversion  Any, AnyRef, Nothing
  • 8. Built-in Control Structures(if, while, switch)  They return values!  Complicated for expression  Producing collections with for and yield  Catch with pattern matching  match – switch on steroids  No break and continue println(if (!args.isEmpty) args(0) else "default.txt") def grep(pattern: String) = for ( file <- filesHere if file.getName.endsWith(".scala"); line <- fileLines(file) if line.trim.matches(pattern) ) yield (file +": "+ line.trim) try { val f = new FileReader("input.txt") // Use and close file } catch { case ex: FileNotFoundException => // Handle missing file case ex: IOException => // Handle other I/O error } def describe(x: Any) = x match { case 5 => "five" case true => "truth" case "hello" => "hi!" case Nil => "the empty list" case _ => "something else" }
  • 9. Packages and Imports  Java-like or C#-like packages  Imports can be declared anywhere  And are much more powerful package bobsrockets { package navigation { // In package bobsrockets.navigation class Navigator package tests { // In package bobsrockets.navigation.tests class NavigatorSuite } } } // easy access to Fruit import bobsdelights.Fruit // easy access to all members of bobsdelights import bobsdelights._ // easy access to all members of Fruits import bobsdelights.Fruits._ //import only Apple and Orange import Fruits.{Apple, Orange} //import all Fruits but Orange and rename Apple to McIntosh import Fruits.{Apple => McIntosh, Orange =>_, _}
  • 10. Assertions and Unit Testing  Static assert() and non-static ensuring()  JUnit, TestNG, ScalaTest, specs import org.specs._ import Element.elem object ElementSpecification extends Specification { "A UniformElement" should { "have a width equal to the passed value" in { elem('x', 2, 3).width must be_==(2) } "have a height equal to the passed value" in { elem('x', 2, 3).height must be_==(3) } "throw an IAE if passed a negative width" in { elem('x', -2, 3) must throwA[IllegalArgumentException] } } }
  • 11. Collections  Defaults are immutable!  Tuples  Sequences(Arrays, Lists, ArrayBuffers, ListBuffers)  map, flatMap, filter, collect, fold, …  Set, Map, MutableSet, MutableMap val tuple = (1, “hello”, 3.1) val (integer, str, dbl) = tuple Array(1.0, 2.0, 3.0) List(1, 2, 3, 4) Set(‘a’, ‘b’, ‘c’) Map("hi" -> 2, "there" -> 5)
  • 12. Type Parameterization(Generics)  Types and type parameters are guessed if possible(target typing)  No raw types  Generic types have by default nonvariant (or, “rigid”) subtyping  Lower and upper bounds scala> val q = Queue(1, 2, 3) q: Queue[Int] = Queue(1, 2, 3) def doesNotCompile(q: Queue) {} //variance trait Q[T] {…} // nonvariant, Q[String] is not instance of Q[AnyRef] trait Q[+T] {…} // covariant, Q[String] is instance of Q[AnyRef] trait Q[-T] {…} // contravariant, Q[AnyRef] is instance of Q[String] class Queue[+T] (private val leading: List[T], private val trailing: List[T] ) { def enqueue[U >: T](x: U) = new Queue[U](leading, x :: trailing) // ... } def orderedMergeSort[T <: Ordered[T]](xs: List[T]): List[T] = {…}
  • 13. New features  Scripting  Functional/immutable objects  Functions and Closures  Traits  Case Classes, Pattern Matching and Extractors  Implicit Conversions and Parameters  Working with XML  Actors and Concurrency
  • 14. Scripting  Scala console  Script files  Good debug/testing tool #!/bin/sh exec scala "$0" "$@" !# // Say hello to the first argument println("Hello, "+ args(0) +"!") $ scala Welcome to Scala version 2.8.1. Type in expressions to have them evaluated. Type :help for more information. scala> 1 + 2 res0: Int = 3 scala> res0 * 3 res1: Int = 9
  • 15. Functional/immutable objects  Matter of convention  No mutable state(vals of immutable types only)  “Mutating” methods return new object  Mutable alternatives(builders)  + Easier to reason about  + Pass freely around the system  + Thread safe  + Safe hash keys  - COPY
  • 16. Functions and Closures  Methods  Nested/local functions  Function literals and values(first-class and higher-class)  Placeholder syntax  Closures  Repeated parameters  Named arguments  Default values  Tail recursion – no stack overflow and overhead scala> var increase = (x: Int) => x + 1 increase: (Int) => Int = <function1> scala> increase(10) res0: Int = 11 val someNumbers = List(11, 10, 5, 0, 5, 10) someNumbers.foreach((x: Int) => println(x)) someNumbers.filter(x => x > 0) someNumbers.map(_ * 2) val f = (_: Int) + (_: Int) ; f(5, 10) def sum(a: Int, b: Int, c: Int) = a + b + c val a = sum _ //partially applied function // also correct: val a = sum val b = sum(1, _: Int, 3) def makeIncreaser(more: Int) = (x: Int) => x + more //closure val inc1 = makeIncreaser(1) // inc1(10) = 11 val inc9999 = makeIncreaser(9999) //inc9999(-9) = 9990 def approximate(guess: Double): Double = if (isGoodEnough(guess)) guess else approximate(improve(guess)) // last call in body is recursive def div(x :Int, y :Int=1) { x*y } div( y=2 , x=3) div(3) scala> var increase = (x: Int) => x + 1 increase: (Int) => Int = <function1> scala> increase(10) res0: Int = 11 scala> var increase = (x: Int) => x + 1 increase: (Int) => Int = <function1> scala> increase(10) res0: Int = 11 scala> var increase = (x: Int) => x + 1 increase: (Int) => Int = <function1> scala> increase(10) res0: Int = 11 scala> var increase = (x: Int) => x + 1 increase: (Int) => Int = <function1> scala> increase(10) res0: Int = 11
  • 17. Traits  Interfaces on steroids  Multiple inheritance  Linear order  Order of mixins is important! abstract class IntQueue { def get(): Int def put(x: Int) } class BasicIntQueue extends IntQueue { private val buf = new ArrayBuffer[Int] def get() = buf.remove(0) def put(x: Int) { buf += x } } trait Doubling extends IntQueue { abstract override def put(x: Int) { super.put(2 * x) } } trait Incrementing extends IntQueue { abstract override def put(x: Int) { super.put(x + 1) } } trait Filtering extends IntQueue { abstract override def put(x: Int) { if (x >= 0) super.put(x) } } val queue = (new BasicIntQueue with Incrementing with Filtering) • If the behavior will not be reused, then make it a concrete class • If it might be reused in multiple, unrelated classes, make it a trait. • If you want to inherit from it in Java code, use an abstract class. • If you plan to distribute it in compiled form … its more complicated • If efficiency is very important, lean towards using a class. • If you still do not know, start with a trait.
  • 18. Case Classes, Pattern Matching and Extractors abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: String, left: Expr, right: Expr) extends Expr  A bit like enums  Automatically added factory method, toString, hashCode, equals, copy  Deep matches  Sealed classes  Subclasses in the same file  Matching coverage check  Extractors – allows you to treat class like a case class def simplifyAll(expr: Expr): Expr = expr match { case UnOp("", UnOp("", e)) => simplifyAll(e) // ‘’is its own inverse case BinOp("+", e, Number(0)) => simplifyAll(e) // ‘0’ is a neutral element for ‘+’ case BinOp("*", e, Number(1)) => simplifyAll(e) // ‘1’ is a neutral element for ‘*’ case UnOp(op, e) => UnOp(op, simplifyAll(e)) case BinOp(op, l, r) => BinOp(op, simplifyAll(l), simplifyAll(r)) case _ => expr } def describe(x: Any) = x match { case 5 => "five" case true => "truth" case "hello" => "hi!" case Nil => "the empty list" case _ => "something else" } object EMail { // The injection method (optional) def apply(user: String, domain: String) = user +"@"+ domain // The extraction method (mandatory) def unapply(str: String): Option[(String, String)] = { val parts = str split "@" if (parts.length == 2) Some(parts(0), parts(1)) else None } }
  • 19. Implicit Conversions and Parameters  Implicit conversion  Only definitions marked implicit are available  An inserted implicit conversion must be in scope as a single identifier, or be associated with the source or target type of the conversion.  One-at-a-time Rule: Only one implicit is tried  Whenever code type checks as it is written, no implicits are attempted.  Implicit parameters implicit def function2ActionListener(f: ActionEvent => Unit) = new ActionListener { def actionPerformed(event: ActionEvent) = f(event) } class PreferredPrompt(val preference: String) object Greeter { def greet(name: String)(implicit prompt: PreferredPrompt) { println("Welcome, "+ name +". The system is ready.") println(prompt.preference) } } object JoesPrefs { implicit val prompt = new PreferredPrompt("Yes, master> ") } import JoesPrefs._ Greeter.greet("Joe")
  • 20. Working with XML  XML literals  Compiled Xpath-like syntax  Pattern matching scala> val yearMade = 1955 scala> <a> { if (yearMade < 2000) <old>{yearMade+”r.”}</old> else xml.NodeSeq.Empty } </a> scala> <a><b><c>hello</c></b></a> "b" res10: scala.xml.NodeSeq = <b><c>hello</c></b> scala> <a><b><c d="world">hello</c></b></a> "c" "@d" res12: scala.xml.NodeSeq = world abstract class CCTherm { ... def toXML = <cctherm> <description>{description}</description> <yearMade>{yearMade}</yearMade> <dateObtained>{dateObtained}</dateObtained> <bookPrice>{bookPrice}</bookPrice> <purchasePrice>{purchasePrice}</purchasePrice> <condition>{condition}</condition> </cctherm> } abstract class CCTherm { ... def fromXML(node: scala.xml.Node): CCTherm = new CCTherm { val description = (node "description").text val yearMade = (node "yearMade").text.toInt val dateObtained = (node "dateObtained").text val bookPrice = (node "bookPrice").text.toInt val purchasePrice = (node "purchasePrice").text.toInt val condition = (node "condition").text.toInt }}
  • 21. Actors and Concurrency  Anti „synchronized” way  Actor – thread + mailbox + actions  Good style:  Actors should not block  Communicate only via messages  Prefer immutable messages  Akka  Typesafe actors comming object NameResolver extends Actor { import java.net.{InetAddress, UnknownHostException} def act() { react { case (name: String, actor: Actor) => actor ! getIp(name) act() case "EXIT" => println("Name resolver exiting.") // quit case msg => println("Unhandled message: "+ msg) act() } } def getIp(name: String): Option[InetAddress] = { try { Some(InetAddress.getByName(name)) } catch { case _:UnknownHostException => None } } } scala> NameResolver.start() res0: scala.actors.Actor = NameResolver$@90d6c5 scala> NameResolver ! ("www.scalalang.org", self) scala> self.receiveWithin(0) { case x => x } res2: Any = Some(www.scalalang.org/128.178.154.102) scala> NameResolver ! ("wwwwww.scalalang.org", self) scala> self.receiveWithin(0) { case x => x } res4: Any = None
  • 22. Why scala?  Conciseness  Immutability  Modernity  Convenience
  • 23. References  Programming in Scala  Free at http://www.artima.com/pins1ed/  Scala for Java Programmers Tutorial  http://docs.scala-lang.org/tutorials/scala-for-java-programmers.html