SlideShare ist ein Scribd-Unternehmen logo
1 von 46
A first date with Scala Franco Lombardo XP User Group - Bergamo http://www.francolombardo.net Edward Hopper - Summertime
First of all, you must know… http://www.francolombardo.net … I’m not a Guru!!!
Scala: passport ,[object Object],[object Object],[object Object],[object Object],http://www.francolombardo.net Claudio Destito – Auto-ritratto
Scala = SCAlable LAnguage http://www.francolombardo.net In order to “scale” you need solid basis! ,[object Object],[object Object],[object Object],The base of Scala is Java
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Yet another Hello World program Running on JMV 1.6.0_13 On Sat May 23 17:36:34 CEST 2009 Output
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Basic syntax very close to Java
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Object from java.lang are imported by default
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Object from Java libraries are easly avaible
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Simplified syntax: semicolon is not mandatory
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Simplified syntax: less code
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello   extends   Application  { println ( "Yet another Hello World program" ) val   jvmVer  =  System . getProperty ( "java.version" ) println ( "Running on JMV "   +   jvmVer ) println ( "On "   +   new   Date ()) } Simplified syntax: type inference
Type inference: have you ever seen it? http://www.francolombardo.net //Java return  customer.getOrder(40) .getRow(20) .getItem() .getWeight() * 2.5; (OK, in this example we do not obey Demeter law, but it’s only an example…  )
Tools for scalability:  static typing http://www.francolombardo.net ,[object Object],[object Object],[object Object],[object Object]
Tools for scalbility:  “pure” Object Orientation http://www.francolombardo.net ,[object Object],[object Object],2  +  5 //equals to... 2. + (5) ,[object Object],[object Object],[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net ,[object Object],Fernando Botero – Bailerina na barra
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { public  String name() { //some implementation… } public  String address() { //some implementation } ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //… public  Bank preferredBank() { //some implementation… } public  Solvability solvability() { //some implementation… } ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //… public  Agent zoneAgent() { //some implementation… } public  Boolean isToSendEMails() { //some implementation… } ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //… public  Carrier[] carriers() { //some implementation… } public  Discount[] discounts() { //some implementation… } ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //… public  Priority schedulingPriority() { //some implementation… } public  TechnicalInfo[] standards() { //some implementation… } ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net ,[object Object],Customer name() address() AccountingCustomer preferredBank() solvability() CRMCustomer zoneAgent() isToSendEMails() ProductionCustomer schedulingPriority() standards() ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Scala case   class   Customer ( name :  String , address :  String ) trait   accountingCstomer  { def   preferredBank  =  //some implementation def   solvability  =  //some implementation } trait   CRMCustomer  { def   zoneAgent  =  //some implementation def   isToSendEMail  =  //implementation } ,[object Object]
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Scala val   customer  =  new   Customer ( “Lombardo ltd" , “ Wall Street, 1" )  with   CRMCustomer   with   AccountingCustomer ,[object Object],“ Dynamic” type composition
Tools for scalability:  functional programming http://www.francolombardo.net ,[object Object],[object Object],You can decompose the system in generic functions which you can reuse reducing duplications Higher order functions
Tools for scalability:  functional programming http://www.francolombardo.net ,[object Object],[object Object],[object Object],[object Object],Referential transparency
Tools for scalability:  functional programming http://www.francolombardo.net An example: numeric integration…
Tools for scalability:  functional programming http://www.francolombardo.net … I was joking, let’s use a more “concrete” example //Here’s an order row case   class   OrderRow ( description :  String ,  price :  Double ,  qty :  Double ) { def   amount  =  price   *   qty } //And an order val   order  =  List ( OrderRow ( "Beer" , 5.0, 2),  OrderRow ( "Chips" , 2.5, 1))
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total );
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total ); ,[object Object],[object Object]
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total ); ,[object Object],[object Object],[object Object]
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total ); ,[object Object],[object Object],[object Object],[object Object]
Tools for scalability:  functional programming http://www.francolombardo.net //The computation: it’s a function we can assign val   vat  = ( row :  OrderRow ) =>  row . amount   *  0.2 //Composition of accumulation and computation def   composition ( aggr : ( Double ,  Double ) =>  Double , calculus : ( OrderRow  =>  Double )) ( partial :  Double ,  row :  OrderRow ) = aggr ( partial ,  calculus ( row )) val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Let’s decompose these 3 operations
Tools for scalability:  functional programming http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Let’s decompose these 3 operations ,[object Object],//Java B b = start; for ( final  A a : listOfA) { b  = method( b ,  a ); } return  b; //Scala listOfA . foldLeft(start)(method)
Tools for scalability:  functional programming http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Let’s decompose these 3 operations ,[object Object],[object Object]
Tools for scalability:  functional programming http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Let’s decompose these 3 operations ,[object Object],[object Object],[object Object]
Tools for scalability:  functional programming http://www.francolombardo.net val  totalAmount = order . foldLeft (0.0)( composition (_ + _,  _. amount )) val  maxAmount = order . foldLeft (0.0)( composition ( Math . max ,  _. amount )) val  maxVat = order . foldLeft (0.0)( composition ( Math . max ,  vat )) We can recompose them in a different way
Structural types http://www.francolombardo.net def   deleteAllRows ( statement :  java . sql . Statement ) =  statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) How can we test this method? An hand written mock?  The java.sql.Statement interface has 41 methods!!! (Well, I don’t like frameworks…)
Structural types http://www.francolombardo.net def   deleteAllRows ( statement : { def   execute ( sql : String):  Boolean }) =  statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) Let’s modify our method declaring only what we need
Structural types http://www.francolombardo.net def   testDeleteAllRows () { val   mockStatement  =  new  { def   execute ( sql :  String ) = { println ( sql )  //Oppure qualsiasi cosa x test true   //Valore di ritorno di execute } } deleteAllRows ( mockStatement ) } Now we can test our code easily
Implicit conversions http://www.francolombardo.net class   RemoteStatement  { def   remoteExecute ( sql :  String ) = { println ( sql   +   &quot; executed remotely :-)&quot; ) true } } What if we’d need to use a library for doing remote queries?
Implicit conversions http://www.francolombardo.net implicit   def   normalize ( remote :  RemoteStatement ) = new  {   def   execute ( sql :  String ) = remote . remoteExecute ( sql ) } //I can use it even if it is not the exact type! deleteAllRows ( new   RemoteStatement ) No problem: we can convert on the fly!
Domain Specific Languages http://www.francolombardo.net val   tenDollars  =  (4.0  USD )  +  (6.0  USD ) I’d like to write something like this Andy Warhol– Dollar sign
Domain Specific Languages http://www.francolombardo.net abstract   class   Currency  { def   name :  String ; override   def   toString  =  name } object   Euro   extends   Currency  { def   name  = &quot;EUR&quot; } object   Dollar   extends   Currency  { def   name  =  &quot;USD&quot; } No problem!
Domain Specific Languages http://www.francolombardo.net case   class   Money [ C  <:  Currency ]( amount :  Double ,   currency :  C ) { def   +  ( otherMoney :  Money [ C ]) =  new   Money ( amount   +   otherMoney . amount , currency ) override   def   toString  =  amount   +   &quot; &quot;   +   currency } No problem!
Domain Specific Languages http://www.francolombardo.net object   Money  { implicit   def   doubleConverter ( d :  Double ) =  new  { def   EUR  = { new   Money ( d ,  Euro ) } def   USD  = { new   Money ( d ,  Dollar ) } } No problem!
Domain Specific Languages http://www.francolombardo.net object   Playground   extends   BasicClass  { def   main ( args :  Array [ String ]) {  10  PRINT   &quot;SCALA ROCKS!&quot; 20  GOTO  10 RUN } } With these tools we can write very exciting DSLs! Example created by Szymon Jachim See http://www.scala-lang.org/node/1403

