SlideShare a Scribd company logo
1 of 44
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 Oriented Functional Production
A language that grows on you ,[object Object],[object Object],[object Object],var  capital =  Map( "US"  ->  "Washington" ,  "France"  ->  "Paris" ) capital += ( "Japan"  ->  "Tokyo" ) println(capital( "France" ))
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)
Growing new control constructs ,[object Object],[object Object],[object Object],recipient ! msg ←  async receive { case   Msg1  =>  … // handle Msg1 case   Msg2  =>  … // handle Msg2 // …  } ←  mailbox
Growing new control constructs val  checksum = actor { var  sum = 0 loop { receive { case   Data (bytes)  => sum += hash(bytes) case   GetSum (requester) => requester ! sum } } }
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)
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 )
What makes it scalable?
is Object Oriented
Pure Object Oriented 1 + 2 1.+(2) 123.toString
Example – Rational Number ,[object Object],[object Object],[object Object],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
Example – Rational Number ,[object Object],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
Example – Rational Number ,[object Object],class   Rational (n:  Int , d:  Int ) { require(d != 0) override   def  toString = n +  “/”  + d } scala>  new   Rational (5, 0) res0: Rational = 5/0
Example – Rational Number ,[object Object],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) }
Example – Rational Number ,[object Object],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
Example – Rational Number ,[object Object],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 }
Example – Rational Number ,[object Object],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
Traits
Trait trait   Dad  { private var  children:  List [ Child ] =  Nil def  addChild(child:  Child ) = children = child :: children def  getChildren = children.clone }
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” ) adi.addChild( new   Child ( “Yehonatan” )) adi.addChild( new   Child ( “Lior” ))
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” ))
Composition class   MrPotato (name:  String )  extends   MrPotatoHead with  Eyes with  Ears with  Mouth with  Nose with  Hands with  Hat with  Shoes
is Functional
First Class Functions (x:  Int ) => x + 1 ,[object Object],[object Object],[object Object],[object Object]
Functions as values scala>  val  increase = (x:  Int ) => x + 1 increase: (Int) => Int = <function> scala> increase(10) res0: Int = 11 ,[object Object]
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)
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) ,[object Object],[object Object]
Placeholder syntax scala> someNumbers.filter(_ > 0) res9: List[Int] = List(5, 10) ,[object Object],scala>  val  f = _ + _ <console>:4: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$plus(x$2)) val f = _ + _ ˆ ,[object Object],scala>  val  f = (_:  Int ) + (_:  Int ) f: (Int, Int) => Int = <function>
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>  var  more = 1 more: Int = 1 scala>  val  addMore = (x:  Int ) => x + more addMore: (Int) => Int = <function> scala> addMore(10) res19: Int = 11
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
Functional Data Structures
List - Creating ,[object Object],[object Object],List (1, 2, 3) 1 :: 2 :: 3 ::  Nil
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
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)
Other  ,[object Object],[object Object],[object Object],[object Object]
& Java
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 } } }
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) } }
Credits
Jonas Bonér Scalable Solutions &

More Related Content

What's hot

Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceAlexey Raga
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Model-Driven Software Development - Static Analysis & Error Checking
Model-Driven Software Development - Static Analysis & Error CheckingModel-Driven Software Development - Static Analysis & Error Checking
Model-Driven Software Development - Static Analysis & Error CheckingEelco Visser
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional ProgrammingLuka Jacobowitz
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverseLuka Jacobowitz
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final TaglessJohn De Goes
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Lucas Witold Adamus
 
Practical scalaz
Practical scalazPractical scalaz
Practical scalazoxbow_lakes
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FPLuka Jacobowitz
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about lazinessJohan Tibell
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type ClassesJohn De Goes
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 

What's hot (20)

Type classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritanceType classes 101 - classification beyond inheritance
Type classes 101 - classification beyond inheritance
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Model-Driven Software Development - Static Analysis & Error Checking
Model-Driven Software Development - Static Analysis & Error CheckingModel-Driven Software Development - Static Analysis & Error Checking
Model-Driven Software Development - Static Analysis & Error Checking
 
