Why Teams call analytics are critical to your entire business
Introduction To Scala
1. scala> “Hello Scala”
Scala stands for “scalable language.” The
language is so named
because it was designed to grow with the
demands of its users.
2. History
● The design of Scala started in 2001 at the (EPFL) by Martin
Odersky, Odersky had previously worked on Generic Java
and javac, Sun's Java compiler
● Scala was released late 2003/early 2004 on the Java
platform
● Recent Scala release 2.10.0
● On 12 May 2011, Odersky and collaborators launched
Typesafe Inc., a company to provide commercial support,
training, and services for Scala
4. Features
● Scala is OO
● Scala is Functional
● Strongly Typed – even more so than Java
● Type Invariance
● Implicits – Analogous to Ruby's open classes
● Duck Typing – anything that quacks is a duck
● String Interpolation
5. Hello Scala
object Greeting {
def main(args: Array[String]) {
println("Hello World !!!")
}
}
● Look maa no semicolon
● Object ?
6. Variables
object Greeter {
def main(args:Array[String]) {
val message = “Hello” // final equivalent
message = “Bye !!” // won't work
var num = 2
num = 3 // will work
}
}
7. Classes
● class Person(val name:String, val id:Int)
● Is equivalent to Java's
class Person {
private String name;
private Integer id;
….
}
● val p = new Person(“John”, 1)
8. Functions
def calculate(msg:String, count:Int) : String =
msg.take(count)
● def implies a function declaration
● msg and count are parameters
● : String implies the return type of the function
● In Scala everything returns a value
● Even assignment x = 1 returns a special value called
Unit (A feature coming from dynamic languages like
Ruby)
9. Tuples are DTO's – Ted Neward
● Returning multiple values from a function
● def calculate() : Tuple2[Int, String] = (2, "Ted")
● val v = calculate // Scala convention no parenthesis
● Accessing each of the values
– v._1 : refers to the first return value
– v._2 : refers to the second value
● Scala has had a big impact from dynamic languages
like Ruby
10. Collections
● List(1,2,3) // no need to pass the type info
● Set(1,2,2)
● Map(1-> ”One”, 2-> ”Two”, 3-> ”Three”)
● Ranges: 1 to 10 / 1 until 10 by 2
● Array(1,2,3)
● Default collections types are immutable
12. Higher Order Functions
● Functions taking functions are called higher
order functions
● Cannot pass functions in Java not until
Lambdas in Java8
● def foo(f : (String => Boolean)) : String = if (f("Ted")) "Cool" else "Not Cool !!"
● l.filter / l.foreach / l.map are all examples
● This is a standard feature of all Scala libraries
13. Case classes
● case class Person(name:String, id:Integer)
● val p = Person(“Martin”, 2) // no new needed
● It automatically adds getter's, equals and
hashcode
● Case classes are used for pattern matching
14. Pattern Matching
● Switch with steroids :)
List(1,2,3) match {
case Nil => "Empty"
case x if x.length == 3 => "Blaah ..."
case _ =>
}
● Match also returns a value
● You can have conditional expressions as well
● It works for all data types
15. Excited? Any Questions?
● Resources
– Scala lang
– Programming in Scala 2nd Edition
– Typesafe Inc.
– IDE Support (Intellij & Eclipse Scala IDE)
– Pune Scala User Group
● Follow me on twitter for live Scala feeds :)