FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
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 ( "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
6. 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
7. 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
8. 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
9. 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
10. 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
11. 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
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 ( "Beer" , 5.0, 2), OrderRow ( "Chips" , 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 ( "VAT (Total): " + 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 ( "DELETE FROM MYTABLE" ) 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 ( "DELETE FROM MYTABLE" ) 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 + " executed remotely :-)" ) 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 = "EUR" } object Dollar extends Currency { def name = "USD" } 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 + " " + 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 "SCALA ROCKS!" 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