SlideShare a Scribd company logo
1 of 78
If I were to pick a language to use on the JVM
today other than Java, it would be Scala.
– James Gosling, creator of Java
http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming
Scala, it must be stated, is the current heir apparent to the
Java throne. No other language on the JVM seems as
capable of being a "replacement for Java" as Scala, and
the momentum behind Scala is now unquestionable.
– Charlies Nutter, JRuby lead
http://blog.headius.com/2009/04/future-part-one.html
My tip though for the long term replacement of javac is Scala.
I'm very impressed with it! I can honestly say if someone had
shown me the Programming in Scala book […] back in 2003
I'd probably have never created Groovy.
– James Strachan, creator of Groovy
http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html
Agenda
● Background and motivation
● Intro to Scala
● Basic syntax
● Pattern matching
● Functions
● Classes and traits
● <break />
● Practical Scala
About us
● Alf Kristian Støyle and Fredrik Vraalsen
● Java developers with 6 and 9 years experience
● Scala enthusiasts since 2008
● Developed SubmitIT for JavaZone
● Alf worked on Scala projects for
Kommuneforlaget
● Held 4 Scala training courses for 60+
participants
public class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
}
class Person(var age: Int, var name: String)
List<Person> persons = ...
List<Person> adults = new LinkedList<Person>();
List<Person> kids = new LinkedList<Person>();
for (Person person : persons) {
if (person.getAge() < 18) {
kids.add(person);
} else {
adults.add(person);
}
}
val persons: List[Person] = ...
val (kids, adults) = persons.partition(_.age < 18)
using(new BufferedReader(new FileReader("f.txt"))) {
reader => println(reader.readLine())
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("f.txt"));
System.out.println(reader.readLine());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// Exception on close, ignore
}
}
}
Scala
● Object oriented and functional
● Statically typed
● Java compatible
● Compiles to Java bytecode
● Existing libraries/frameworks
● Better Java
http://4.bp.blogspot.com/_RshsKGzNxWA/TB9QhCXK0fI/AAAAAAAAAEc/MFwujJaIRmw/s1600/BlueRedPill.jpg
Variables
var i: Int = 42
Variables
var i = 42
i = 3
i = "Hello world!" // compile error
Values
val i = 42
i = 3 // compile error
Methods
def sum(a: Int, b: Int): Int = {
return a + b
}
Methods
def sum(a: Int, b: Int): Int = {
a + b
}
Methods
def sum(a: Int, b: Int) = {
a + b
}
Methods
def sum(a: Int, b: Int) = a + b
Methods
"apple".charAt(0)
"apple" charAt 0
1.0.+(2.0)
1.0 + 2.0
Collections
val list = List("apple", "orange", "banana")
val map = Map(1 -> "one", 2 -> "two")
val array = Array(1, 2, 3, 4, 5)
list(1) // orange
map(2) // two
array(3) // 4
myObject match {
case 1 => println("First")
case 2 => println("Second")
case _ => println("Unknown")
}
Pattern matching
myObject match {
case i: Int => println("Found number " + i)
case s: String => println("Found text " + s)
case _ => println("Unknown")
}
Pattern matching
val email = """(.+)@(.+)""".r
"scala@java.no" match {
case email(name, domain) =>
println("User " + name + " at " + domain)
case x => println(x + " is not an email")
}
Pattern matching
Functions
Functions
Predicate<Integer> even = new Predicate<Integer>() {
@Override
public boolean apply(Integer i) {
return i % 2 == 0;
}
};
List<Integer> numbers = … // 1, 2, 3, 4
Iterable<Integer> evenNums =
Iterables.filter(numbers, even);
=> [2, 4]
Google collections:
Functions
val even = (i: Int) => i % 2 == 0
val numbers = List(1, 2, 3, 4)
val evenNums = numbers.filter(even)
=> List(2, 4)
Scala collections:
Functions
val numbers = List(1, 2, 3, 4)
val evenNums = numbers.filter((i: Int) => i % 2 == 0)
=> List(2, 4)
Scala collections:
Functions
val numbers = List(1, 2, 3, 4)
val evenNums = numbers.filter(i => i % 2 == 0)
=> List(2, 4)
Scala collections:
Functions
val numbers = List(1, 2, 3, 4)
val evenNums = numbers.filter(_ % 2 == 0)
=> List(2, 4)
Scala collections:
Functions
Predicate<Integer> even = new Predicate<Integer>() {
@Override
public boolean apply(Integer i) {
return i % 2 == 0;
}
};
val numbers = List(1, 2, 3, 4)
val evenNums = numbers.filter(_ % 2 == 0)
=> List(2, 4)
Scala collections:
“Functional programming”
● First class functions
● Pattern matching
● …
Functional programming
● Mathematical functions have no side effects
● f(x) = 2x + 3
In practice
● Only immutable objects (and object graphs)
● All field must be immutable
● No side-effects from method calls
● All methods must return something
Immutable datastrukturer
for (Iterator i = persons.iterator(); i.hasNext();) {
Person person = (Person) i.next();
if (person.getAge() < 18) {
i.remove();
}
}
val adults = persons.filter(_.age >= 18)
Strive to be pure
● Concurrency
● Easier to avoid errors
● Easier to test
Control structures return values
val numbers = for (i <- 1 to 10) yield i
val res1 = if (cond) x else y
val res2 = try { x } catch { … y } finally { … }
Classes and constructors
class Person(val name: String, val age: Int)
Classes and constructors
class Person(val name: String, val age: Int) {
def this(name: String) = this(name, 42)
override def toString = "My age is " + age
}
Traits (= Interface + Mixin)
● “Multiple inheritance done right”
● Implement methods
● Initialize fields
scala.Ordered trait
trait Ordered[A] {
def compare(that: A): Int
def < (that: A) = this.compare(that) < 0
def > (that: A) = this.compare(that) > 0
def <= (that: A) = this.compare(that) <= 0
def >= (that: A) = this.compare(that) >= 0
}
The Ordered trait
class Person(val age: Int) extends Ordered[Person] {
def compare(other: Person) = this.age - other.age
}
val person1 = new Person(21)
val person2 = new Person(31)
person1.<(person2) // true
person1 <= person2 // true
person1 >= person2 // false
Just scratching the surface...
● Tuples
● Singleton objects
● Closures
● For-comprehensions
● Implicit conversions
● Actors
● Native XML data types
● Parsers
Q & A
val xml = <break time="30" />
Practical Scala
● The downside
● Tools
● ScalaTest
● Java interoperability
● Learning curve
● Your choice
● Implicit conversions
● Higher order functions
IDE support
● Netbeans - very good, but inferior in other
areas
● IntelliJ IDEA - very good, but slow compilation
● Eclipse - getting better, very fast when working
Backward compatibility
● Binary compatibility broken several times
● Think twice before using a library
● 2.7.3 – 2.7.4
● 2.8.0 <-> 2.8.1.RC4
Tools demo
● REPL - Read eval print loop
● Maven
● SBT - Simple Build Tool
● IntelliJ IDEA
ScalaTest demo
● DSL for testing
● Powerful assertions
● Different test styles
● JUnit
● Functional tests
● BDD test
The feel of Scala
http://parleys.com/#id=10&st=5&sl=1
Java interoperability demo
● Scala using Java
● Java using Scala
● Mixed projects?
Learning curve
● Syntax small smack in the face
● Easy to write Java-ish Scala
● The language scales with your understanding
● Gradually migrate to more functional style
What would you like to hear?
● Implicit conversion
● Why does "gnirtS.gnal.avaj".reverse work?
● Higher order functions
● How does the using/resource handling example work?
Higher order functions
Higher order functions
● Functions which take functions as parameters
and/or return functions
Higher order functions
First class functions:
val even: (Int => Boolean) = (i: Int) => i % 2 == 0
Higher order function:
def test(numbers: List[Int], f: Int => Boolean) = ...
Use:
test(List(1, 2, 3), (i: Int) => i % 2 == 0)
test(List(1, 2, 3), _ % 2 == 0)
Higher order functions
def test(numbers: List[Int], f: Int => Boolean): List[Boolean] =
numbers.map(tall => f(tall))
Higher order functions
def test(numbers: List[Int], f: Int => Boolean): List[Boolean] =
numbers.map(tall => f(tall))
Use:
test(List(1, 2, 3), _ % 2 == 0) // List(false, true, false)
call-by-value vs. call-by-name
● by-value: expressions are evaluated before
being passed to the function
● by-name: expressions evaluated inside function
● nice when computationally expensive
● possible to create nice APIs
call-by-value vs. call-by-name
Example: Logging
def thisTakesTime = {
println(“Slow computation”)
“result”
}
call-by-value
def debug(s: String) {
println(“debug”)
if (logLevel <= DEBUG) println(s)
}
logger.debug(thisTakesTime())
// Slow computation
// debug
// result
call-by-name
def debug(s: => String) {
println(“debug”)
if (logLevel <= DEBUG) println(s)
}
logger.debug(thisTakesTime())
// debug
// Slow computation
// result
using(new BufferedReader(new FileReader("f.txt"))) {
reader => println(reader.readLine())
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("f.txt"));
System.out.println(reader.readLine());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// Exception on close, ignore
}
}
}
def using[T <: { def close() }, A]
(closeable: T)
(f: T => A) = {
try {
f(closeable)
} finally {
if (closeable != null) {
try {
closeable.close()
}
catch {
case e: Exception => // Ignore
}
}
}
}
using(new BufferedReader(new FileReader("f.txt"))) {
reader => println(reader.readLine())
}
Implicit conversions
String s = "!em esreveR";
System.out.println(s.reverse());
Magic?
val s: java.lang.String = "!em esreveR"
println(s.reverse)
=> Reverse me!
Magic?
val s: java.lang.String = "!em esreveR"
println(s.reverse)
println(stringWrapper(s).reverse)
=> Reverse me!
String s = "!em esreveR";
System.out.println(s.reverse());
Magic?
val s: java.lang.String = "!em esreveR"
println(s.reverse)
println(augmentString(s).reverse)
println(new StringOps(s).reverse)
=> Reverse me!
String s = "!em esreveR";
System.out.println(s.reverse());
implicit
implicit def augmentString(x: String) =
new StringOps(x)
StringOps
def toArray : Array[Char]
def toBoolean : Boolean
def toByte : Byte
def toDouble : Double
def toFloat : Float
def toInt : Int
def toLong : Long
def toShort : Short
def r : Regex
def lines : Iterator[java.lang.String]
def * (n : Int) : java.lang.String
...
scala.Predef
● A set of implicits
● byteWrapper(x: Byte)
● shortWrapper(x: Short)
● intWrapper(x: Int)
● charWrapper(c: Char)
● longWrapper(x: Long)
● floatWrapper(x: Float)
● doubleWrapper(x: Double)
● booleanWrapper(x: Boolean)
● stringWrapper(x: String)
● stringBuilderWrapper(x :
StringBuilder)
● ...
Map and ArrowAssoc
val myMap: Map[Int, String] =
Map(1 -> "one", 2 -> "two")
Map and ArrowAssoc
val myMap: Map[Int, String] =
Map(1 -> "one", 2 -> "two")
Map.apply((1, "one"), (2, "two"))
Map and ArrowAssoc
val myMap: Map[Int, String] =
Map(1 -> "one", 2 -> "two")
Map((1, "one"), (2, "two"))
Map and ArrowAssoc
val myMap: Map[Int, String] =
Map(1 -> "one", 2 -> "two")
Map((1, "one"), (2, "two"))
val tuple: Tuple2[Int, String] = (1, "one")
Map and ArrowAssoc
val myMap: Map[Int, String] =
Map(1 -> "one", 2 -> "two")
Map(any2ArrowAssoc(1) -> "one",
any2ArrowAssoc(2) -> "two")
Map((1, "one"), (2, "two"))
scala.Predef.ArrowAssoc
implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] =
new ArrowAssoc(x)
class ArrowAssoc[A](x: A) {
def -> [B](y: B): Tuple2[A, B] = (x, y)
}
val myMap = Map(1 -> "one", 2 -> "two")
Implicit rules!
● Marking Rule: Only definitions marked implicit are available.
● Scope Rule: An inserted implicit conversion must be in scope as a
single identifier, or be associated with the source or target type of the
conversion.
● Non-Ambiguity Rule: An implicit conversion is only inserted if there is
no other possible conversion to insert.
● One-at-a-time Rule: Only one implicit is tried.
● Explicits-First Rule: Whenever code type checks as it is written, no
implicits are attempted.
Q & A
Resources
● scala@java.no
(http://lister.java.no/mailman/listinfo/scala)
● http://scala.java.no
● http://www.slideshare.net/stoyle/scala-ntnu
● http://github.com/javaBin/scala-training-slides
● fvr@knowit.no, aks@knowit.no

More Related Content

What's hot

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
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaOstap Andrusiv
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in ScalaShai Yallin
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersMiles Sabin
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scalascalaconfjp
 
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
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and ClosuresSandip Kumar
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in ScalaDamian Jureczko
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scalapramode_ce
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasGanesh Samarthyam
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesTomer Gabel
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8Martin Toshev
 

What's hot (20)

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala on Android
Scala on AndroidScala on Android
Scala on Android
 
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
 
Scaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with ScalaScaladroids: Developing Android Apps with Scala
Scaladroids: Developing Android Apps with Scala
 
Scala coated JVM
Scala coated JVMScala coated JVM
Scala coated JVM
 
Intro to Functional Programming in Scala
Intro to Functional Programming in ScalaIntro to Functional Programming in Scala
Intro to Functional Programming in Scala
 
Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
scala
scalascala
scala
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
An Introduction to Scala for Java Developers
An Introduction to Scala for Java DevelopersAn Introduction to Scala for Java Developers
An Introduction to Scala for Java Developers
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
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
 
Functional Objects & Function and Closures
Functional Objects  & Function and ClosuresFunctional Objects  & Function and Closures
Functional Objects & Function and Closures
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
 
Introduction to Functional Programming with Scala
Introduction to Functional Programming with ScalaIntroduction to Functional Programming with Scala
Introduction to Functional Programming with Scala
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Scala jargon cheatsheet
Scala jargon cheatsheetScala jargon cheatsheet
Scala jargon cheatsheet
 
New Features in JDK 8
New Features in JDK 8New Features in JDK 8
New Features in JDK 8
 

Viewers also liked

Cultures:Foreign and Domestic
Cultures:Foreign and DomesticCultures:Foreign and Domestic
Cultures:Foreign and DomesticKdemaio
 
Cultures:Foreign and Domestic
Cultures:Foreign and DomesticCultures:Foreign and Domestic
Cultures:Foreign and DomesticKdemaio
 
Simple machines scavenger hunt
Simple machines scavenger huntSimple machines scavenger hunt
Simple machines scavenger huntdanbel2
 
Bakerfinal
BakerfinalBakerfinal
Bakerfinalbakedizz
 
Ekonomika portów lotniczych w Polsce a problem kosztów zewnętrznych
Ekonomika portów lotniczych w Polsce a problem kosztów zewnętrznychEkonomika portów lotniczych w Polsce a problem kosztów zewnętrznych
Ekonomika portów lotniczych w Polsce a problem kosztów zewnętrznychInstytut Ekonomiki Miast i Regionów
 
Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...
Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...
Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...Instytut Ekonomiki Miast i Regionów
 
Fossil fuels powerpoint
Fossil fuels powerpointFossil fuels powerpoint
Fossil fuels powerpointdanbel2
 

Viewers also liked (19)

Cultures:Foreign and Domestic
Cultures:Foreign and DomesticCultures:Foreign and Domestic
Cultures:Foreign and Domestic
 
Cultures:Foreign and Domestic
Cultures:Foreign and DomesticCultures:Foreign and Domestic
Cultures:Foreign and Domestic
 
Simple machines scavenger hunt
Simple machines scavenger huntSimple machines scavenger hunt
Simple machines scavenger hunt
 
Dependency injection in Scala
Dependency injection in ScalaDependency injection in Scala
Dependency injection in Scala
 
Learning Lisp
Learning LispLearning Lisp
Learning Lisp
 
Clojure workshop
Clojure workshopClojure workshop
Clojure workshop
 
Bakerfinal
BakerfinalBakerfinal
Bakerfinal
 
Karma profile
Karma profileKarma profile
Karma profile
 
MODELE BIZNESOWE NA RYNKU PORTÓW LOTNICZYCH...
MODELE BIZNESOWE NA RYNKU PORTÓW LOTNICZYCH...MODELE BIZNESOWE NA RYNKU PORTÓW LOTNICZYCH...
MODELE BIZNESOWE NA RYNKU PORTÓW LOTNICZYCH...
 
Ekonomika portów lotniczych w Polsce a problem kosztów zewnętrznych
Ekonomika portów lotniczych w Polsce a problem kosztów zewnętrznychEkonomika portów lotniczych w Polsce a problem kosztów zewnętrznych
Ekonomika portów lotniczych w Polsce a problem kosztów zewnętrznych
 
Udział kolei w przygotowaniach do EURO 2012 (wrzesień 2008 r.)
Udział kolei w przygotowaniach do EURO 2012 (wrzesień 2008 r.)Udział kolei w przygotowaniach do EURO 2012 (wrzesień 2008 r.)
Udział kolei w przygotowaniach do EURO 2012 (wrzesień 2008 r.)
 
Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...
Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...
Połączenia komunikacyjne z miastem – udział kolei w przygotowaniach do EURO 2...
 
Fossil fuels powerpoint
Fossil fuels powerpointFossil fuels powerpoint
Fossil fuels powerpoint
 
El lenguaje de la vida
El lenguaje de la vidaEl lenguaje de la vida
El lenguaje de la vida
 
Sobre a cultura e a personalidade.
Sobre a cultura e a personalidade.Sobre a cultura e a personalidade.
Sobre a cultura e a personalidade.
 
Einstein e as maquinas do tempo
Einstein e as maquinas do tempoEinstein e as maquinas do tempo
Einstein e as maquinas do tempo
 
Que é a teoría da relatividade
Que é a teoría da relatividadeQue é a teoría da relatividade
Que é a teoría da relatividade
 
La clave secreta del universo
La clave secreta del universoLa clave secreta del universo
La clave secreta del universo
 
As Sete Marabillas Do Mundo Raquel Losada GarcíA
As Sete Marabillas Do Mundo Raquel Losada GarcíAAs Sete Marabillas Do Mundo Raquel Losada GarcíA
As Sete Marabillas Do Mundo Raquel Losada GarcíA
 

Similar to Scala ntnu

BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersMiles Sabin
 
Intro to scala
Intro to scalaIntro to scala
Intro to scalaJoe Zulli
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvmIsaias Barroso
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Martin Odersky
 
(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
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersMiles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksSeniorDevOnly
 
Programming picaresque
Programming picaresqueProgramming picaresque
Programming picaresqueBret McGuire
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Languageleague
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with ScalaNeelkanth Sachdeva
 

Similar to Scala ntnu (20)

Scala for curious
Scala for curiousScala for curious
Scala for curious
 
BCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java DevelopersBCS SPA 2010 - An Introduction to Scala for Java Developers
BCS SPA 2010 - An Introduction to Scala for Java Developers
 
Intro to scala
Intro to scalaIntro to scala
Intro to scala
 
Scala uma poderosa linguagem para a jvm
Scala   uma poderosa linguagem para a jvmScala   uma poderosa linguagem para a jvm
Scala uma poderosa linguagem para a jvm
 
Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009Scala Talk at FOSDEM 2009
Scala Talk at FOSDEM 2009
 
(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?
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
From Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risksFrom Java to Scala - advantages and possible risks
From Java to Scala - advantages and possible risks
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Programming picaresque
Programming picaresqueProgramming picaresque
Programming picaresque
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Java
JavaJava
Java
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 

Scala ntnu

  • 1. If I were to pick a language to use on the JVM today other than Java, it would be Scala. – James Gosling, creator of Java http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a "replacement for Java" as Scala, and the momentum behind Scala is now unquestionable. – Charlies Nutter, JRuby lead http://blog.headius.com/2009/04/future-part-one.html My tip though for the long term replacement of javac is Scala. I'm very impressed with it! I can honestly say if someone had shown me the Programming in Scala book […] back in 2003 I'd probably have never created Groovy. – James Strachan, creator of Groovy http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html
  • 2. Agenda ● Background and motivation ● Intro to Scala ● Basic syntax ● Pattern matching ● Functions ● Classes and traits ● <break /> ● Practical Scala
  • 3. About us ● Alf Kristian Støyle and Fredrik Vraalsen ● Java developers with 6 and 9 years experience ● Scala enthusiasts since 2008 ● Developed SubmitIT for JavaZone ● Alf worked on Scala projects for Kommuneforlaget ● Held 4 Scala training courses for 60+ participants
  • 4.
  • 5. public class Person { private int age; private String name; public Person(int age, String name) { this.age = age; this.name = name; } public int getAge() { return this.age; } public void setAge(int age) { this.age = age; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } } class Person(var age: Int, var name: String)
  • 6. List<Person> persons = ... List<Person> adults = new LinkedList<Person>(); List<Person> kids = new LinkedList<Person>(); for (Person person : persons) { if (person.getAge() < 18) { kids.add(person); } else { adults.add(person); } } val persons: List[Person] = ... val (kids, adults) = persons.partition(_.age < 18)
  • 7. using(new BufferedReader(new FileReader("f.txt"))) { reader => println(reader.readLine()) } BufferedReader reader = null; try { reader = new BufferedReader(new FileReader("f.txt")); System.out.println(reader.readLine()); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // Exception on close, ignore } } }
  • 8. Scala ● Object oriented and functional ● Statically typed ● Java compatible ● Compiles to Java bytecode ● Existing libraries/frameworks ● Better Java
  • 11. Variables var i = 42 i = 3 i = "Hello world!" // compile error
  • 12. Values val i = 42 i = 3 // compile error
  • 13. Methods def sum(a: Int, b: Int): Int = { return a + b }
  • 14. Methods def sum(a: Int, b: Int): Int = { a + b }
  • 15. Methods def sum(a: Int, b: Int) = { a + b }
  • 16. Methods def sum(a: Int, b: Int) = a + b
  • 18. Collections val list = List("apple", "orange", "banana") val map = Map(1 -> "one", 2 -> "two") val array = Array(1, 2, 3, 4, 5) list(1) // orange map(2) // two array(3) // 4
  • 19. myObject match { case 1 => println("First") case 2 => println("Second") case _ => println("Unknown") } Pattern matching
  • 20. myObject match { case i: Int => println("Found number " + i) case s: String => println("Found text " + s) case _ => println("Unknown") } Pattern matching
  • 21. val email = """(.+)@(.+)""".r "scala@java.no" match { case email(name, domain) => println("User " + name + " at " + domain) case x => println(x + " is not an email") } Pattern matching
  • 23. Functions Predicate<Integer> even = new Predicate<Integer>() { @Override public boolean apply(Integer i) { return i % 2 == 0; } }; List<Integer> numbers = … // 1, 2, 3, 4 Iterable<Integer> evenNums = Iterables.filter(numbers, even); => [2, 4] Google collections:
  • 24. Functions val even = (i: Int) => i % 2 == 0 val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(even) => List(2, 4) Scala collections:
  • 25. Functions val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter((i: Int) => i % 2 == 0) => List(2, 4) Scala collections:
  • 26. Functions val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(i => i % 2 == 0) => List(2, 4) Scala collections:
  • 27. Functions val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(_ % 2 == 0) => List(2, 4) Scala collections:
  • 28. Functions Predicate<Integer> even = new Predicate<Integer>() { @Override public boolean apply(Integer i) { return i % 2 == 0; } }; val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(_ % 2 == 0) => List(2, 4) Scala collections:
  • 29. “Functional programming” ● First class functions ● Pattern matching ● …
  • 30. Functional programming ● Mathematical functions have no side effects ● f(x) = 2x + 3
  • 31. In practice ● Only immutable objects (and object graphs) ● All field must be immutable ● No side-effects from method calls ● All methods must return something
  • 32. Immutable datastrukturer for (Iterator i = persons.iterator(); i.hasNext();) { Person person = (Person) i.next(); if (person.getAge() < 18) { i.remove(); } } val adults = persons.filter(_.age >= 18)
  • 33. Strive to be pure ● Concurrency ● Easier to avoid errors ● Easier to test
  • 34. Control structures return values val numbers = for (i <- 1 to 10) yield i val res1 = if (cond) x else y val res2 = try { x } catch { … y } finally { … }
  • 35. Classes and constructors class Person(val name: String, val age: Int)
  • 36. Classes and constructors class Person(val name: String, val age: Int) { def this(name: String) = this(name, 42) override def toString = "My age is " + age }
  • 37. Traits (= Interface + Mixin) ● “Multiple inheritance done right” ● Implement methods ● Initialize fields
  • 38. scala.Ordered trait trait Ordered[A] { def compare(that: A): Int def < (that: A) = this.compare(that) < 0 def > (that: A) = this.compare(that) > 0 def <= (that: A) = this.compare(that) <= 0 def >= (that: A) = this.compare(that) >= 0 }
  • 39. The Ordered trait class Person(val age: Int) extends Ordered[Person] { def compare(other: Person) = this.age - other.age } val person1 = new Person(21) val person2 = new Person(31) person1.<(person2) // true person1 <= person2 // true person1 >= person2 // false
  • 40. Just scratching the surface... ● Tuples ● Singleton objects ● Closures ● For-comprehensions ● Implicit conversions ● Actors ● Native XML data types ● Parsers
  • 41. Q & A
  • 42. val xml = <break time="30" />
  • 43. Practical Scala ● The downside ● Tools ● ScalaTest ● Java interoperability ● Learning curve ● Your choice ● Implicit conversions ● Higher order functions
  • 44. IDE support ● Netbeans - very good, but inferior in other areas ● IntelliJ IDEA - very good, but slow compilation ● Eclipse - getting better, very fast when working
  • 45. Backward compatibility ● Binary compatibility broken several times ● Think twice before using a library ● 2.7.3 – 2.7.4 ● 2.8.0 <-> 2.8.1.RC4
  • 46. Tools demo ● REPL - Read eval print loop ● Maven ● SBT - Simple Build Tool ● IntelliJ IDEA
  • 47. ScalaTest demo ● DSL for testing ● Powerful assertions ● Different test styles ● JUnit ● Functional tests ● BDD test
  • 48. The feel of Scala http://parleys.com/#id=10&st=5&sl=1
  • 49. Java interoperability demo ● Scala using Java ● Java using Scala ● Mixed projects?
  • 50. Learning curve ● Syntax small smack in the face ● Easy to write Java-ish Scala ● The language scales with your understanding ● Gradually migrate to more functional style
  • 51. What would you like to hear? ● Implicit conversion ● Why does "gnirtS.gnal.avaj".reverse work? ● Higher order functions ● How does the using/resource handling example work?
  • 53. Higher order functions ● Functions which take functions as parameters and/or return functions
  • 54. Higher order functions First class functions: val even: (Int => Boolean) = (i: Int) => i % 2 == 0 Higher order function: def test(numbers: List[Int], f: Int => Boolean) = ... Use: test(List(1, 2, 3), (i: Int) => i % 2 == 0) test(List(1, 2, 3), _ % 2 == 0)
  • 55. Higher order functions def test(numbers: List[Int], f: Int => Boolean): List[Boolean] = numbers.map(tall => f(tall))
  • 56. Higher order functions def test(numbers: List[Int], f: Int => Boolean): List[Boolean] = numbers.map(tall => f(tall)) Use: test(List(1, 2, 3), _ % 2 == 0) // List(false, true, false)
  • 57. call-by-value vs. call-by-name ● by-value: expressions are evaluated before being passed to the function ● by-name: expressions evaluated inside function ● nice when computationally expensive ● possible to create nice APIs
  • 58. call-by-value vs. call-by-name Example: Logging def thisTakesTime = { println(“Slow computation”) “result” }
  • 59. call-by-value def debug(s: String) { println(“debug”) if (logLevel <= DEBUG) println(s) } logger.debug(thisTakesTime()) // Slow computation // debug // result
  • 60. call-by-name def debug(s: => String) { println(“debug”) if (logLevel <= DEBUG) println(s) } logger.debug(thisTakesTime()) // debug // Slow computation // result
  • 61. using(new BufferedReader(new FileReader("f.txt"))) { reader => println(reader.readLine()) } BufferedReader reader = null; try { reader = new BufferedReader(new FileReader("f.txt")); System.out.println(reader.readLine()); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { // Exception on close, ignore } } }
  • 62. def using[T <: { def close() }, A] (closeable: T) (f: T => A) = { try { f(closeable) } finally { if (closeable != null) { try { closeable.close() } catch { case e: Exception => // Ignore } } } } using(new BufferedReader(new FileReader("f.txt"))) { reader => println(reader.readLine()) }
  • 64. String s = "!em esreveR"; System.out.println(s.reverse()); Magic? val s: java.lang.String = "!em esreveR" println(s.reverse) => Reverse me!
  • 65. Magic? val s: java.lang.String = "!em esreveR" println(s.reverse) println(stringWrapper(s).reverse) => Reverse me! String s = "!em esreveR"; System.out.println(s.reverse());
  • 66. Magic? val s: java.lang.String = "!em esreveR" println(s.reverse) println(augmentString(s).reverse) println(new StringOps(s).reverse) => Reverse me! String s = "!em esreveR"; System.out.println(s.reverse());
  • 67. implicit implicit def augmentString(x: String) = new StringOps(x)
  • 68. StringOps def toArray : Array[Char] def toBoolean : Boolean def toByte : Byte def toDouble : Double def toFloat : Float def toInt : Int def toLong : Long def toShort : Short def r : Regex def lines : Iterator[java.lang.String] def * (n : Int) : java.lang.String ...
  • 69. scala.Predef ● A set of implicits ● byteWrapper(x: Byte) ● shortWrapper(x: Short) ● intWrapper(x: Int) ● charWrapper(c: Char) ● longWrapper(x: Long) ● floatWrapper(x: Float) ● doubleWrapper(x: Double) ● booleanWrapper(x: Boolean) ● stringWrapper(x: String) ● stringBuilderWrapper(x : StringBuilder) ● ...
  • 70. Map and ArrowAssoc val myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two")
  • 71. Map and ArrowAssoc val myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map.apply((1, "one"), (2, "two"))
  • 72. Map and ArrowAssoc val myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map((1, "one"), (2, "two"))
  • 73. Map and ArrowAssoc val myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map((1, "one"), (2, "two")) val tuple: Tuple2[Int, String] = (1, "one")
  • 74. Map and ArrowAssoc val myMap: Map[Int, String] = Map(1 -> "one", 2 -> "two") Map(any2ArrowAssoc(1) -> "one", any2ArrowAssoc(2) -> "two") Map((1, "one"), (2, "two"))
  • 75. scala.Predef.ArrowAssoc implicit def any2ArrowAssoc[A](x: A): ArrowAssoc[A] = new ArrowAssoc(x) class ArrowAssoc[A](x: A) { def -> [B](y: B): Tuple2[A, B] = (x, y) } val myMap = Map(1 -> "one", 2 -> "two")
  • 76. Implicit rules! ● Marking Rule: Only definitions marked implicit are available. ● Scope Rule: An inserted implicit conversion must be in scope as a single identifier, or be associated with the source or target type of the conversion. ● Non-Ambiguity Rule: An implicit conversion is only inserted if there is no other possible conversion to insert. ● One-at-a-time Rule: Only one implicit is tried. ● Explicits-First Rule: Whenever code type checks as it is written, no implicits are attempted.
  • 77. Q & A
  • 78. Resources ● scala@java.no (http://lister.java.no/mailman/listinfo/scala) ● http://scala.java.no ● http://www.slideshare.net/stoyle/scala-ntnu ● http://github.com/javaBin/scala-training-slides ● fvr@knowit.no, aks@knowit.no

Editor's Notes

  1. Denne siden skal vel bort?
  2. Bruke logging eksempelet
  3. Logging?