Testing in the World of Functional Programming
Testing in the World of Functional ProgrammingTesting in the World of Functional Programming
Testing in the World of Functional Programming
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
The Death of Final Tagless
The Death of Final TaglessThe Death of Final Tagless
The Death of Final Tagless
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?Why async and functional programming in PHP7 suck and how to get overr it?
Why async and functional programming in PHP7 suck and how to get overr it?
 
Practical scalaz
Practical scalazPractical scalaz
Practical scalaz
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
 
Reasoning about laziness
Reasoning about lazinessReasoning about laziness
Reasoning about laziness
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
Refactoring Functional Type Classes
Refactoring Functional Type ClassesRefactoring Functional Type Classes
Refactoring Functional Type Classes
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 

Similar to JBUG 11 - Scala For Java Programmers

Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scalaparag978978
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meetMario Fusco
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional ProgrammingEelco Visser
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecLoïc Descotte
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfHiroshi Ono
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In ScalaSkills Matter
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming iiPrashant Kalkar
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala LanguageAshal aka JOKER
 

Similar to JBUG 11 - Scala For Java Programmers (20)

Scala introduction
Scala introductionScala introduction
Scala introduction
 
SDC - Einführung in Scala
SDC - Einführung in ScalaSDC - Einführung in Scala
SDC - Einführung in Scala
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
Lecture 5: Functional Programming
Lecture 5: Functional ProgrammingLecture 5: Functional Programming
Lecture 5: Functional Programming
 
Scala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar ProkopecScala presentation by Aleksandar Prokopec
Scala presentation by Aleksandar Prokopec
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
Functional object
Functional objectFunctional object
Functional object
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
Functional programming ii
Functional programming iiFunctional programming ii
Functional programming ii
 
여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language여자개발자모임터 6주년 개발 세미나 - Scala Language
여자개발자모임터 6주년 개발 세미나 - Scala Language
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Meet scala
Meet scalaMeet scala
Meet scala
 

More from Tikal Knowledge

Clojure - LISP on the JVM
Clojure - LISP on the JVM Clojure - LISP on the JVM
Clojure - LISP on the JVM Tikal Knowledge
 
Tabtale story: Building a publishing and monitoring mobile games architecture...
Tabtale story: Building a publishing and monitoring mobile games architecture...Tabtale story: Building a publishing and monitoring mobile games architecture...
Tabtale story: Building a publishing and monitoring mobile games architecture...Tikal Knowledge
 
Processing Big Data in Realtime
Processing Big Data in RealtimeProcessing Big Data in Realtime
Processing Big Data in RealtimeTikal Knowledge
 
Docking your services_with_docker
Docking your services_with_dockerDocking your services_with_docker
Docking your services_with_dockerTikal Knowledge
 
Writing a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerWriting a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerTikal Knowledge
 
.Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013
.Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013 .Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013
.Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013 Tikal Knowledge
 
Tikal's Backbone_js introduction workshop
Tikal's Backbone_js introduction workshopTikal's Backbone_js introduction workshop
Tikal's Backbone_js introduction workshopTikal Knowledge
 
Cloud computing - an insight into "how does it really work ?"
Cloud computing - an insight into "how does it really work ?" Cloud computing - an insight into "how does it really work ?"
Cloud computing - an insight into "how does it really work ?" Tikal Knowledge
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud ComputingTikal Knowledge
 
