This document provides an overview of the Scala programming language. It notes that Scala is a multi-paradigm language created in 2003 by Martin Odersky that runs on the JVM. It supports both object-oriented and functional programming. Some key features highlighted include functions being treated as values, pattern matching, case classes, and high order functions. Examples are provided of common functional operations like filtering, mapping and reducing collections as well as using anonymous functions.
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
6. 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)
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