Weitere ähnliche Inhalte

Was ist angesagt?

Spring AOP Introduction
Spring AOP IntroductionSpring AOP Introduction
Spring AOP Introduction
b0ris_1
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
Zend Studio Tips and Tricks
Zend Studio Tips and TricksZend Studio Tips and Tricks
Zend Studio Tips and Tricks
Roy Ganor
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Kamal Acharya
 
SiriusCon 2015 - Breathe Life into Your Designer!
SiriusCon 2015 - Breathe Life into Your Designer!SiriusCon 2015 - Breathe Life into Your Designer!
SiriusCon 2015 - Breathe Life into Your Designer!
melbats
 

Was ist angesagt? (20)

operator overloading
operator overloadingoperator overloading
operator overloading
 
Unit Testing for Great Justice
Unit Testing for Great JusticeUnit Testing for Great Justice
Unit Testing for Great Justice
 
JDK8 Functional API
JDK8 Functional APIJDK8 Functional API
JDK8 Functional API
 
Advance JS and oop
Advance JS and oopAdvance JS and oop
Advance JS and oop
 
Spring AOP Introduction
Spring AOP IntroductionSpring AOP Introduction
Spring AOP Introduction
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
Building Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devicesBuilding Progressive Web Apps for Windows devices
Building Progressive Web Apps for Windows devices
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
06. operator overloading
06. operator overloading06. operator overloading
06. operator overloading
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Zend Studio Tips and Tricks
Zend Studio Tips and TricksZend Studio Tips and Tricks
Zend Studio Tips and Tricks
 
