Scala

690 views

Published on

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
690
On SlideShare
0
From Embeds
0
Number of Embeds
236
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Scala

  1. 1. Introduction to Scala Suraj Atreya surajatreyac@gmail.com @suraj2585
  2. 2. First bits• Scalable language pronounced as “Sca-lah”• Developed by Martin Odersky in 2001• He is also famous for developing javac• Currently owns his own company called Typesafe
  3. 3. Motivation Java Code Scala Codepublic class DemoAdd { object DemoAdd {public static void main(String[] args) { def main(args: Array[String]): Unit = { int sum = 0; println((1 to 1000).reduceLeft( _ + _ )) for(int i = 1; i <= 1000; ++i){ } sum = sum + i; } } System.out.println(sum); }}
  4. 4. Motivation Java Code Scala Codepublic class DemoList { object DemoList {public static void main(String[] args) { def main(args:Array[String]):Unit = { List<String> ls = new val ls = List("One", "Two", "Three", "Four") ArrayList<String>(); for(xs <- ls) println(xs) ls.add(“One"); } ls.add(“Two"); } ls.add(“Three"); Iterator it = ls.iterator(); while(it.hasNext()){ String value=(String)it.next(); System.out.println(value); } }}
  5. 5. Motivation• Code size is much smaller compared to Java• Less verbose• Functional Programming(FP) + Object oriented• Statically typed• JVM based• .NET (CLR) support• Scalable – from small to large distributed systems
  6. 6. Motivation• Seamless integration with Java• Hot language • Facebook, Twitter, LinkedIn, Foursquare
  7. 7. IDE• http://www.scala-lang.org/• Eclipse plugin (http://www.eclipse.org)• IntelliJ (http://www.jetbrains.com/idea/)
  8. 8. Classes and Objects class DemoClass { var name:String = "" } object Demo{ def main (args:Array[String]):Unit = { val hello = new DemoClass hello name = "Hello" println(hello name) } }
  9. 9. Classes and Objectspublic class Person{ class Person(age: Int, name: String) { override def toString = age + " " + name private int m_age = 0; } private String m_name = ""; object DemoConstr { Person(int age, String name){ m_age = age; def main(args: Array[String]): Unit = { m_name = name; val person = new Person(10, "John") } println(person) } public static void main(String[] args) { } Person person = new Person(10, "John"); System.out.println(person.m_age + " " + person.m_name); }}
  10. 10. Functional Programming (FP)• Learning FP is challenging• Functions are first-class citizens• In FP, constraint is “no state” and “immutable” – Other words FP does not allow “side effects” – Reassigning a variable – Modifying DS in place• FP allows programming using “pure functions”
  11. 11. FP Pure Functionsval x = "Hello, World" x: java.lang.String = Hello, Worldval r1 = x.reverse r1: String = dlroW ,olleHval r2 = x.reverse r2: String = dlroW ,olleH x is referentially transparent After substitution, the outputSubstitute “x” with its value remains same – “no side effects”scala> val r1 = "Hello, World".reverse r1: String = dlroW ,olleHval r2 = "Hello, World".reverse r2: String = dlroW ,olleH
  12. 12. FP Pure Functionsval x = new StringBuilder("Hello") x: java.lang.StringBuilder = Helloval y = x.append(", World") y: java.lang.StringBuilder = Hello, Worldval r1 = y.toString r1: java.lang.String = Hello, Worldval r2 = y.toString r2: java.lang.String = Hello, World
  13. 13. FP Pure Functionsval x = new StringBuilder("Hello") x: java.lang.StringBuilder = Helloval r1 = x.append(", World").toString r1: java.lang.String = Hello, Worldval r2 = x.append(", World").toString r2: java.lang.String = Hello, World, World x is not referentially transparent After substitution, the output Is different– “side effects”
  14. 14. FPobject NumOfElems{ def length[A](ls: List[A]): Int = ls match { case Nil => 0 case _ :: tail => 1 + length(tail) case _ => throw new NoSuchElementException } def main(args: Array[String]): Unit = { println(List(1, 2, 4, 5, 98, 23, 53).length) println(length(List(1, 2, 4, 5, 98, 23, 53))) }}
  15. 15. FP 1 + length(List(2, 4, 5, 98, 23, 53)) 1 + (1 + length(List(4, 5, 98, 23, 53))) 1 + (1 + (1 + length(List(5, 98, 23, 53)))) 1 + (1 + (1 + (1 + length(List(98, 23, 53))))) 1 + (1 + (1 + (1 + (1 + length(List(23, 53)))))) 1 + (1 + (1 + (1 + (1 + (1 + length(List(53))))))) 1 + (1 + (1 + (1 + (1 + (1 + (1 + length(List()))))))) 1 + (1 + (1 + (1 + (1 + (1 + (1 + 0))))))) 1 + (1 + (1 + (1 + (1 + (1 + 1))))) 1 + (1 + (1 + (1 + (1 + 2)))) 1 + (1 + (1 + (1 + 3))) 1 + (1 + (1 + 4)) 1 + (1 + 5) 1+6 7
  16. 16. FP• This works for small lists, but what if you have a very large list ?!• Don’t give up on “Recursion”, we have a solution for that• Remember, “Recursion is the bread and butter of FP”
  17. 17. FP - Tail call optimisationdef length[A](ls: List[A]): Int = ls match { def lengthTR[A](ls: List[A]):Int = { case Nil => 0 case _ :: tail => 1 + length(tail) def loop(ls:List[A], acc:Int):Int = ls match{ case _ => throw new NoSuchElementException case Nil => acc } case _ :: tail => loop(tail, acc + 1) } loop(ls,0) }
  18. 18. FP - Tail call optimisationdef lengthTR[A](ls: List[A]):Int = { loop(List(2, 4, 5, 98, 23, 53), 0 + 1) loop(List(4, 5, 98, 23, 53), 1 + 1) def loop(ls:List[A], acc:Int):Int = ls match{ case Nil => acc loop(List(5, 98, 23, 53), 2 + 1) case _ :: tail => loop(tail, acc + 1) } loop(List(98, 23, 53), 3 + 1) loop(ls,0) loop(List(23, 53), 4 + 1) } loop(List(53), 5 + 1) loop(List(), 6 + 1) 7
  19. 19. Pattern Matching object DemoRegEx { def main(args:Array[String]):Unit = { val Date = """(dd)/(dd)/(dddd)""".r val date = "01/02/2013" val Date(day, month, year) = date println(day) println(month) println(year) } }
  20. 20. Final thoughts• Scala is great for general purpose computing• Scales well and has an elegant syntax• Neat integration with Java• Great RegEx support• Actors replaces Threads in Java for concurrency
  21. 21. E0F

×