SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
2.
What is Scala “ a general purpose programming language designed to express common programming patterns in a concise , elegant , and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive .” http://www.scala-lang.org
3.
Object-oriented <ul><li>Everything is an object </li></ul><ul><ul><li>No “primitives” </li></ul></ul><ul><li>Classes </li></ul><ul><ul><li>Same as Java, but concise </li></ul></ul><ul><li>Traits </li></ul><ul><ul><li>Interfaces done right </li></ul></ul><ul><li>Singletons </li></ul><ul><ul><li>Language-level concept </li></ul></ul>
4.
Statically typed <ul><li>Rich type system (by Java’s standard) </li></ul><ul><ul><li>Higher-kinded types </li></ul></ul><ul><ul><li>Implicit conversions </li></ul></ul><ul><ul><li>Type evidence </li></ul></ul><ul><li>Expressive type system </li></ul><ul><li>Inferred types </li></ul>
5.
Functional Programming <ul><li>Functions as values </li></ul><ul><ul><li>May be </li></ul></ul><ul><ul><ul><li>Saved </li></ul></ul></ul><ul><ul><ul><li>Passed to other functions (higher-order functions) </li></ul></ul></ul><ul><ul><ul><li> No need to write ugly anonymous classes </li></ul></ul></ul><ul><ul><li>Advanced pattern matching </li></ul></ul><ul><ul><li>Expressions return a value </li></ul></ul><ul><ul><ul><li>if/else, try/catch, match, … </li></ul></ul></ul><ul><li>Promotes immutability </li></ul><ul><ul><li>But Scala doesn’t force it </li></ul></ul>
6.
Java Interoperability <ul><li>Compiles to Java byte code </li></ul><ul><ul><li>Jars, wars, … </li></ul></ul><ul><ul><li>No special operational change </li></ul></ul><ul><li>Scala calling Java, Java calling Scala code is fine </li></ul><ul><li>Whole Java eco-system at your service </li></ul><ul><li>You can write apps using Scala and Spring, today </li></ul>
7.
Hello World: Scripting Style $ scala hello-script.scala Hello World No compilation
8.
Hello World: Porting of Java Code $ scalac hello-java.scala $ scala example.Main Hello World ‘ static’ Inferred semicolons
9.
Hello World: Using the App trait $ scalac hello-app.scala $ scala example.Main Hello World
10.
Simple Class class Person val p = new Person Type Inferred Default access: public No curly braces needed (but allowed)
11.
Simple Class class Person val p: Person = new Person Explicit type specification
12.
Class with constructor class Person(firstName: String, lastName: String) val p = new Person( "Ramnivas" , "Laddad" ) println(p.firstName) // Error Primary constructor Fields – accessible in class body
13.
Class with “getters” class Person( val firstName: String, val lastName: String) val p = new Person( "Ramnivas" , "Laddad" ) println(p.firstName) Value (Java ‘final’)
14.
Class with “getters” and “setters” class Person( var firstName: String, var lastName: String) val p = new Person( "Ramnivas" , "Laddad" ) println(p.firstName) p.firstName = "Ramnivas2” Variable (Java non-final)
15.
Extending a class val s = new Student( "Ramnivas" , "Laddad" , 1) println(s.firstName) println(s.grade)
16.
Defining methods class Person( val firstName: String, val lastName: String) { def name = firstName + " " + lastName override def toString = name } val p = new Person( "Ramnivas" , "Laddad" ) println(p.name) // Ramnivas Laddad println(p) // Ramnivas Laddad Not optional
17.
Uniform access principle class Person( val firstName: String, val lastName: String) { val name = firstName + " " + lastName override def toString = name } val p = new Person( "Ramnivas" , "Laddad" ) println(p.name) // Ramnivas Laddad println(p) // Ramnivas Laddad
18.
Names in Scala <ul><li>Class, method, field names can contain non alpha-numeric characters </li></ul><ul><ul><li>:: </li></ul></ul><ul><ul><li>::: </li></ul></ul><ul><ul><li>~> </li></ul></ul><ul><ul><li>f@#: </li></ul></ul><ul><li>Valuable if used judiciously </li></ul><ul><ul><li>DSLs </li></ul></ul>
19.
More about methods and fields <ul><li>Declaring abstract methods and fields </li></ul><ul><ul><li>Just don’t provide definition </li></ul></ul><ul><ul><li>def learn(subject: String) </li></ul></ul><ul><ul><li>val knowledge </li></ul></ul><ul><li>Classes with abstract method must be declared abstract </li></ul><ul><ul><li>Just as in Java </li></ul></ul><ul><li>Methods can be defined inside methods </li></ul><ul><li>Methods may be marked @tailrec to check for tail recursiveness </li></ul>
20.
Finer access control levels <ul><li>Default access level: public </li></ul><ul><li>Protected: protected </li></ul><ul><ul><li>Same as Java </li></ul></ul><ul><li>Private: </li></ul><ul><ul><li>private </li></ul></ul><ul><ul><li>private [ this ] Access only from this instance </li></ul></ul><ul><ul><li>private [package-name] Access from package and its subpackages </li></ul></ul>
21.
Traits: Interfaces done right trait PartyGoer { val age: Int val yearsUntilLegalDrinking = if (age >= 18) 0 else 18-age }
22.
Collections val people = List( "John" , "Jacob" , "Mike" )
23.
Collections val people = Array( "John" , "Jacob" , "Mike" )
24.
Working with collections: for comprehension for (person <- people) { println(person) }
25.
Working with collections: for comprehension for (person <- people if person startsWith "J" ) { println( """Lucky one to start name in "J" """ + person) } // You are lucky one to start name in "J" John // You are lucky one to start name in "J" Jacob
26.
Working with collections: filter val student1 = new Student( "first1" , "last1" , 1) val student2 = new Student( "first2" , "last2" , 1) val student3 = new Student( "first3" , "last3" , 2) val student4 = new Student( "first4" , "last4" , 6) val students = List(student1, student2, student3, student4)
27.
Working with collections: filter val student1 = new Student( "first1" , "last1" , 1) val student2 = new Student( "first2" , "last2" , 1) val student3 = new Student( "first3" , "last3" , 2) val student4 = new Student( "first4" , "last4" , 6) val students = List(student1, student2, student3, student4)
28.
Working with collections: using _ val student1 = new Student( "first1" , "last1" , 1) val student2 = new Student( "first2" , "last2" , 1) val student3 = new Student( "first3" , "last3" , 2) val student4 = new Student( "first4" , "last4" , 6) val students = List(student1, student2, student3, student4)
29.
Working with collections: passing method as function val student1 = new Student( "first1" , "last1" , 1) val student2 = new Student( "first2" , "last2" , 1) val student3 = new Student( "first3" , "last3" , 2) val student4 = new Student( "first4" , "last4" , 6) val students = List(student1, student2, student3, student4)
30.
Working with collections: partition val student1 = new Student( "first1" , "last1" , 1) val student2 = new Student( "first2" , "last2" , 1) val student3 = new Student( "first3" , "last3" , 2) val student4 = new Student( "first4" , "last4" , 6) val students = List(student1, student2, student3, student4) val (elementarySchoolers, middleSchoolers) = students .partition(_.grade < 6) println( elementarySchoolers ) println( middleSchoolers ) //List(first1 last1, first2 last2, first3 last3) //List(first4 last4) Tuple
31.
Working with collections: transforming val student1 = new Student( "first1" , "last1" , 1) val student2 = new Student( "first2" , "last2" , 1) val student3 = new Student( "first3" , "last3" , 2) val student4 = new Student( "first4" , "last4" , 6) val students = List(student1, student2, student3, student4)
32.
Map println(studentSchools(student1)) // Miller
33.
More collections <ul><li>Set </li></ul><ul><li>IndexedSeq </li></ul><ul><li>Vector (good one!) </li></ul><ul><li>Range </li></ul><ul><ul><li>1 to 100 </li></ul></ul><ul><ul><li>1 until 100 </li></ul></ul><ul><ul><li>2 until 100 by 2 </li></ul></ul><ul><li>… </li></ul><ul><li>Mutable versions </li></ul><ul><li>Parallel versions </li></ul>
35.
Putting it together: Quicksort println(quicksort(List(1, 3, 4, 5, 1)))
36.
Pattern matching: Basics val a:Any = "foo" a match { case str: String => println( "A string: " + str) case i: Int => println( "An int: " + i) case _ => println( "Something else" ) }
37.
Pattern matching: with collections val l = List( "a" , "b" , "c" ) l match { case Nil => println( "Empty!" ) case head :: Nil => println( "Only one item " + head) case head :: tail => println( "Item " + head + " followed by " + tail) }
38.
Quicksort with pattern matching def quicksort[T](input: Traversable[T]) ( implicit ordering: Ordering[T]) : Traversable[T] = input match { case head :: tail => val (low, high) = tail.partition(ordering.lt(_, head)) quicksort(low) ++ List(head) ++ quicksort(high) case _ => input } println(quicksort(List(1, 3, 4, 5, 1)))
39.
Destutter using Pattern matching def destutter[A](lst: List[A]): List[A] = lst match { case h1 :: h2 :: tail if (h1 == h2) => destutter(h2 :: tail) case h1 :: h2 :: tail => h1 :: destutter(h2 :: tail) case _ => lst } // destutter(List(1,1,1,1,1,1)) => List(1) // destutter(List(1,1,4,3,3,2)) => List(1,4,3,2) // destutter(List() )=> List()
40.
Case classes <ul><li>Useful in pattern matching </li></ul><ul><ul><li>“ case” </li></ul></ul><ul><li>Offer many useful common methods </li></ul><ul><ul><li>equals() </li></ul></ul><ul><ul><li>hashCode() </li></ul></ul><ul><ul><li>toString </li></ul></ul><ul><ul><li>copy() </li></ul></ul>
41.
Case classes case class Human(name: String) case class SuperHero(name: String, power: String) val characters = List(Human( "Programmer" ), SuperHero( "Customer" , "money" ), SuperHero( "QA" , "testing" ))
42.
Case classes and pattern matching val actions = for (character <- characters) yield character match { case Human(name) => name + " needs to be saved" case SuperHero(name, power) => name + " will save using " + power } actions.foreach(println)
43.
Pattern matching and extracting just enough val actions = for (character <- characters) yield character match { case Human(name) => name + " needs to be saved" case SuperHero(_, power) => "Could be saved using " + power } actions.foreach(println) // Programmer needs to be saved // Could be saved using money // Could be saved using testing
44.
Regular expressions val text = "Ramnivas Laddad" val Name = """(+)+(+)""" .r val person = text match { case Name(first, last) => Some( new Person(first, last)) case _ => None } println(person) // Some(Ramnivas Laddad)
45.
Options val texts = List( "Ramnivas Laddad" , "foo" , "Scott Andrews" ) val peopleOptions = texts.map { _ match { case Name(first, last) => Some( new Person(first, last)) case _ => None } } println(peopleOptions) // List(Some(Ramnivas Laddad), None, Some(Scott Andrews))
46.
Options: flattening val texts = List( "Ramnivas Laddad" , "foo" , "Scott Andrews" ) val peopleOptions = texts.map { _ match { case Name(first, last) => Some( new Person(first, last)) case _ => None } } println(peopleOptions.flatten) // List(Ramnivas Laddad, Scott Andrews)
47.
Options: flatMap val texts = List( "Ramnivas Laddad" , "foo" , "Scott Andrews" ) val people = texts.flatMap { _ match { case Name(first, last) => Some( new Person(first, last)) case _ => None } } println(people) // List(Ramnivas Laddad, Scott Andrews)
48.
Higher order functions def process() : Unit = { retry(5) { ... } } def retry[T](maxRetry: Int)(thunk: => T) = { def retry(thunk: => T, attempt: Int): T = { try { thunk } catch { case ex if (attempt < maxRetry) => retry(thunk, attempt + 1) } } retry(thunk, 0) } Thunk
53.
Transaction management implementation <ul><li>abstract class TransactionManagement( val txManager: PlatformTransactionManager) { </li></ul><ul><li>def transactional[T](...)(thunk: => T) : T = { </li></ul><ul><li>val txAttribute = new TransactionAttributeWithRollbackRules(...) </li></ul><ul><li>val status = txManager.getTransaction(txAttribute) </li></ul><ul><li>try { </li></ul><ul><li>val ret = thunk </li></ul><ul><li>txManager.commit(status) </li></ul><ul><li>ret </li></ul><ul><li>} catch { </li></ul><ul><li>case ex => { </li></ul><ul><li>if (txAttribute.rollbackOn(ex)) { </li></ul><ul><li>txManager.rollback(status) </li></ul><ul><li>} else { </li></ul><ul><li>txManager.commit(status) </li></ul><ul><li>} </li></ul><ul><li>throw ex </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
54.
There is more… a lot more <ul><li>Methods/functions </li></ul><ul><ul><li>Default parameters </li></ul></ul><ul><ul><li>Named parameters </li></ul></ul><ul><ul><li>Curried parameters </li></ul></ul><ul><ul><li>Partial functions </li></ul></ul><ul><li>Type system </li></ul><ul><ul><li>Higher-kinded types </li></ul></ul><ul><ul><li>Bounded types </li></ul></ul><ul><ul><li>Implicit type conversion </li></ul></ul><ul><ul><li>Type parameter evidence </li></ul></ul><ul><ul><li>Type aliasing </li></ul></ul><ul><li>Lazy values </li></ul><ul><li>Partial imports </li></ul><ul><li>Actors </li></ul><ul><li>Extractors </li></ul><ul><li>Scala ecosystem </li></ul><ul><li>Combinator/parser </li></ul><ul><li>Continuations </li></ul><ul><li>Compiler plugin </li></ul><ul><li>… </li></ul>
55.
Learning Scala <ul><li>Read a Scala book </li></ul><ul><ul><li>Get the whole picture </li></ul></ul><ul><li>May feel complex at first </li></ul><ul><ul><li>Java-style Scala may serve best during initial exploration </li></ul></ul><ul><ul><li>Over time you will appreciate its simplicity </li></ul></ul><ul><li>Learn Haskell first! </li></ul><ul><ul><li>Might help to break from the Java way of thinking </li></ul></ul>Be ready to be humbled