EclipseCon France 2016 - Sirius 4.0: Let me Sirius that for you!
EclipseCon France 2016 - Sirius 4.0: Let me Sirius that for you!EclipseCon France 2016 - Sirius 4.0: Let me Sirius that for you!
EclipseCon France 2016 - Sirius 4.0: Let me Sirius that for you!
 
Pure Functions and Immutable Objects
Pure Functions and Immutable ObjectsPure Functions and Immutable Objects
Pure Functions and Immutable Objects
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)Kotlin For Android - Properties (part 4 of 7)
Kotlin For Android - Properties (part 4 of 7)
 
Scala Future & Promises
Scala Future & PromisesScala Future & Promises
Scala Future & Promises
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
SiriusCon 2015 - Breathe Life into Your Designer!
SiriusCon 2015 - Breathe Life into Your Designer!SiriusCon 2015 - Breathe Life into Your Designer!
SiriusCon 2015 - Breathe Life into Your Designer!
 
Effective C++/WinRT for UWP and Win32
Effective C++/WinRT for UWP and Win32Effective C++/WinRT for UWP and Win32
Effective C++/WinRT for UWP and Win32
 

Andere mochten auch

Rock scissors-paper-kata
Rock scissors-paper-kataRock scissors-paper-kata
Rock scissors-paper-kata
Franco Lombardo
 
Happiness Presentation
Happiness PresentationHappiness Presentation
Happiness Presentation
rgonzalezjr
 

Andere mochten auch (10)

Primo Incontro Con Scala
Primo Incontro Con ScalaPrimo Incontro Con Scala
Primo Incontro Con Scala
 
Rock scissors-paper-kata
Rock scissors-paper-kataRock scissors-paper-kata
Rock scissors-paper-kata
 
Java per as400
Java per as400Java per as400
Java per as400
 
Science of happiness
Science of happinessScience of happiness
Science of happiness
 
The Dragonfly Effect - Harnessing Social Media to Build Brands
The Dragonfly Effect - Harnessing Social Media to Build BrandsThe Dragonfly Effect - Harnessing Social Media to Build Brands
The Dragonfly Effect - Harnessing Social Media to Build Brands
 
Scrum, Games and The Science of Happiness
Scrum, Games and The Science of HappinessScrum, Games and The Science of Happiness
Scrum, Games and The Science of Happiness
 
Science of Happiness Presentation
Science of Happiness PresentationScience of Happiness Presentation
Science of Happiness Presentation
 
Happiness presentation ppt
Happiness presentation pptHappiness presentation ppt
Happiness presentation ppt
 
The Power of Happiness
The Power of HappinessThe Power of Happiness
The Power of Happiness
 
Happiness Presentation
Happiness PresentationHappiness Presentation
Happiness Presentation
 

Ähnlich wie A First Date With Scala

Advisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptAdvisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScript
dominion
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
Fabio Franzini
 
Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256
Azhar Satti
 

Ähnlich wie A First Date With Scala (20)

JavaScript
JavaScriptJavaScript
JavaScript
 
Understanding Framework Architecture using Eclipse
Understanding Framework Architecture using EclipseUnderstanding Framework Architecture using Eclipse
Understanding Framework Architecture using Eclipse
 
Protocol-Oriented Programming in Swift
Protocol-Oriented Programming in SwiftProtocol-Oriented Programming in Swift
Protocol-Oriented Programming in Swift
 
Advisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScriptAdvisor Jumpstart: JavaScript
Advisor Jumpstart: JavaScript
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
SystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features SummarySystemVerilog OOP Ovm Features Summary
SystemVerilog OOP Ovm Features Summary
 
Adding a modern twist to legacy web applications
Adding a modern twist to legacy web applicationsAdding a modern twist to legacy web applications
Adding a modern twist to legacy web applications
 
JavaScript for real men
JavaScript for real menJavaScript for real men
JavaScript for real men
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
RichFaces - Testing on Mobile Devices
RichFaces - Testing on Mobile DevicesRichFaces - Testing on Mobile Devices
RichFaces - Testing on Mobile Devices
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
 
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
 
eXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction TrainingeXo SEA - JavaScript Introduction Training
eXo SEA - JavaScript Introduction Training
 
