Marcelo Cure
WTF is Scala? 
 Multi-paradigm programming lang 
 Created on 2003 by Martin Odersky 
 Based on Java 
 Compiled into Java Byte Code 
 Runs under JVM 
 Integration with Java
Functional Paradigm 
 Executed evaluating expressions 
 Avoids using mutable state 
 Functions treated as values(can be 
passed as functions) 
 No side effects 
 Expressiveness 
 Less Bugs 
 Simpler 
 Less code
Difference example
List manipulation 
scala> val list = (1 to 12).toList 
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) 
scala> list.filter(_ % 2 == 0) 
res1: List[Int] = List(2, 4, 6, 8, 10, 12) 
scala> val list = (1 to 12).toList 
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) 
scala> val list2 = list.map(_ * 2) 
list2: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24) 
scala> list2.reduceLeft(_ + _) 
res2: Int = 156
FlatMaps 
scala> val fruits = Seq("apple", "banana", "orange") 
fruits: Seq[String] = List(apple, banana, orange) 
scala> fruits.flatMap(_.toUpperCase) 
res13: Seq[Char] = List(A, P, P, L, E, B, A, N, A, N, A, O, R, A, N, G, E)
Anonymous Functions 
scala> var sum = (x:Int,y:Int) => x + y 
sum: (Int, Int) => Int = <function2> 
scala> sum(1,2) 
res3: Int = 3
Pattern Matching 
scala> def toYesOrNo(choice: Int): String = choice match { 
| case 1 => "yes" 
| case 0 => "no" 
| case _ => "error" 
| } 
toYesOrNo: (choice: Int)String 
scala> println(toYesOrNo(1)) 
yes 
scala> println(toYesOrNo(0)) 
no 
scala> println(toYesOrNo(3)) 
error
Case Class 
 Plain and immutable data-holding 
objects 
 Depends exclusively on its constructor 
arguments. 
scala> case class Node(left: Int, right: Int) 
defined class Node 
scala> var node1 = Node(1,2) 
node1: Node = Node(1,2)
High Order Functions 
 Takes other functions as parameter 
 Return functions 
scala> def apply(f: Int => String, v: Int) = f(v) 
apply: (f: Int => String, v: Int)String 
scala> def layout[A](x: A) = "[" + x.toString() + "]" 
layout: [A](x: A)String 
scala> println( apply( layout, 10) ) 
[10]
Web Frameworks 
 Lift Framework 
 Play framework 
 Bowler framework
End

Scala

  • 1.
  • 2.
    WTF is Scala?  Multi-paradigm programming lang  Created on 2003 by Martin Odersky  Based on Java  Compiled into Java Byte Code  Runs under JVM  Integration with Java
  • 3.
    Functional Paradigm Executed evaluating expressions  Avoids using mutable state  Functions treated as values(can be passed as functions)  No side effects  Expressiveness  Less Bugs  Simpler  Less code
  • 4.
  • 5.
    List manipulation scala>val list = (1 to 12).toList list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) scala> list.filter(_ % 2 == 0) res1: List[Int] = List(2, 4, 6, 8, 10, 12) scala> val list = (1 to 12).toList list: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) scala> val list2 = list.map(_ * 2) list2: List[Int] = List(2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24) scala> list2.reduceLeft(_ + _) res2: Int = 156
  • 6.
    FlatMaps scala> valfruits = Seq("apple", "banana", "orange") fruits: Seq[String] = List(apple, banana, orange) scala> fruits.flatMap(_.toUpperCase) res13: Seq[Char] = List(A, P, P, L, E, B, A, N, A, N, A, O, R, A, N, G, E)
  • 7.
    Anonymous Functions scala>var sum = (x:Int,y:Int) => x + y sum: (Int, Int) => Int = <function2> scala> sum(1,2) res3: Int = 3
  • 8.
    Pattern Matching scala>def toYesOrNo(choice: Int): String = choice match { | case 1 => "yes" | case 0 => "no" | case _ => "error" | } toYesOrNo: (choice: Int)String scala> println(toYesOrNo(1)) yes scala> println(toYesOrNo(0)) no scala> println(toYesOrNo(3)) error
  • 9.
    Case Class Plain and immutable data-holding objects  Depends exclusively on its constructor arguments. scala> case class Node(left: Int, right: Int) defined class Node scala> var node1 = Node(1,2) node1: Node = Node(1,2)
  • 10.
    High Order Functions  Takes other functions as parameter  Return functions scala> def apply(f: Int => String, v: Int) = f(v) apply: (f: Int => String, v: Int)String scala> def layout[A](x: A) = "[" + x.toString() + "]" layout: [A](x: A)String scala> println( apply( layout, 10) ) [10]
  • 11.
    Web Frameworks Lift Framework  Play framework  Bowler framework
  • 12.