Introduction To Scala

742 views

Published on

Introduction To Scala for Java Programmers

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

  • Be the first to like this

No Downloads
Views
Total views
742
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Introduction To Scala

  1. 1. scala> “Hello Scala”Scala stands for “scalable language.” The language is so namedbecause it was designed to grow with the demands of its users.
  2. 2. History● The design of Scala started in 2001 at the (EPFL) by Martin Odersky, Odersky had previously worked on Generic Java and javac, Suns 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
  3. 3. Scala Adopters● Twitter● FourSqaure● LinkedIn● Sony● OPOWER● Siemens
  4. 4. Features● Scala is OO● Scala is Functional● Strongly Typed – even more so than Java● Type Invariance● Implicits – Analogous to Rubys open classes● Duck Typing – anything that quacks is a duck● String Interpolation
  5. 5. Hello Scalaobject Greeting { def main(args: Array[String]) { println("Hello World !!!") }}● Look maa no semicolon● Object ?
  6. 6. Variablesobject Greeter { def main(args:Array[String]) { val message = “Hello” // final equivalent message = “Bye !!” // wont work var num = 2 num = 3 // will work }}
  7. 7. Classes● class Person(val name:String, val id:Int)● Is equivalent to Javas class Person { private String name; private Integer id; …. }● val p = new Person(“John”, 1)
  8. 8. Functionsdef 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. 9. Tuples are DTOs – 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. 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
  11. 11. Commonly used methods● val l = List(1,2,3)● l.head / l.tail● l.foreach(println)● l.map(_ * 2)● l.filter(_ > 1) / l.filter(_ < 2)● l.exists(_ < 0)● l.reverse● l.take / l.drop● ….
  12. 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. 13. Case classes● case class Person(name:String, id:Integer)● val p = Person(“Martin”, 2) // no new needed● It automatically adds getters, equals and hashcode● Case classes are used for pattern matching
  14. 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. 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 :)

×