By : Adi Baron, “Tikal”  For Java Programmers
"Which Programming Language would you use  *now* on top of JVM, except Java?" “ Scala”
Since   2003 Runs on   JVM Pragmatic High Level Statically  Typed Seamless  Java   interoperability Scalable EPFL Object O...
A language that grows on you <ul><li>To the Point </li></ul><ul><li>Not cluttered </li></ul><ul><li>Not syntax </li></ul>v...
Growing new types import  java.math.BigInteger; public  BigInteger  factorial (BigInteger x) { if  (x == BigInteger. ZERO ...
Growing new control constructs <ul><li>Scala Actors </li></ul><ul><ul><li>Implemented on top of threads </li></ul></ul><ul...
Growing new control constructs val  checksum = actor { var  sum = 0 loop { receive { case   Data (bytes)  => sum += hash(b...
High-Level boolean  nameHasUpperCase =  false ; for  ( int  i = 0; i < name.length(); ++i) { if  (Character.isUpperCase(na...
Concise public  class MyClass { private   int   id ; private  String  description ; public  MyClass( int  id, String descr...
What makes it scalable?
is Object Oriented
Pure Object Oriented 1 + 2 1.+(2) 123.toString
Example – Rational Number <ul><li>A number that can be expressed a ratio  </li></ul><ul><ul><li>n (numerator) & d (denomin...
Example – Rational Number <ul><li>Implementing toString </li></ul>class   Rational (n:  Int , d:  Int ) { override   def  ...
Example – Rational Number <ul><li>Input Validation </li></ul>class   Rational (n:  Int , d:  Int ) { require(d != 0) overr...
Example – Rational Number <ul><li>Add Fields </li></ul>class   Rational (n:  Int , d:  Int ) { require(d != 0) val  numer:...
Example – Rational Number <ul><li>Adding </li></ul>scala>  val  oneHalf =  new   Rational (1, 2) oneHalf: Rational = 1/2 s...
Example – Rational Number <ul><li>A better version </li></ul>class   Rational (n:  Int , d:  Int ) { require(d != 0) val  ...
Example – Rational Number <ul><li>Finally </li></ul>scala>  val  x =  new   Rational (1, 2) x: Rational = 1/2 scala>  val ...
Traits
Trait trait   Dad  { private var  children:  List [ Child ] =  Nil def  addChild(child:  Child ) = children = child :: chi...
Base class   Man ( val  name:  String )  extends   Human
Static mixin composition class   Man ( val  name:  String )  extends   Human  with  Dad val  adi =  new   Man ( “Adi” ) ad...
Dynamic mixin composition class   Man ( val  name:  String )  extends   Human val  adi =  new   Man ( “Adi” )  with  Dad a...
Composition class   MrPotato (name:  String )  extends   MrPotatoHead with  Eyes with  Ears with  Mouth with  Nose with  H...
is Functional
First Class Functions (x:  Int ) => x + 1 <ul><li>Function literal </li></ul><ul><li>Compilation </li></ul><ul><li>Functio...
Functions as values scala>  val  increase = (x:  Int ) => x + 1 increase: (Int) => Int = <function> scala> increase(10) re...
Functions as parameters scala>  val  someNumbers =  List (-11, -10, -5, 0, 5, 10) someNumbers: List[Int] = List(-11, -10, ...
Short form of function literals scala> someNumbers.filter((x) => x > 0) res7: List[Int] = List(5, 10) scala> someNumbers.f...
Placeholder syntax scala> someNumbers.filter(_ > 0) res9: List[Int] = List(5, 10) <ul><li>“ Fill in” the missing... </li><...
Functions as closures (x:  Int ) => x + more  // how much more?
Functions as closures scala> (x:  Int ) => x + more <console>:5: error: not found: value more (x: Int) => x + more ˆ scala...
Functions as closures def  makeIncreaser(more:  Int ) = (x:  Int ) => x + more scala>  val  inc1 = makeIncreaser(1) inc1: ...
Functional Data Structures
List - Creating <ul><li>Prepends new elements </li></ul><ul><ul><li>Append time grows linearly </li></ul></ul>List (1, 2, ...
List - Basics scala>  val  list =  List (1, 2, 3) list: List[Int] = List(1, 2, 3) scala> list.head res0: Int = 1 scala> li...
List – High level operations scala>  val  list =  List (1, 2, 3) list: List[Int] = List(1, 2, 3) scala> list.map(_ + 1) re...
Other  <ul><li>Maps </li></ul><ul><li>Sets </li></ul><ul><li>Trees </li></ul><ul><li>Stacks </li></ul>
& Java
Unit Testing - JUnit import  junit.framework.TestCase import  junit.framework.Assert.assertEquals import  junit.framework....
Unit Testing - TestNG import  org.testng.annotations.Test import  org.testng.Assert.assertEquals import  Element.elem clas...
Credits
Jonas Bonér Scalable Solutions &
Upcoming SlideShare
Loading in...5
×

JBUG 11 - Scala For Java Programmers

2,962

Published on

Scala is 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.&quot; This session is an introduction of Scala for Java developers.
Presentation &amp; Fetured Video Included.

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

No Downloads
Views
Total Views
2,962
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

JBUG 11 - Scala For Java Programmers

  1. 1. By : Adi Baron, “Tikal” For Java Programmers
  2. 2. &quot;Which Programming Language would you use *now* on top of JVM, except Java?&quot; “ Scala”
  3. 3. Since 2003 Runs on JVM Pragmatic High Level Statically Typed Seamless Java interoperability Scalable EPFL Object Oriented Functional Production
  4. 4. A language that grows on you <ul><li>To the Point </li></ul><ul><li>Not cluttered </li></ul><ul><li>Not syntax </li></ul>var capital = Map( &quot;US&quot; -> &quot;Washington&quot; , &quot;France&quot; -> &quot;Paris&quot; ) capital += ( &quot;Japan&quot; -> &quot;Tokyo&quot; ) println(capital( &quot;France&quot; ))
  5. 5. Growing new types import java.math.BigInteger; public BigInteger factorial (BigInteger x) { if (x == BigInteger. ZERO ) { return BigInteger. ONE ; } return x.multiply(factorial( x.subtract(BigInteger. ONE ))); } def factorial(x: BigInt ): BigInt = if (x == 0) 1 else x * factorial(x - 1)
  6. 6. Growing new control constructs <ul><li>Scala Actors </li></ul><ul><ul><li>Implemented on top of threads </li></ul></ul><ul><ul><li>Two basic operations – Send / Receive </li></ul></ul>recipient ! msg ← async receive { case Msg1 => … // handle Msg1 case Msg2 => … // handle Msg2 // … } ← mailbox
  7. 7. Growing new control constructs val checksum = actor { var sum = 0 loop { receive { case Data (bytes) => sum += hash(bytes) case GetSum (requester) => requester ! sum } } }
  8. 8. High-Level boolean nameHasUpperCase = false ; for ( int i = 0; i < name.length(); ++i) { if (Character.isUpperCase(name.charAt(i))) { nameHasUpperCase = true ; break ; } } val nameHasUpperCase = name.exists(_.isUpperCase)
  9. 9. Concise public class MyClass { private int id ; private String description ; public MyClass( int id, String description) { this . id = id; this . description = description; } public int getId() { return id ; } public void setId( int id) { this . id = id; } public String getDescription() { return description ; } public void setDescription(String description) { this . description = description; } } class MyClass (id: Int , description: String )
  10. 10. What makes it scalable?
  11. 11. is Object Oriented
  12. 12. Pure Object Oriented 1 + 2 1.+(2) 123.toString
  13. 13. Example – Rational Number <ul><li>A number that can be expressed a ratio </li></ul><ul><ul><li>n (numerator) & d (denominator) are integers </li></ul></ul><ul><ul><li>Operations – Add, Subtract, Multiply, Divide </li></ul></ul>n _ d class Rational (n: Int , d: Int ) class Rational (n: Int , d: Int ) { println( “Created “ + n + “/” + d) } scala> new Rational (1, 2) Created 1/2 res0: Rational = Rational@90110a
  14. 14. Example – Rational Number <ul><li>Implementing toString </li></ul>class Rational (n: Int , d: Int ) { override def toString = n + “/” + d } scala> val x = new Rational (1, 3) x: Rational = 1/3 scala> val x = new Rational (5, 7) y: Rational = 5/7
  15. 15. Example – Rational Number <ul><li>Input Validation </li></ul>class Rational (n: Int , d: Int ) { require(d != 0) override def toString = n + “/” + d } scala> new Rational (5, 0) res0: Rational = 5/0
  16. 16. Example – Rational Number <ul><li>Add Fields </li></ul>class Rational (n: Int , d: Int ) { require(d != 0) val numer: Int = n val denom: Int = d override def toString = n + “/” + d def add(that: Rational ): Rational = new Rational ( numer * that.denom + that.numer * denom, denom * that.denom ) } class Rational (n: Int , d: Int ) { // This won’t compile require(d != 0) override def toString = n + “/” + d def add(that: Rational ): Rational = new Rational (n * that.d + that.n * d, d * that.d) }
  17. 17. Example – Rational Number <ul><li>Adding </li></ul>scala> val oneHalf = new Rational (1, 2) oneHalf: Rational = 1/2 scala> val twoThirds = new Rational (2, 3) twoThirds: Rational = 2/3 scala> oneHalf add twoThirds res3: Rational = 7/6
  18. 18. Example – Rational Number <ul><li>A better version </li></ul>class Rational (n: Int , d: Int ) { require(d != 0) val numer: Int = n val denom: Int = d def this (n: Int ) = this (n, 1) def +(that: Rational ): Rational = new Rational ( numer * that.denom + that.numer * denom, denom * that.denom ) override def toString = n + “/” + d }
  19. 19. Example – Rational Number <ul><li>Finally </li></ul>scala> val x = new Rational (1, 2) x: Rational = 1/2 scala> val y = new Rational (2, 3) y: Rational = 2/3 scala> x + y res8: Rational = 7/6
  20. 20. Traits
  21. 21. Trait trait Dad { private var children: List [ Child ] = Nil def addChild(child: Child ) = children = child :: children def getChildren = children.clone }
  22. 22. Base class Man ( val name: String ) extends Human
  23. 23. Static mixin composition class Man ( val name: String ) extends Human with Dad val adi = new Man ( “Adi” ) adi.addChild( new Child ( “Yehonatan” )) adi.addChild( new Child ( “Lior” ))
  24. 24. Dynamic mixin composition class Man ( val name: String ) extends Human val adi = new Man ( “Adi” ) with Dad adi.addChild( new Child ( “Yehonatan” )) adi.addChild( new Child ( “Lior” ))
  25. 25. Composition class MrPotato (name: String ) extends MrPotatoHead with Eyes with Ears with Mouth with Nose with Hands with Hat with Shoes
  26. 26. is Functional
  27. 27. First Class Functions (x: Int ) => x + 1 <ul><li>Function literal </li></ul><ul><li>Compilation </li></ul><ul><li>Function class </li></ul><ul><li>Runtime – function value </li></ul>
  28. 28. Functions as values scala> val increase = (x: Int ) => x + 1 increase: (Int) => Int = <function> scala> increase(10) res0: Int = 11 <ul><li>=> converts the “thing” on the left (any integer x) to the thing pm the right (x + 1) </li></ul>
  29. 29. Functions as parameters scala> val someNumbers = List (-11, -10, -5, 0, 5, 10) someNumbers: List[Int] = List(-11, -10, -5, 0, 5, 10) scala> someNumbers.foreach((x: Int ) => println(x)) -11 -10 -5 0 5 10 scala> someNumbers.filter((x: Int ) => x > 0) res6: List[Int] = List(5, 10)
  30. 30. Short form of function literals scala> someNumbers.filter((x) => x > 0) res7: List[Int] = List(5, 10) scala> someNumbers.filter(x => x > 0) res8: List[Int] = List(5, 10) <ul><li>Target typing </li></ul><ul><li>Leave the parentheses out </li></ul>
  31. 31. Placeholder syntax scala> someNumbers.filter(_ > 0) res9: List[Int] = List(5, 10) <ul><li>“ Fill in” the missing... </li></ul>scala> val f = _ + _ <console>:4: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2)) val f = _ + _ ˆ <ul><li>Type inference caution </li></ul>scala> val f = (_: Int ) + (_: Int ) f: (Int, Int) => Int = <function>
  32. 32. Functions as closures (x: Int ) => x + more // how much more?
  33. 33. Functions as closures scala> (x: Int ) => x + more <console>:5: error: not found: value more (x: Int) => x + more ˆ scala> var more = 1 more: Int = 1 scala> val addMore = (x: Int ) => x + more addMore: (Int) => Int = <function> scala> addMore(10) res19: Int = 11
  34. 34. Functions as closures def makeIncreaser(more: Int ) = (x: Int ) => x + more scala> val inc1 = makeIncreaser(1) inc1: (Int) => Int = <function> scala> val inc9999 = makeIncreaser(9999) inc9999: (Int) => Int = <function> scala> inc1(10) res24: Int = 11 scala> inc9999(10) res25: Int = 10009
  35. 35. Functional Data Structures
  36. 36. List - Creating <ul><li>Prepends new elements </li></ul><ul><ul><li>Append time grows linearly </li></ul></ul>List (1, 2, 3) 1 :: 2 :: 3 :: Nil
  37. 37. List - Basics scala> val list = List (1, 2, 3) list: List[Int] = List(1, 2, 3) scala> list.head res0: Int = 1 scala> list.tail res1: List[Int] = List(2, 3) scala> list.isEmpty res2: Boolean = false
  38. 38. List – High level operations scala> val list = List (1, 2, 3) list: List[Int] = List(1, 2, 3) scala> list.map(_ + 1) res0: List[Int] = List(2, 3, 4) scala> list.filter(_ < 2) res1: List[Int] = List(1) scala> list.exists(_ == 3) res2: Boolean = true scala> list.drop(2) res3: List[Int] = List(3) scala> list.reverse res4: List[Int] = List(3, 2, 1)
  39. 39. Other <ul><li>Maps </li></ul><ul><li>Sets </li></ul><ul><li>Trees </li></ul><ul><li>Stacks </li></ul>
  40. 40. & Java
  41. 41. Unit Testing - JUnit import junit.framework.TestCase import junit.framework.Assert.assertEquals import junit.framework.Assert.fail import Element.elem class ElementTestCase extends TestCase { def testUniformElement() { val ele = elem( 'x' , 2, 3) assertEquals(2, ele.width) assertEquals(3, ele.height) try { elem('x', -2, 3) fail() } catch { case e: IllegalArgumentException => // expected } } }
  42. 42. Unit Testing - TestNG import org.testng.annotations.Test import org.testng.Assert.assertEquals import Element.elem class ElementTests { @ Test def verifyUniformElement() { val ele = elem( 'x' , 2, 3) assertEquals(ele.width, 2) assertEquals(ele.height, 3) } @ Test { val expectedExceptions = Array (classOf[ IllegalArgumentException ]) } def elemShouldThrowIAE() { elem( 'x' , -2, 3) } }
  43. 43. Credits
  44. 44. Jonas Bonér Scalable Solutions &

×