SlideShare une entreprise Scribd logo
1  sur  46
Primo incontro con Scala Franco Lombardo XP User Group - Bergamo http://www.francolombardo.net Edward Hopper - Summertime
Premessa http://www.francolombardo.net Non sono un Guru!!!
Scala: carta di identità ,[object Object],[object Object],[object Object],[object Object],http://www.francolombardo.net Claudio Destito – Auto-ritratto
Scala = SCAlable LAnguage http://www.francolombardo.net Per “scalare” occorrono basi solide! ,[object Object],[object Object],[object Object],La base di Scala è Java
Finalmente 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
Finalmente 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 ()) } Sintassi base molto simile a Java
Finalmente 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 ()) } Oggetti di java.lang importati di default
Finalmente 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 ()) } Oggetti librerie Java importabili facilmente
Finalmente 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 ()) } Sintassi semplificata: punto e virgola non obbligatorio
Finalmente 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 ()) } Sintassi semplificata: meno codice
Finalmente 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 ()) } Sintassi semplificata: inferenza dei tipi
Inferenza dei tipi: una novità? http://www.francolombardo.net //Java return  cliente.getOrdine(40) .getRiga(20) .getArticolo() .getPeso() * 2.5; (OK, questo esempio non rispetta la legge di Demeter, ma è solo un esempio…  )
Strumenti per la scalabilità:  tipizzazione statica http://www.francolombardo.net ,[object Object],[object Object],[object Object],[object Object]
Strumenti per la scalabilità:  Object Orientation “pura” http://www.francolombardo.net ,[object Object],[object Object],2  +  5 //Equivale a... 2. + (5) ,[object Object],[object Object],[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net ,[object Object],Fernando Botero – Bailerina na barra
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Java public   class  Cliente { public  String ragioneSociale() { //implementazione… } public  String indirizzo() { //implementazione… } ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Java public   class  Cliente { //… public  Banca bancaDiAppoggio() { //implementazione… } public  Solvibilita solvibilita() { //implementazione… } ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Java public   class  Cliente { //… public  Agente agenteDiZona() { //implementazione… } public  Boolean daContattareViaEMail() { //implementazione… } ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Java public   class  Cliente { //… public  Vettori[] vettori() { //implementazione… } public  Sconti[] sconti() { //implementazione… } ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Java public   class  Cliente { //… public  Priorita prioritaSchedulazione() { //implementazione… } public  NormeTecniche[] normeParticolari() { //implementazione… } ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net ,[object Object],Cliente ragioneSociale() indirizzo() ClienteContabile bancaDiAppoggio() solvibilita() ClienteCRM agenteDiZona() contattareViaEMail() ClienteProduzione prioritaSchedulazione() normeParticolari() ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Scala case   class   Cliente ( ragioneSociale :  String , indirizzo :  String ) trait   ClienteContabile  { def   bancaDiAppoggio  =  //Implementazione def   solvibilita  =  //Implementazione } trait   ClienteCRM  { def   agenteDiZona  =  //Implementazione def   contattareViaEMail  =  //Implementazione } ,[object Object]
Strumenti per la scalabilità:  Object Orientation con Traits http://www.francolombardo.net //Scala val   cliente  =  new   Cliente ( "Prova s.p.a" , "Via Roma, 1" )  with   ClienteCRM   with   ClienteContabile ,[object Object],Composizione “dinamica” del tipo a seconda delle necessità
Strumenti per la scalabilità:  programmazione funzionale http://www.francolombardo.net ,[object Object],[object Object],Il sistema è decomponibile in termini di funzioni generiche riutilizzabili, riducendo le duplicazioni Higher order functions
Strumenti per la scalabilità:  programmazione funzionale http://www.francolombardo.net ,[object Object],[object Object],E’ più facile riutilizzare le funzioni se non è necessario riprodurre uno stato E’ più facile testare il codice Referential transparency
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net Un esempio: l’integrazione numerica…
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net … scherzavo, facciamo un esempio più “concreto” //Ecco una riga d’ordine case   class   OrderRow ( description :  String ,  price :  Double ,  qty :  Double ) { def   amount  =  price   *   qty } //Ed un ordine val   order  =  List ( OrderRow ( "Beer" , 5.0, 2),  OrderRow ( "Chips" , 2.5, 1))
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net //Un approccio tradizionale per il totale IVA var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total );
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net //Un approccio tradizionale per il totale IVA var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total ); ,[object Object],[object Object]
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net //Un approccio tradizionale per il totale IVA var   total  = 0.0 for  ( row  <-  order ) { total   +=   row . amount   * 0.2 } println ( &quot;VAT (Total): &quot;   +   total ); ,[object Object],[object Object],[object Object]
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net //Un approccio tradizionale per il totale IVA 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]
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net //Il calcolo (è una funzione che assegnamo) val   vat  = ( row :  OrderRow ) =>  row . amount   *  0.2 //Composizione di aggregazione e calcolo 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 )) Scomponiamo le 3 operazioni
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Scomponiamo le 3 operazioni ,[object Object],//Java B b = start; for ( final  A a : listOfA) { b  = method( b ,  a ); } return  b; //Scala listOfA . foldLeft(start)(method)
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Scomponiamo le 3 operazioni ,[object Object],[object Object]
Strumenti per la scalabilità:   programmazione funzionale http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( composition (_ + _,  vat )) Scomponiamo le 3 operazioni ,[object Object],[object Object],[object Object]
Strumenti per la scalabilità:   programmazione funzionale 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 )) Possiamo ricomporre in modo differente
Tipi strutturali http://www.francolombardo.net def   deleteAllRows ( statement :  java . sql . Statement ) =  statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) Come testare questo metodo? Un mock fatto a mano?  L’interfaccia Statement ha 41 metodi!!!
Tipi strutturali http://www.francolombardo.net def   deleteAllRows ( statement : { def   execute ( sql : String):  Boolean }) =  statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) Modifichiamo il nostro metodo dichiarando solo quello che ci occorre
Tipi strutturali 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 ) } Ora possiamo testare facilmente
Conversioni implicite http://www.francolombardo.net class   RemoteStatement  { def   remoteExecute ( sql :  String ) = { println ( sql   +   &quot; executed remotely :-)&quot; ) true } } Se trovassimo una libreria per eseguire query remote?
Conversioni implicite http://www.francolombardo.net implicit   def   normalize ( remote :  RemoteStatement ) = new  {   def   execute ( sql :  String ) = remote . remoteExecute ( sql ) } //Posso invocare anche se il tipo non è corretto! deleteAllRows ( new   RemoteStatement ) Nessun problema: convertiamo al volo!
Domain Specific Languages http://www.francolombardo.net val   tenDollars  =  (4.0  USD )  +  (6.0  USD ) In un programma mi piacerebbe scrivere 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; } Nessun problema!
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 } Nessun problema!
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 ) } } Nessun problema!
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 } } Con questi strumenti possiamo scrivere DSL interni molto interessanti! Esempio di Szymon Jachim Vedi http://www.scala-lang.org/node/1403

