scala is better than java!
DESCRIPTION
scala is a language that mix functional and object oriented programming. It allow you to use both declarative and programmatic style of programming. In this presentation I show an introduction of key principles that driven scala implementation. With some simple example i will show you some scala tools to became a more proficient programmer.TRANSCRIPT
Scala, un Java miglioreintroduzione al linguaggio che rende
più efficace la programmazione.
Scalable Language
Object Oriented
Object Oriented Pure
Functional
Java Virtual Machine
println(1 * 2)
val numbers = List(1, 2, 3)
numbers.foreach(n => println(n))
$ scala
scala> println(1 * 2)
2
scala> var numbers = List(1, 2, 3)
scala> numbers.foreach(n => println(n))
1
2
3
Scala conventions
return is “almost” optional
def sum(a: Int, b: Int) = return a + b
scala> def sum(a: Int, b: Int) = a + b
scala> println(sum(1, 2))
3
; is optional
def sum(a: Int, b:Int) = {
println(a);
println(b);
a + b;
}
Type inference
Scala is strongly typed
Type is inferred by scala
Type definition is “almost” optional
val a:Int = 1
def funz(a:Int) = 1
def funz(a: Int):Int = 1
Operator Overloading
Scala hasn't operators
Operators are object's functions
+ - * / are valid function names
Lenient invocation syntax
object.method(object)
object method object
object.+(object)
object + object
1.+(1)
1 + 1
Operator precedence?
1.+(1).+(2).*(2) == 8
1.+(1).+(2).*(2) == 6
Immutability rocks
Value vs Variable
A value is immutable
A variable is mutable
var a = 1
a = 2 // ok
val a = 1
a = 2 // error: reassignment to val
scala val ~= java final attribute
classes
public class Car {
private int milesDriven;
private final int age;
public Car(int age) { this.age = age; }
public void driveFor(int distance) {
milesDriven += distance;
}
public int getAge() { return age; }
public int getMiles() { return miles; }
}
class Car(val age:Int) {
private var milesDriven = 0
def driveFor(distance:Int) = milesDriven += distance
def miles = milesDriven
}
val car = new Car(2010)
car driveFor 100
println(car.age)
println(car.miles)
Class constructor?
Class costructor is class body itself!
class Person(name:String) {
var age = 0
println(name)
}
Costructors override
class Person(name:String) {
var age = 0
def this(name:String, anAge:Int) {
this(name)
age = anAge
}
}
Map
val map = Map(
“key1” -> “value2”,
“key2” -> “value2”
)
map(“key1”)
map(“key2”)
List
val list = List(1, 2, 3)
list.foreach(i => println(i))
Mutable vs Immutable Collections
:: is concatenate operator
val L = List()
val L1 = 3 :: L
val L2 = 1 :: 2 :: L1
When you want exactly one object
Many way to make singleton in java
Java Singleton simple way
class App {
private static final App instance;
public static final App getInstance() {
if(instance == null) {
instance = new App();
}
return instance;
}
}
Scala hasn't static
because Scala is object oriented
Scala allow you to define object
object App {
def doSomething() = ...
}
App.doSomething()
Object and class work togheter
class Color(name:String)
object Color {
private val colors = Map(
“red” -> new Color(“red”),
“green” -> new Color(“green”),
“blue” -> new Color(“blue”),
)
def mk(name:String) = colors(name)
}
val red = Color.mk(“red”)
var green = Color.mk(“green”)
val red = Color(“red”)
?
object Color {
...
def apply(name:String) = colors(name)
}
val red = Color(“red”)
Pattern Matching
switch(poor) {
case 1:
doSomething();
break;
case 2:
doSomethingElse();
break;
default:
inOtherCases();
}
Scala match is powerfull
def activity(day: String) {
day match {
case “sunday” => println(“sleep”)
case _ => println(“code”)
}
}
List(“monday”, “sunday”).foreach { activity }
can match int, string..
can match list!
List(1, 2) match {
case List(2, 1) => println(“!match”)
case List(1, 2) => println(“match!”)
}
can match case class!
case class?
are special classes
used in pattern matching
case class Color(name:String)
new Color(“red”)
case class Red extends Color(“red”)
case class Blue extends Color(“blue”)
def brush(color:Color) {
color match {
case Red() => print(“fire”)
case Blue() => print(“water”)
}
}
brush(Red())
extract matching values
case class Color(name:String)
val red = Color(“red”)
red match {
case Color(name) => println(name)
}
case class Color(name:String)
val red = Color(“red”)
red match {
case Color(name) => println(name)
}
case class Color(name:String)
val red = Color(“red”)
red match {
case Color(name) => println(name)
}
First class function
def f1(x) → x
def f2(x) → x * 2
def g(f, x) → f(x) + 1
g(f1, 3) → f1(3) + 1 → 4
g(f2, 3) → f2(3) + 1 → 7
def f1(x: Int) = x
def f2(x: Int) = x * 2
def g(f:Int=>Int, x:Int) = f(x) + 1
g(f1, 3) → f1(3) + 1 → 4
g(f2, 3) → f2(3) + 1 → 7
First class function over collections
def m2(i:Int) = i * 2
def p2(i:Int) = i * i
List(1, 2, 3).map(m2)
List(1, 2, 3).map(p2)
List(1, 2, 3).map(i => i * 2)
List(1, 2, 3).map(i => i * i)
List(1, 2, 3).filter(i => i % 2 == 0)
List(1, 2, 3).filter(i => i < 3)
Around method Pattern
try {
doSomething();
}
finally {
cleanup();
}
Too many open files!?
def write(path:String)(block: PrintWriter =>Unit) = {
val out = new PrintWriter(new File(path))
try {
block(out)
}
finally {
out.close
}
}
write(“/tmp/output”) {
out => out write “scala rocks!”
}
while(i < 100000) write(“/tmp/output”) {
out => out write “scala rocks!”
}
Be carefull, don't try this at home!
www.scala-lang.org