Tikal Fuse Day Access Layer Implementation (C#) Based On Mongo Db
Tikal Fuse Day   Access Layer Implementation (C#) Based On Mongo DbTikal Fuse Day   Access Layer Implementation (C#) Based On Mongo Db
Tikal Fuse Day Access Layer Implementation (C#) Based On Mongo DbTikal Knowledge
 
Ship early ship often with Django
Ship early ship often with DjangoShip early ship often with Django
Ship early ship often with DjangoTikal Knowledge
 
Introduction To Cloud Computing
Introduction To Cloud ComputingIntroduction To Cloud Computing
Introduction To Cloud ComputingTikal Knowledge
 

More from Tikal Knowledge (20)

Clojure - LISP on the JVM
Clojure - LISP on the JVM Clojure - LISP on the JVM
Clojure - LISP on the JVM
 
Clojure presentation
Clojure presentationClojure presentation
Clojure presentation
 
Tabtale story: Building a publishing and monitoring mobile games architecture...
Tabtale story: Building a publishing and monitoring mobile games architecture...Tabtale story: Building a publishing and monitoring mobile games architecture...
Tabtale story: Building a publishing and monitoring mobile games architecture...
 
Kafka short
Kafka shortKafka short
Kafka short
 
Heatmap
HeatmapHeatmap
Heatmap
 
Processing Big Data in Realtime
Processing Big Data in RealtimeProcessing Big Data in Realtime
Processing Big Data in Realtime
 
Docking your services_with_docker
Docking your services_with_dockerDocking your services_with_docker
Docking your services_with_docker
 
Who moved my_box
Who moved my_boxWho moved my_box
Who moved my_box
 
Writing a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media playerWriting a Fullstack Application with Javascript - Remote media player
Writing a Fullstack Application with Javascript - Remote media player
 
.Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013
.Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013 .Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013
.Net OSS Ci & CD with Jenkins - JUC ISRAEL 2013
 
Tikal's Backbone_js introduction workshop
Tikal's Backbone_js introduction workshopTikal's Backbone_js introduction workshop
Tikal's Backbone_js introduction workshop
 
TCE Automation
TCE AutomationTCE Automation
TCE Automation
 
Tce automation-d4
Tce automation-d4Tce automation-d4
Tce automation-d4
 
Cloud computing - an insight into "how does it really work ?"
Cloud computing - an insight into "how does it really work ?" Cloud computing - an insight into "how does it really work ?"
Cloud computing - an insight into "how does it really work ?"
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
 
Tikal Fuse Day Access Layer Implementation (C#) Based On Mongo Db
Tikal Fuse Day   Access Layer Implementation (C#) Based On Mongo DbTikal Fuse Day   Access Layer Implementation (C#) Based On Mongo Db
Tikal Fuse Day Access Layer Implementation (C#) Based On Mongo Db
 
Ship early ship often with Django
Ship early ship often with DjangoShip early ship often with Django
Ship early ship often with Django
 
Google App Engine
Google App EngineGoogle App Engine
Google App Engine
 
AWS Case Study
AWS Case StudyAWS Case Study
AWS Case Study
 
Introduction To Cloud Computing
Introduction To Cloud ComputingIntroduction To Cloud Computing
Introduction To Cloud Computing
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 

JBUG 11 - Scala For Java Programmers

  • 1. By : Adi Baron, “Tikal” For Java Programmers
  • 2. &quot;Which Programming Language would you use *now* on top of JVM, except Java?&quot; “ Scala”
  • 3. Since 2003 Runs on JVM Pragmatic High Level Statically Typed Seamless Java interoperability Scalable EPFL Object Oriented Functional Production
  • 4.
  • 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.
  • 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. 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. 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. What makes it scalable?
  • 12. Pure Object Oriented 1 + 2 1.+(2) 123.toString
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 21. Trait trait Dad { private var children: List [ Child ] = Nil def addChild(child: Child ) = children = child :: children def getChildren = children.clone }
  • 22. Base class Man ( val name: String ) extends Human
  • 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. 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. Composition class MrPotato (name: String ) extends MrPotatoHead with Eyes with Ears with Mouth with Nose with Hands with Hat with Shoes
  • 27.
  • 28.
  • 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.
  • 31.
  • 32. Functions as closures (x: Int ) => x + more // how much more?
  • 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. 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
  • 36.
  • 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. 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.
  • 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. 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) } }
  • 44. Jonas Bonér Scalable Solutions &