Contenu connexe

Tendances

Ajax Primi Passi Per Iniziare
Ajax Primi Passi Per IniziareAjax Primi Passi Per Iniziare
Ajax Primi Passi Per Iniziareastanco
 
Acadevmy - ES6 Modern JS Today
Acadevmy - ES6 Modern JS TodayAcadevmy - ES6 Modern JS Today
Acadevmy - ES6 Modern JS TodayFrancesco Sciuti
 
Dynamic Language Programming For The Statically Typed Programmer
Dynamic Language Programming For The Statically Typed ProgrammerDynamic Language Programming For The Statically Typed Programmer
Dynamic Language Programming For The Statically Typed ProgrammerMarco Parenzan
 
corso web developer - Introduzione a Javascript
corso web developer - Introduzione a Javascriptcorso web developer - Introduzione a Javascript
corso web developer - Introduzione a JavascriptRiccardo Piccioni
 
01 - Ereditarietà e polimorfismo
01 - Ereditarietà e polimorfismo01 - Ereditarietà e polimorfismo
01 - Ereditarietà e polimorfismoFederico Russo
 
JAMP DAY 2010 - ROMA (3)
JAMP DAY 2010 - ROMA (3)JAMP DAY 2010 - ROMA (3)
JAMP DAY 2010 - ROMA (3)jampslide
 
