Your SlideShare is downloading. ×
0
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 <ul><li>IT was born in 2001 at  the  Ecoles Polytechniques fédérales de Lausanne </li></ul><ul><li>Firs pu...
Scala = SCAlable LAnguage http://www.francolombardo.net In order to “scale” you need solid basis! <ul><li>Widespread virtu...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Here is our star: Hello World!!! http://www.francolombardo.net package   hello import   java . util .Date object   Hello  ...
Type inference: have you ever seen it? http://www.francolombardo.net //Java return  customer.getOrder(40) .getRow(20) .get...
Tools for scalability:  static typing http://www.francolombardo.net <ul><li>The compiler gives lots of automatic tests on ...
Tools for scalbility:  “pure” Object Orientation http://www.francolombardo.net <ul><li>Every value is an object </li></ul>...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net <ul><li>A problem of big Object Orien...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { pub...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //…...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //…...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //…...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Java public   class  Customer { //…...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net <ul><li>Fat objects: does inheritance...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Scala case   class   Customer ( nam...
Tools for scalability:  Object Orientation with Traits http://www.francolombardo.net //Scala val   customer  =  new   Cust...
Tools for scalability:  functional programming http://www.francolombardo.net <ul><li>Functions are base  objects  of the l...
Tools for scalability:  functional programming http://www.francolombardo.net <ul><li>Functions don’t have a state: their r...
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” e...
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation v...
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation v...
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation v...
Tools for scalability:  functional programming http://www.francolombardo.net //A traditional approach to VAT computation v...
Tools for scalability:  functional programming http://www.francolombardo.net //The computation: it’s a function we can ass...
Tools for scalability:  functional programming http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( compo...
Tools for scalability:  functional programming http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( compo...
Tools for scalability:  functional programming http://www.francolombardo.net val  totalVat = order . foldLeft (0.0)( compo...
Tools for scalability:  functional programming http://www.francolombardo.net val  totalAmount = order . foldLeft (0.0)( co...
Structural types http://www.francolombardo.net def   deleteAllRows ( statement :  java . sql . Statement ) =  statement . ...
Structural types http://www.francolombardo.net def   deleteAllRows ( statement : { def   execute ( sql : String):  Boolean...
Structural types http://www.francolombardo.net def   testDeleteAllRows () { val   mockStatement  =  new  { def   execute (...
Implicit conversions http://www.francolombardo.net class   RemoteStatement  { def   remoteExecute ( sql :  String ) = { pr...
Implicit conversions http://www.francolombardo.net implicit   def   normalize ( remote :  RemoteStatement ) = new  {   def...
Domain Specific Languages http://www.francolombardo.net val   tenDollars  =  (4.0  USD )  +  (6.0  USD ) I’d like to write...
Domain Specific Languages http://www.francolombardo.net abstract   class   Currency  { def   name :  String ; override   d...
Domain Specific Languages http://www.francolombardo.net case   class   Money [ C  <:  Currency ]( amount :  Double ,   cur...
Domain Specific Languages http://www.francolombardo.net object   Money  { implicit   def   doubleConverter ( d :  Double )...
Domain Specific Languages http://www.francolombardo.net object   Playground   extends   BasicClass  { def   main ( args : ...
Upcoming SlideShare
Loading in...5
×

A First Date With Scala

1,871

Published on

An introduction to the Scala programming language

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
  • A First Date With Scala
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
1,871
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
42
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "A First Date With Scala"

  1. 1. A first date with Scala Franco Lombardo XP User Group - Bergamo http://www.francolombardo.net Edward Hopper - Summertime
  2. 2. First of all, you must know… http://www.francolombardo.net … I’m not a Guru!!!
  3. 3. Scala: passport <ul><li>IT was born in 2001 at the Ecoles Polytechniques fédérales de Lausanne </li></ul><ul><li>Firs public release in 2004 </li></ul><ul><li>Ideated by Martin Odersky, the father of Java “Generics” </li></ul><ul><li>The compiler generates .class file for the JVM (someone says there’s also a version for .NET  ) </li></ul>http://www.francolombardo.net Claudio Destito – Auto-ritratto
  4. 4. Scala = SCAlable LAnguage http://www.francolombardo.net In order to “scale” you need solid basis! <ul><li>Widespread virtual machine </li></ul><ul><li>Libraries/frameworks/application servers created in Java are immediately available in Scala </li></ul><ul><li>Base syntax very close to Java, so it’s very appealing for a great number of programmers </li></ul>The base of Scala is Java
  5. 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. 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. 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. 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. 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. 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. 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. 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. 13. Tools for scalability: static typing http://www.francolombardo.net <ul><li>The compiler gives lots of automatic tests on the code for free </li></ul><ul><li>Enhanced refactorings </li></ul><ul><li>Better average performances (OK, not always, but Scala substituted Ruby at Twitter for performances issues) </li></ul><ul><li>Static typing is a clear and automatic tool for code documentation </li></ul>
  14. 14. Tools for scalbility: “pure” Object Orientation http://www.francolombardo.net <ul><li>Every value is an object </li></ul><ul><li>Every operation is a method call (so we can redefine operators easily) </li></ul>2 + 5 //equals to... 2. + (5) <ul><li>Syntactic tricks to improve readability: </li></ul><ul><li>Starting method names with letters is not mandatory </li></ul><ul><li>In some cases points and parenthesis are not mandatory </li></ul>
  15. 15. Tools for scalability: Object Orientation with Traits http://www.francolombardo.net <ul><li>A problem of big Object Oriented systems: objects that can become very fat </li></ul>Fernando Botero – Bailerina na barra
  16. 16. 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 } <ul><li>Fat objects: the Customer </li></ul>
  17. 17. 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… } <ul><li>Fat objects: the Customer for accounting purposes </li></ul>
  18. 18. 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… } <ul><li>Fat Objects: the Customer for the CRM system </li></ul>
  19. 19. 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… } <ul><li>Fat objects: the Customer for orders </li></ul>
  20. 20. 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… } <ul><li>Fat objects: the Customer for production </li></ul>
  21. 21. Tools for scalability: Object Orientation with Traits http://www.francolombardo.net <ul><li>Fat objects: does inheritance help? </li></ul>Customer name() address() AccountingCustomer preferredBank() solvability() CRMCustomer zoneAgent() isToSendEMails() ProductionCustomer schedulingPriority() standards() <ul><li>What if I needed the preferred bank in the CRM module? </li></ul>
  22. 22. 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 } <ul><li>Fat objects: decomposing with Traits </li></ul>
  23. 23. Tools for scalability: Object Orientation with Traits http://www.francolombardo.net //Scala val customer = new Customer ( “Lombardo ltd&quot; , “ Wall Street, 1&quot; ) with CRMCustomer with AccountingCustomer <ul><li>Fat objects: decomposing with Traits </li></ul>“ Dynamic” type composition
  24. 24. Tools for scalability: functional programming http://www.francolombardo.net <ul><li>Functions are base objects of the language, as strings and numbers </li></ul><ul><li>Therefore functions can be arguments and return values of method calls </li></ul>You can decompose the system in generic functions which you can reuse reducing duplications Higher order functions
  25. 25. Tools for scalability: functional programming http://www.francolombardo.net <ul><li>Functions don’t have a state: their results are based only on input values. </li></ul><ul><li>A function evaluation has no side effects beside the computation of the result </li></ul><ul><li>Reusing functions is simpler if we don’t need to reproduce a particular state </li></ul><ul><li>We can test code more easily </li></ul>Referential transparency
  26. 26. Tools for scalability: functional programming http://www.francolombardo.net An example: numeric integration…
  27. 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. 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. 29. 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 ); <ul><li>We merge 3 operations </li></ul><ul><li>Loop </li></ul>
  30. 30. 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 ); <ul><li>We merge 3 operations </li></ul><ul><li>Loop </li></ul><ul><li>Accumulation </li></ul>
  31. 31. 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 ); <ul><li>We merge 3 operations </li></ul><ul><li>Loop </li></ul><ul><li>Accumulation </li></ul><ul><li>Computation </li></ul>
  32. 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. 33. Tools for scalability: functional programming http://www.francolombardo.net val totalVat = order . foldLeft (0.0)( composition (_ + _, vat )) Let’s decompose these 3 operations <ul><li>Loop </li></ul>//Java B b = start; for ( final A a : listOfA) { b = method( b , a ); } return b; //Scala listOfA . foldLeft(start)(method)
  34. 34. Tools for scalability: functional programming http://www.francolombardo.net val totalVat = order . foldLeft (0.0)( composition (_ + _, vat )) Let’s decompose these 3 operations <ul><li>Loop </li></ul><ul><li>Accumulation </li></ul>
  35. 35. Tools for scalability: functional programming http://www.francolombardo.net val totalVat = order . foldLeft (0.0)( composition (_ + _, vat )) Let’s decompose these 3 operations <ul><li>Loop </li></ul><ul><li>Accumulation </li></ul><ul><li>Computation </li></ul>
  36. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×