Release with confidence
Release with confidenceRelease with confidence
Release with confidence
 
Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256Qtp Training Deepti 3 Of 44256
Qtp Training Deepti 3 Of 44256
 
Qtp Training
Qtp Training Qtp Training
Qtp Training
 
Pragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScriptPragmatic Parallels: Java and JavaScript
Pragmatic Parallels: Java and JavaScript
 
Javazone 2010-lift-framework-public
Javazone 2010-lift-framework-publicJavazone 2010-lift-framework-public
Javazone 2010-lift-framework-public
 
Performance and Scalability Testing with Python and Multi-Mechanize
Performance and Scalability Testing with Python and Multi-MechanizePerformance and Scalability Testing with Python and Multi-Mechanize
Performance and Scalability Testing with Python and Multi-Mechanize
 
Reactive application using meteor
Reactive application using meteorReactive application using meteor
Reactive application using meteor
 

Mehr von Franco Lombardo

Mehr von Franco Lombardo (12)

happiness_2023.pdf
happiness_2023.pdfhappiness_2023.pdf
happiness_2023.pdf
 
Kotlin from-scratch 3 - coroutines
Kotlin from-scratch 3 - coroutinesKotlin from-scratch 3 - coroutines
Kotlin from-scratch 3 - coroutines
 
Kotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functionsKotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functions
 
Kotlin from-scratch
Kotlin from-scratchKotlin from-scratch
Kotlin from-scratch
 
Agile Venture Milan - Unit testing on AS400? Yes we can! (With Kotlin)
Agile Venture Milan - Unit testing on AS400? Yes we can! (With Kotlin)Agile Venture Milan - Unit testing on AS400? Yes we can! (With Kotlin)
Agile Venture Milan - Unit testing on AS400? Yes we can! (With Kotlin)
 
Unit testing on AS400? Yes we can! (With Kotlin)
Unit testing on AS400? Yes we can! (With Kotlin)Unit testing on AS400? Yes we can! (With Kotlin)
Unit testing on AS400? Yes we can! (With Kotlin)
 
Interprete Kotlin per l’RPG e libreria Web Components: Open Source per la m...
Interprete Kotlin per l’RPG  e libreria Web Components: Open Source per  la m...Interprete Kotlin per l’RPG  e libreria Web Components: Open Source per  la m...
Interprete Kotlin per l’RPG e libreria Web Components: Open Source per la m...
 
TDD su AS400? Con Kotlin si può fare! - Italian Agile Days 2019
TDD su AS400? Con Kotlin si può fare! - Italian Agile Days 2019TDD su AS400? Con Kotlin si può fare! - Italian Agile Days 2019
TDD su AS400? Con Kotlin si può fare! - Italian Agile Days 2019
 
Un interprete Kotlin per il linguaggio RPG AS400 - IBM i
Un interprete Kotlin per il linguaggio RPG AS400 - IBM iUn interprete Kotlin per il linguaggio RPG AS400 - IBM i
Un interprete Kotlin per il linguaggio RPG AS400 - IBM i
 
Agile Happiness - Agile O'Day 2018
Agile Happiness - Agile O'Day 2018Agile Happiness - Agile O'Day 2018
Agile Happiness - Agile O'Day 2018
 
Agile Happiness 2
Agile Happiness 2Agile Happiness 2
Agile Happiness 2
 
Agile Happiness
Agile HappinessAgile Happiness
Agile Happiness
 

Kürzlich hochgeladen

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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...
 
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, ...
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
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
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 