JavaScript Object Oriented
JavaScript Object OrientedJavaScript Object Oriented
JavaScript Object OrientedManuel Scapolan
 
Introduzione a JavaScript
Introduzione a JavaScriptIntroduzione a JavaScript
Introduzione a JavaScriptGiovanni Buffa
 
(My) Best Practices in Symfony
(My) Best Practices in Symfony(My) Best Practices in Symfony
(My) Best Practices in Symfonyinmarelibero
 

Tendances (11)

Ajax Primi Passi Per Iniziare
Ajax Primi Passi Per IniziareAjax Primi Passi Per Iniziare
Ajax Primi Passi Per Iniziare
 
Ajax
AjaxAjax
Ajax
 
Acadevmy - ES6 Modern JS Today
Acadevmy - ES6 Modern JS TodayAcadevmy - ES6 Modern JS Today
Acadevmy - ES6 Modern JS Today
 
Dynamic Language Programming For The Statically Typed Programmer
Dynamic Language Programming For The Statically Typed ProgrammerDynamic Language Programming For The Statically Typed Programmer
Dynamic Language Programming For The Statically Typed Programmer
 
corso web developer - Introduzione a Javascript
corso web developer - Introduzione a Javascriptcorso web developer - Introduzione a Javascript
corso web developer - Introduzione a Javascript
 
01 - Ereditarietà e polimorfismo
01 - Ereditarietà e polimorfismo01 - Ereditarietà e polimorfismo
01 - Ereditarietà e polimorfismo
 
JAMP DAY 2010 - ROMA (3)
JAMP DAY 2010 - ROMA (3)JAMP DAY 2010 - ROMA (3)
JAMP DAY 2010 - ROMA (3)
 
JavaScript Object Oriented
JavaScript Object OrientedJavaScript Object Oriented
JavaScript Object Oriented
 
Introduzione a JavaScript
Introduzione a JavaScriptIntroduzione a JavaScript
Introduzione a JavaScript
 
Pycon 8 python e swift
Pycon 8 python e swiftPycon 8 python e swift
Pycon 8 python e swift
 
(My) Best Practices in Symfony
(My) Best Practices in Symfony(My) Best Practices in Symfony
(My) Best Practices in Symfony
 

En vedette

Rock scissors-paper-kata
Rock scissors-paper-kataRock scissors-paper-kata
Rock scissors-paper-kataFranco Lombardo
 
Science of happiness
Science of happinessScience of happiness
Science of happinessHappiitude
 
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 BrandsAndy Smith
 
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 HappinessStefano Lucantoni
 
Science of Happiness Presentation
Science of Happiness PresentationScience of Happiness Presentation
Science of Happiness PresentationJon Unal
 
The Power of Happiness
The Power of HappinessThe Power of Happiness
The Power of HappinessAndy Smith
 
Happiness Presentation
Happiness PresentationHappiness Presentation
Happiness Presentationrgonzalezjr
 

En vedette (10)

Rock scissors-paper-kata
Rock scissors-paper-kataRock scissors-paper-kata
Rock scissors-paper-kata
 
A First Date With Scala
A First Date With ScalaA First Date With Scala
A First Date With Scala
 
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
 

Similaire à Primo Incontro Con Scala

Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)DotNetMarche
 
Rich Ajax Web Interfaces in Jquery
Rich Ajax Web Interfaces in JqueryRich Ajax Web Interfaces in Jquery
Rich Ajax Web Interfaces in JqueryAlberto Buschettu
 
Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)Davide Cerbo
 
Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)Marcello Missiroli
 
Write less do more...with jQuery
Write less do more...with jQueryWrite less do more...with jQuery
Write less do more...with jQueryXeDotNet
 
Javascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il webJavascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il webRoberto Messora
 
Progettazione e sviluppo di applicazioni web 2.0 con PHP e Ajax
Progettazione e sviluppo di applicazioni web 2.0 con PHP e AjaxProgettazione e sviluppo di applicazioni web 2.0 con PHP e Ajax
Progettazione e sviluppo di applicazioni web 2.0 con PHP e AjaxGiovanni Cappellini
 
Introduzione a jQuery
Introduzione a jQueryIntroduzione a jQuery
Introduzione a jQuerySandro Marcon
 
Introduzione a Node.js
Introduzione a Node.jsIntroduzione a Node.js
Introduzione a Node.jsMichele Capra
 

Similaire à Primo Incontro Con Scala (20)

Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)Deep diving C# 4 (Raffaele Rialdi)
Deep diving C# 4 (Raffaele Rialdi)
 
Applicazioni native in java
Applicazioni native in javaApplicazioni native in java
Applicazioni native in java
 
Rich Ajax Web Interfaces in Jquery
Rich Ajax Web Interfaces in JqueryRich Ajax Web Interfaces in Jquery
Rich Ajax Web Interfaces in Jquery
 
Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)Non Conventional Android Programming (Italiano)
Non Conventional Android Programming (Italiano)
 
Many Designs Elements
Many Designs ElementsMany Designs Elements
Many Designs Elements
 
Html5 e PHP
Html5 e PHPHtml5 e PHP
Html5 e PHP
 
Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)Programmazione a oggetti tramite la macchina del caffé (pt. 2)
Programmazione a oggetti tramite la macchina del caffé (pt. 2)
 
Yagwto
YagwtoYagwto
Yagwto
 
Write less do more...with jQuery
Write less do more...with jQueryWrite less do more...with jQuery
Write less do more...with jQuery
 
Java lezione 14
Java lezione 14Java lezione 14
Java lezione 14
 
Javascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il webJavascript avanzato: sfruttare al massimo il web
Javascript avanzato: sfruttare al massimo il web
 
Java codestyle & tipstricks
Java codestyle & tipstricksJava codestyle & tipstricks
Java codestyle & tipstricks
 
Js intro
Js introJs intro
Js intro
 
Progettazione e sviluppo di applicazioni web 2.0 con PHP e Ajax
Progettazione e sviluppo di applicazioni web 2.0 con PHP e AjaxProgettazione e sviluppo di applicazioni web 2.0 con PHP e Ajax
Progettazione e sviluppo di applicazioni web 2.0 con PHP e Ajax
 
Introduzione a jQuery
Introduzione a jQueryIntroduzione a jQuery
Introduzione a jQuery
 
Introduzione a node.js
Introduzione a node.jsIntroduzione a node.js
Introduzione a node.js
 
Introduzione a Node.js
Introduzione a Node.jsIntroduzione a Node.js
Introduzione a Node.js
 
8. Architetture web
8. Architetture web8. Architetture web
8. Architetture web
 
#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2#dd12 grillo daniele_xpages_tips_tricks_rev2
#dd12 grillo daniele_xpages_tips_tricks_rev2
 
Corso Javascript
Corso JavascriptCorso Javascript
Corso Javascript
 

Plus de Franco Lombardo

Kotlin from-scratch 3 - coroutines
Kotlin from-scratch 3 - coroutinesKotlin from-scratch 3 - coroutines
Kotlin from-scratch 3 - coroutinesFranco Lombardo
 
Kotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functionsKotlin from-scratch 2 - functions
Kotlin from-scratch 2 - functionsFranco Lombardo
 
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)Franco Lombardo
 
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)Franco Lombardo
 
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...Franco Lombardo
 
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 2019Franco Lombardo
 
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 iFranco Lombardo
 
Agile Happiness - Agile O'Day 2018
Agile Happiness - Agile O'Day 2018Agile Happiness - Agile O'Day 2018
Agile Happiness - Agile O'Day 2018Franco Lombardo
 

Plus de 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
 

