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

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 

Recently uploaded (20)

Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 

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 &