A First Date With Scala

  • 1. A first date with Scala Franco Lombardo XP User Group - Bergamo http://www.francolombardo.net Edward Hopper - Summertime
  • 2. First of all, you must know… http://www.francolombardo.net … I’m not a Guru!!!
  • 3.
  • 4.
  • 5. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Yet another Hello World program Running on JMV 1.6.0_13 On Sat May 23 17:36:34 CEST 2009 Output
  • 6. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Basic syntax very close to Java
  • 7. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Object from java.lang are imported by default
  • 8. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Object from Java libraries are easly avaible
  • 9. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Simplified syntax: semicolon is not mandatory
  • 10. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Simplified syntax: less code
  • 11. Here is our star: Hello World!!! http://www.francolombardo.net package hello import java . util .Date object Hello extends Application { println ( &quot;Yet another Hello World program&quot; ) val jvmVer = System . getProperty ( &quot;java.version&quot; ) println ( &quot;Running on JMV &quot; + jvmVer ) println ( &quot;On &quot; + new Date ()) } Simplified syntax: type inference
  • 12. Type inference: have you ever seen it? http://www.francolombardo.net //Java return customer.getOrder(40) .getRow(20) .getItem() .getWeight() * 2.5; (OK, in this example we do not obey Demeter law, but it’s only an example…  )
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. Tools for scalability: functional programming http://www.francolombardo.net An example: numeric integration…
  • 27. Tools for scalability: functional programming http://www.francolombardo.net … I was joking, let’s use a more “concrete” example //Here’s an order row case class OrderRow ( description : String , price : Double , qty : Double ) { def amount = price * qty } //And an order val order = List ( OrderRow ( &quot;Beer&quot; , 5.0, 2), OrderRow ( &quot;Chips&quot; , 2.5, 1))
  • 28. Tools for scalability: functional programming http://www.francolombardo.net //A traditional approach to VAT computation var total = 0.0 for ( row <- order ) { total += row . amount * 0.2 } println ( &quot;VAT (Total): &quot; + total );
  • 29.
  • 30.
  • 31.
  • 32. Tools for scalability: functional programming http://www.francolombardo.net //The computation: it’s a function we can assign val vat = ( row : OrderRow ) => row . amount * 0.2 //Composition of accumulation and computation def composition ( aggr : ( Double , Double ) => Double , calculus : ( OrderRow => Double )) ( partial : Double , row : OrderRow ) = aggr ( partial , calculus ( row )) val totalVat = order . foldLeft (0.0)( composition (_ + _, vat )) Let’s decompose these 3 operations
  • 33.
  • 34.
  • 35.
  • 36. Tools for scalability: functional programming http://www.francolombardo.net val totalAmount = order . foldLeft (0.0)( composition (_ + _, _. amount )) val maxAmount = order . foldLeft (0.0)( composition ( Math . max , _. amount )) val maxVat = order . foldLeft (0.0)( composition ( Math . max , vat )) We can recompose them in a different way
  • 37. Structural types http://www.francolombardo.net def deleteAllRows ( statement : java . sql . Statement ) = statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) How can we test this method? An hand written mock? The java.sql.Statement interface has 41 methods!!! (Well, I don’t like frameworks…)
  • 38. Structural types http://www.francolombardo.net def deleteAllRows ( statement : { def execute ( sql : String): Boolean }) = statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) Let’s modify our method declaring only what we need
  • 39. Structural types http://www.francolombardo.net def testDeleteAllRows () { val mockStatement = new { def execute ( sql : String ) = { println ( sql ) //Oppure qualsiasi cosa x test true //Valore di ritorno di execute } } deleteAllRows ( mockStatement ) } Now we can test our code easily
  • 40. Implicit conversions http://www.francolombardo.net class RemoteStatement { def remoteExecute ( sql : String ) = { println ( sql + &quot; executed remotely :-)&quot; ) true } } What if we’d need to use a library for doing remote queries?
  • 41. Implicit conversions http://www.francolombardo.net implicit def normalize ( remote : RemoteStatement ) = new { def execute ( sql : String ) = remote . remoteExecute ( sql ) } //I can use it even if it is not the exact type! deleteAllRows ( new RemoteStatement ) No problem: we can convert on the fly!
  • 42. Domain Specific Languages http://www.francolombardo.net val tenDollars = (4.0 USD ) + (6.0 USD ) I’d like to write something like this Andy Warhol– Dollar sign
  • 43. Domain Specific Languages http://www.francolombardo.net abstract class Currency { def name : String ; override def toString = name } object Euro extends Currency { def name = &quot;EUR&quot; } object Dollar extends Currency { def name = &quot;USD&quot; } No problem!
  • 44. Domain Specific Languages http://www.francolombardo.net case class Money [ C <: Currency ]( amount : Double , currency : C ) { def + ( otherMoney : Money [ C ]) = new Money ( amount + otherMoney . amount , currency ) override def toString = amount + &quot; &quot; + currency } No problem!
  • 45. Domain Specific Languages http://www.francolombardo.net object Money { implicit def doubleConverter ( d : Double ) = new { def EUR = { new Money ( d , Euro ) } def USD = { new Money ( d , Dollar ) } } No problem!
  • 46. Domain Specific Languages http://www.francolombardo.net object Playground extends BasicClass { def main ( args : Array [ String ]) { 10 PRINT &quot;SCALA ROCKS!&quot; 20 GOTO 10 RUN } } With these tools we can write very exciting DSLs! Example created by Szymon Jachim See http://www.scala-lang.org/node/1403