Primo Incontro Con Scala

  • 1. Primo incontro con Scala Franco Lombardo XP User Group - Bergamo http://www.francolombardo.net Edward Hopper - Summertime
  • 3.
  • 4.
  • 5. Finalmente 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. Finalmente 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 ()) } Sintassi base molto simile a Java
  • 7. Finalmente 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 ()) } Oggetti di java.lang importati di default
  • 8. Finalmente 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 ()) } Oggetti librerie Java importabili facilmente
  • 9. Finalmente 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 ()) } Sintassi semplificata: punto e virgola non obbligatorio
  • 10. Finalmente 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 ()) } Sintassi semplificata: meno codice
  • 11. Finalmente 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 ()) } Sintassi semplificata: inferenza dei tipi
  • 12. Inferenza dei tipi: una novità? http://www.francolombardo.net //Java return cliente.getOrdine(40) .getRiga(20) .getArticolo() .getPeso() * 2.5; (OK, questo esempio non rispetta la legge di Demeter, ma è solo un esempio…  )
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26. Strumenti per la scalabilità: programmazione funzionale http://www.francolombardo.net Un esempio: l’integrazione numerica…
  • 27. Strumenti per la scalabilità: programmazione funzionale http://www.francolombardo.net … scherzavo, facciamo un esempio più “concreto” //Ecco una riga d’ordine case class OrderRow ( description : String , price : Double , qty : Double ) { def amount = price * qty } //Ed un ordine val order = List ( OrderRow ( &quot;Beer&quot; , 5.0, 2), OrderRow ( &quot;Chips&quot; , 2.5, 1))
  • 28. Strumenti per la scalabilità: programmazione funzionale http://www.francolombardo.net //Un approccio tradizionale per il totale IVA var total = 0.0 for ( row <- order ) { total += row . amount * 0.2 } println ( &quot;VAT (Total): &quot; + total );
  • 29.
  • 30.
  • 31.
  • 32. Strumenti per la scalabilità: programmazione funzionale http://www.francolombardo.net //Il calcolo (è una funzione che assegnamo) val vat = ( row : OrderRow ) => row . amount * 0.2 //Composizione di aggregazione e calcolo 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 )) Scomponiamo le 3 operazioni
  • 33.
  • 34.
  • 35.
  • 36. Strumenti per la scalabilità: programmazione funzionale 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 )) Possiamo ricomporre in modo differente
  • 37. Tipi strutturali http://www.francolombardo.net def deleteAllRows ( statement : java . sql . Statement ) = statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) Come testare questo metodo? Un mock fatto a mano? L’interfaccia Statement ha 41 metodi!!!
  • 38. Tipi strutturali http://www.francolombardo.net def deleteAllRows ( statement : { def execute ( sql : String): Boolean }) = statement . execute ( &quot;DELETE FROM MYTABLE&quot; ) Modifichiamo il nostro metodo dichiarando solo quello che ci occorre
  • 39. Tipi strutturali 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 ) } Ora possiamo testare facilmente
  • 40. Conversioni implicite http://www.francolombardo.net class RemoteStatement { def remoteExecute ( sql : String ) = { println ( sql + &quot; executed remotely :-)&quot; ) true } } Se trovassimo una libreria per eseguire query remote?
  • 41. Conversioni implicite http://www.francolombardo.net implicit def normalize ( remote : RemoteStatement ) = new { def execute ( sql : String ) = remote . remoteExecute ( sql ) } //Posso invocare anche se il tipo non è corretto! deleteAllRows ( new RemoteStatement ) Nessun problema: convertiamo al volo!
  • 42. Domain Specific Languages http://www.francolombardo.net val tenDollars = (4.0 USD ) + (6.0 USD ) In un programma mi piacerebbe scrivere 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; } Nessun problema!
  • 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 } Nessun problema!
  • 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 ) } } Nessun problema!
  • 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 } } Con questi strumenti possiamo scrivere DSL interni molto interessanti! Esempio di Szymon Jachim Vedi http://www.scala-lang.org/node/1403