Scala, un Java migliore
    introduzione 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; }
   ...
class Car(val age:Int) {
    private var milesDriven = 0
    def driveFor(distance:Int) = milesDriven += distance
    def ...
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...
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”...
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:
    ...
Scala match is powerfull




                
def activity(day: String) {
    day match {
     case “sunday” => println(“sleep”)
     case _ => println(“code”)
    }
}
...
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 {
     ...
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) ...
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(o...
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




             
Luca Marrocco
                 luca.marrocco@gmail.com
                  twitter.com/lucamarrocco




Giovanni Di Mingo
gi...
Upcoming SlideShare
Loading in...5
×

Scala is better than java!

2,946

Published on

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.

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,946
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
56
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "Scala is better than java!"

  1. 1. Scala, un Java migliore introduzione al linguaggio che rende più efficace la programmazione.    
  2. 2. Scalable Language    
  3. 3. Object Oriented    
  4. 4. Object Oriented Pure    
  5. 5. Functional    
  6. 6. Java Virtual Machine    
  7. 7. println(1 * 2) val numbers = List(1, 2, 3) numbers.foreach(n => println(n))    
  8. 8. $ scala scala> println(1 * 2) 2 scala> var numbers = List(1, 2, 3) scala> numbers.foreach(n => println(n)) 1 2 3    
  9. 9. Scala conventions    
  10. 10. return is “almost” optional    
  11. 11. def sum(a: Int, b: Int) = return a + b    
  12. 12. scala> def sum(a: Int, b: Int) = a + b scala> println(sum(1, 2)) 3    
  13. 13. ; is optional    
  14. 14. def sum(a: Int, b:Int) = { println(a); println(b); a + b; }    
  15. 15. Type inference    
  16. 16. Scala is strongly typed    
  17. 17. Type is inferred by scala    
  18. 18. Type definition is “almost” optional    
  19. 19. val a:Int = 1    
  20. 20. def funz(a:Int) = 1    
  21. 21. def funz(a: Int):Int = 1    
  22. 22. Operator Overloading    
  23. 23. Scala hasn't operators    
  24. 24. Operators are object's functions    
  25. 25. + - * / are valid function names    
  26. 26. Lenient invocation syntax    
  27. 27. object.method(object) object method object    
  28. 28. object.+(object) object + object    
  29. 29. 1.+(1) 1+1    
  30. 30. Operator precedence?    
  31. 31. 1.+(1).+(2).*(2) == 8    
  32. 32. 1.+(1).+(2).*(2) == 6    
  33. 33. Immutability rocks    
  34. 34. Value vs Variable    
  35. 35. A value is immutable    
  36. 36. A variable is mutable    
  37. 37. var a = 1 a = 2 // ok    
  38. 38. val a = 1 a = 2 // error: reassignment to val    
  39. 39. scala val ~= java final attribute    
  40. 40. classes    
  41. 41. 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; } }    
  42. 42. class Car(val age:Int) { private var milesDriven = 0 def driveFor(distance:Int) = milesDriven += distance def miles = milesDriven }    
  43. 43. val car = new Car(2010) car driveFor 100 println(car.age) println(car.miles)    
  44. 44. Class constructor?    
  45. 45. Class costructor is class body itself!    
  46. 46. class Person(name:String) { var age = 0 println(name) }    
  47. 47. Costructors override    
  48. 48. class Person(name:String) { var age = 0 def this(name:String, anAge:Int) { this(name) age = anAge } }    
  49. 49. Map    
  50. 50. val map = Map( “key1” -> “value2”, “key2” -> “value2” ) map(“key1”) map(“key2”)    
  51. 51. List    
  52. 52. val list = List(1, 2, 3) list.foreach(i => println(i))    
  53. 53. Mutable vs Immutable Collections    
  54. 54. :: is concatenate operator    
  55. 55. val L = List() val L1 = 3 :: L val L2 = 1 :: 2 :: L1    
  56. 56. When you want exactly one object    
  57. 57. Many way to make singleton in java    
  58. 58. Java Singleton simple way    
  59. 59. class App { private static final App instance; public static final App getInstance() { if(instance == null) { instance = new App(); } return instance; } }    
  60. 60. Scala hasn't static    
  61. 61. because Scala is object oriented    
  62. 62. Scala allow you to define object    
  63. 63. object App { def doSomething() = ... } App.doSomething()    
  64. 64. Object and class work togheter    
  65. 65. 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) }    
  66. 66. val red = Color.mk(“red”) var green = Color.mk(“green”)    
  67. 67. val red = Color(“red”) ?    
  68. 68. object Color { ... def apply(name:String) = colors(name) } val red = Color(“red”)    
  69. 69. Pattern Matching    
  70. 70. switch(poor) { case 1: doSomething(); break; case 2: doSomethingElse(); break; default: inOtherCases(); }    
  71. 71. Scala match is powerfull    
  72. 72. def activity(day: String) { day match { case “sunday” => println(“sleep”) case _ => println(“code”) } } List(“monday”, “sunday”).foreach { activity }    
  73. 73. can match int, string..    
  74. 74. can match list!    
  75. 75. List(1, 2) match { case List(2, 1) => println(“!match”) case List(1, 2) => println(“match!”) }    
  76. 76. can match case class!    
  77. 77. case class?    
  78. 78. are special classes used in pattern matching    
  79. 79. case class Color(name:String) new Color(“red”)    
  80. 80. 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())    
  81. 81. extract matching values    
  82. 82. case class Color(name:String) val red = Color(“red”) red match { case Color(name) => println(name) }    
  83. 83. case class Color(name:String) val red = Color(“red”) red match { case Color(name) => println(name) }    
  84. 84. case class Color(name:String) val red = Color(“red”) red match { case Color(name) => println(name) }    
  85. 85. First class function    
  86. 86. 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    
  87. 87. 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    
  88. 88. First class function over collections    
  89. 89. def m2(i:Int) = i * 2 def p2(i:Int) = i * i List(1, 2, 3).map(m2) List(1, 2, 3).map(p2)    
  90. 90. List(1, 2, 3).map(i => i * 2) List(1, 2, 3).map(i => i * i)    
  91. 91. List(1, 2, 3).filter(i => i % 2 == 0) List(1, 2, 3).filter(i => i < 3)    
  92. 92. Around method Pattern    
  93. 93. try { doSomething(); } finally { cleanup(); }    
  94. 94. Too many open files!?    
  95. 95. def write(path:String)(block: PrintWriter =>Unit) = { val out = new PrintWriter(new File(path)) try { block(out) } finally { out.close } }    
  96. 96. write(“/tmp/output”) { out => out write “scala rocks!” }    
  97. 97. while(i < 100000) write(“/tmp/output”) { out => out write “scala rocks!” } Be carefull, don't try this at home!    
  98. 98. www.scala-lang.org    
  99. 99. Luca Marrocco luca.marrocco@gmail.com twitter.com/lucamarrocco Giovanni Di Mingo giovanni@dimingo.com twitter.com/pino_otto    
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×