Scala the next … ?<br />ŁukaszKuczera<br />
Java<br />
PHP<br />
Python<br />
Scala<br />Hybrid Object-Functional <br />With type inferention<br />Duck typing<br />And multiple inheritance<br />Compil...
Scalable<br />
History<br />
1995<br />Well no but, lets do some functional programming in Java !!<br />Have you seen Martin this Java thing that “runs...
Pizza<br />
Martin Odersky<br />GiladBracha<br />PhillWadler<br />David Stoutamire<br />Scala<br />Newspeak<br />Haskell<br />
Funnel – 1999Scala – 2003<br />
Scala bread and butter<br />
IDE<br />  Eclipse<br />NetBeans<br />  IDEA<br />Emacs<br />  Vim<br />TextMate<br />
Enough of blabberingshow us the code !<br />
class Test {<br />// variable definition<br />varn: Int= 5<br />// value definition<br />vali= 10<br />// function definit...
Objects and Operators<br /> 3 +4<br /> 5 % 2 <br />3 * 3<br />valtest =new Test<br />test printit"hello world !”<br /> cla...
Rational Example<br />classRational(vali:Int,val n:Int){<br />defthis(i:Int)=this(i, 1)<br />def*(other:Rational)=new Rati...
Do not underestimate the power of Scala !<br />
Implicit definitions unleashed<br />classRational(vali:Int,val n:Int){<br />defthis(i:Int)=this(i, 1)<br />def*(other:Rati...
Pimp my librarypattern<br />
DSL<br />
 Internal DSL’s in Scala<br />Menu.i("Home")/”home” / *>>loggedIn>>User.AddUserMenusAfter<br />employee salary for 2.weeks...
object Lunar extendsBaysick{<br />def main(args:Array[String])={<br />    10 PRINT "Welcome to Baysick Lunar Lander v0.9"<...
And JavaScript<br />AppendHtml("comments",<xml:group><br /><divclass={className}><br /><span>{privateSpanText}</span><br/>...
Class Parameters - Java<br />publicclassPerson{<br />privateStringname;<br />privateintage;<br />publicPerson(String name,...
Class Parameters - Scala<br />  classPerson(var name: String,var age:Int)<br />
Partition - Java<br />classPartition{<br />List<Person>all;<br />List<Person>adults=newArrayList<Person>();<br />List<Pers...
Partition - Scala<br />valall = Array[Person]()<br />val(minors, adults)=all.partition(_.age < 18)<br />
Reduce - Java<br />classReduce{<br />List<Person>all;<br />intsum=0;<br />for(Personp: all){<br />sum+=p.getAge();<br />}<...
Reduce - Scala<br />valall = Array[Person]()<br />all.map(_.age).reduce(_+_)<br />
Sort - Java<br />classSort{<br />List<Person>all;<br />Comparator<Person>comp=new Comparator<Person>(){<br />publicint com...
Sort - Scala<br />valall = Array[Person]()<br />all.sortBy(_.age)<br />
Objects as Functions<br /> valadder =(x: Int)=> x + 1<br />val anonfun1 =new Function1[Int, Int]{<br />def apply(x:Int):In...
Functions as Objects<br /> valadder =(x: Int)=> x + 1<br />adder.apply(1)// == 2<br />adder.apply(5) // == 6<br />defhighe...
Structural (Duck) Typing<br />defdoIn(resource:{def open(): Unit;def close(): Unit }){<br />resource.open();<br />// do so...
Multiple parameter lists<br />defdoIn(code:()=> Unit)<br />  (resource:{def open(): Unit;def close(): Unit }){<br />resour...
Implicit Parameters<br />defdoIn(code:()=> Unit)<br />(implicit resource:{def open(): Unit;def close(): Unit }){<br />reso...
XML<br />val person =<personname="ŁukaszKuczera"><br /><addressvoivodeship="Pomorskie"><br /><city>Gdańsk</city><br /></ad...
val xml =<br /><divclass="rsswidget"><br /><ul><br /><li><ahref="http://www.quotationspage.com/qotd.html"><br />Quotes of ...
valxml =<br /><divclass="rsswidget"><br /><ul><li><br />       <ahref="http://www.quotationspage.com/qotd.html">Quotes of ...
Nulls - Java<br />Map<String, String>capitals=<br />newHashMap<String, String>();<br />capitals.put("Poland","Warsaw");<br...
Nulls - Scala<br />val capitals = Map("Poland"->"Warsaw");<br />valcapitalOption:Option[String]=capitals.get("Polska")<br ...
Who uses Scala already<br />
If I where to choose language other than<br />Java it would be <br />Scala<br />
„I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back...
„Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable ...
Scala Features<br />TypeInference<br />Uniform Access Principle<br />Closures<br />Curying<br />Higher Order Functions<br ...
Join me and together we will rule the galaxy !<br />
About Me	<br />l.kuczera@jextreme.pl<br />lkuczera<br />              http://acidbits.org/blog<br />              http://g...
Upcoming SlideShare
Loading in...5
×

Scala 3camp 2011

1,134

Published on

Scala presentation for 3camp June 2011

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,134
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Skalujesię do potrzeb I umiejętnościObiektowość – dobra do tworzeniaabstrakcji, dużychsystemówFunkcyjność – dobra w małejskali
  • Skalujesię do potrzeb I umiejętnościObiektowość – dobra do tworzeniaabstrakcji, dużychsystemówFunkcyjność – dobra w małejskali
  • Skalujesię do potrzeb I umiejętnościObiektowość – dobra do tworzeniaabstrakcji, dużychsystemówFunkcyjność – dobra w małejskali
  • Skalujesię do potrzeb I umiejętnościObiektowość – dobra do tworzeniaabstrakcji, dużychsystemówFunkcyjność – dobra w małejskali
  • David pollackMesa spreadsheet
  • David pollackMesa spreadsheet
  • David pollackMesa spreadsheet
  • David pollackMesa spreadsheet
  • Wszystko jestobiektem, braktypówprymitywnychNie ma operatorów – wszystko jest wywołaniemmetod
  • Wszystko jestobiektem, braktypówprymitywnychNie ma operatorów – wszystko jest wywołaniemmetod
  • Wszystko jestobiektem, braktypówprymitywnychNie ma operatorów – wszystko jest wywołaniemmetod
  • Pozwalanarozszerzanieistniejącychklas
  • Pozwalanarozszerzanieistniejącychklasbezkoniecznościzmian
  • W czym to jest napisane ?
  • Obie formysąrównoznaczne
  • dfasdfadfladkj
  • Scala 3camp 2011

    1. 1. Scala the next … ?<br />ŁukaszKuczera<br />
    2. 2. Java<br />
    3. 3. PHP<br />
    4. 4. Python<br />
    5. 5. Scala<br />Hybrid Object-Functional <br />With type inferention<br />Duck typing<br />And multiple inheritance<br />Compiles to JVM and CLR<br />
    6. 6. Scalable<br />
    7. 7. History<br />
    8. 8. 1995<br />Well no but, lets do some functional programming in Java !!<br />Have you seen Martin this Java thing that “runs everywhere” ?<br />
    9. 9. Pizza<br />
    10. 10. Martin Odersky<br />GiladBracha<br />PhillWadler<br />David Stoutamire<br />Scala<br />Newspeak<br />Haskell<br />
    11. 11.
    12. 12. Funnel – 1999Scala – 2003<br />
    13. 13. Scala bread and butter<br />
    14. 14. IDE<br /> Eclipse<br />NetBeans<br /> IDEA<br />Emacs<br /> Vim<br />TextMate<br />
    15. 15. Enough of blabberingshow us the code !<br />
    16. 16. class Test {<br />// variable definition<br />varn: Int= 5<br />// value definition<br />vali= 10<br />// function definition<br />defsum(i:Int, n:Int):Int=i+n<br />defprintit(x: Any)=println(x)<br />}<br /> <br />object Basics extends Test {<br />overridevali= 20<br />overridedef sum(i:Int, n:Int):Int=i+n<br />def main(args: Array[String]){<br />printit(sum(i,n))<br />}<br />}<br />
    17. 17. Objects and Operators<br /> 3 +4<br /> 5 % 2 <br />3 * 3<br />valtest =new Test<br />test printit"hello world !”<br /> classOpOverloading{<br />def+(x: Any)=println("I'm + function/operator")<br /> }<br />== 3.+(4)<br />==5.%(2)<br />== 3.*(3)<br />
    18. 18. Rational Example<br />classRational(vali:Int,val n:Int){<br />defthis(i:Int)=this(i, 1)<br />def*(other:Rational)=new Rational(i*other.i, n *other.n)<br />overridedeftoString=i+"/"+ n<br />def*(other:Int)=newRational(i* other, n)<br />}<br />valthird =new Rational(1, 3)<br />val quarter =new Rational(1, 4)<br /> third * quarter // == 1/12<br /> third *3 // compilation error !<br /> third * 3 // == 1/9<br /> 3 * quarter // compilation error !<br />
    19. 19. Do not underestimate the power of Scala !<br />
    20. 20. Implicit definitions unleashed<br />classRational(vali:Int,val n:Int){<br />defthis(i:Int)=this(i, 1)<br />def*(other:Rational)=new Rational(i*other.i, n *other.n)<br />overridedeftoString=i+"/"+ n<br />def*(other:Int)=newRational(i* other, n)<br />}<br />valthird =new Rational(1, 3)<br />val quarter =new Rational(1, 4)<br /> third * quarter // == 1/12<br /> third * 3 // compilation error !<br /> third * 3 // == 1/9<br /> 3 * quarter // compilation error<br />implicitdef int2rational(i:Int)=new Rational(i)<br /> 3 * quarter // == 3/4<br />
    21. 21. Pimp my librarypattern<br />
    22. 22. DSL<br />
    23. 23. Internal DSL’s in Scala<br />Menu.i("Home")/”home” / *>>loggedIn>>User.AddUserMenusAfter<br />employee salary for 2.weeks minus deductions for{ gross =><br />federalIncomeTax is (25. percent_of gross)<br />stateIncomeTax is (5. percent_of gross)<br />insurancePremiums are (500. in gross.currency)<br />retirementFundContributions are (10. percent_of gross)<br />}<br />
    24. 24. object Lunar extendsBaysick{<br />def main(args:Array[String])={<br /> 10 PRINT "Welcome to Baysick Lunar Lander v0.9"<br /> 20 LET ('dist:= 100)<br /> 30 LET ('v := 1)<br /> 40 LET ('fuel := 1000)<br /> 50 LET ('mass:=1000)<br />60 PRINT "Youaredriftingtowardsthemoon."<br /> 70 PRINT "You must decidehowmuchfueltoburn."<br /> 80 PRINT "Toaccelerateenter a positive number"<br /> 90 PRINT "Todecelerate a negative"<br />100 PRINT "Distance "% 'dist%"km, "%"Velocity "% 'v %"km/s, "%"Fuel "% 'fuel<br /> 110 INPUT 'burn<br /> 120 IF ABS('burn)<= 'fuel THEN 150<br /> 130 PRINT "Youdon'thavethatmuch fuel"<br /> 140 GOTO 100<br /> 150 LET ('v := 'v + 'burn* 10 /('fuel + 'mass))<br /> 160 LET ('fuel := 'fuel - ABS('burn))<br /> 170 LET ('dist:= 'dist- 'v)<br /> 180 IF 'dist> 0 THEN 100<br /> 190 PRINT "Youhave hit thesurface"<br /> 200 IF 'v < 3 THEN 240<br /> 210 PRINT "Hit surface too fast ("% 'v %")km/s"<br /> 220 PRINT "You Crashed!"<br /> 230 GOTO 250<br /> 240 PRINT "Welldone"<br />250 END<br />RUN<br />}<br />}<br />
    25. 25. And JavaScript<br />AppendHtml("comments",<xml:group><br /><divclass={className}><br /><span>{privateSpanText}</span><br/><br /><b>Author:</b>{user.firstName}{user.lastName}<br /><span>added at:{comm.createdAt}</span><br/><br /><div>{ Unparsed(comm.text)}</div><br /></div><br /></xml:group>)&<br />FadeOut("commentInput", 200, 200)&<br />FadeOut("privateCommentField",200, 200)&<br />Run("document.getElementById('private').checked = false;")&<br />Run("document.getElementById('uniform-private').childNodes [0].removeAttribute('class');")&<br />FadeOut("commentPrivateBox", 200, 200)&<br />Run("tinyMCE.get('commentEditor').setContent('')")<br />
    26. 26. Class Parameters - Java<br />publicclassPerson{<br />privateStringname;<br />privateintage;<br />publicPerson(String name,int age){<br />this.name= name;<br />this.age= age;<br />}<br />publicStringgetName(){<br />returnname;<br />}<br />publicvoidsetName(String name){<br />this.name= name;<br />}<br />publicintgetAge(){<br />returnage;<br />}<br />publicvoidsetAge(int age){<br />this.age= age;<br />}<br />}<br />
    27. 27. Class Parameters - Scala<br /> classPerson(var name: String,var age:Int)<br />
    28. 28. Partition - Java<br />classPartition{<br />List<Person>all;<br />List<Person>adults=newArrayList<Person>();<br />List<Person>minors=newArrayList<Person>();<br />for(Personp: all){<br />if(p.getAge()<18){<br />minors.add(p);<br />}else{<br />adults.add(p);<br />}<br />}<br />}<br />
    29. 29. Partition - Scala<br />valall = Array[Person]()<br />val(minors, adults)=all.partition(_.age < 18)<br />
    30. 30. Reduce - Java<br />classReduce{<br />List<Person>all;<br />intsum=0;<br />for(Personp: all){<br />sum+=p.getAge();<br />}<br />}<br />
    31. 31. Reduce - Scala<br />valall = Array[Person]()<br />all.map(_.age).reduce(_+_)<br />
    32. 32. Sort - Java<br />classSort{<br />List<Person>all;<br />Comparator<Person>comp=new Comparator<Person>(){<br />publicint compare(p1: Person, p2: Person){<br />return p1.getAge()- p2.getAge();<br />}<br />};<br />Collections.sort(all, comp);<br />}<br />
    33. 33. Sort - Scala<br />valall = Array[Person]()<br />all.sortBy(_.age)<br />
    34. 34. Objects as Functions<br /> valadder =(x: Int)=> x + 1<br />val anonfun1 =new Function1[Int, Int]{<br />def apply(x:Int):Int= x + 1<br />}<br /> adder(1) // == 2<br /> anonfun1(5) // == 6<br />
    35. 35. Functions as Objects<br /> valadder =(x: Int)=> x + 1<br />adder.apply(1)// == 2<br />adder.apply(5) // == 6<br />defhigherOrderFunction(f:(Int=>Int), x:Int)= f(x)<br />higherOrderFunction(adder, 1)<br />
    36. 36. Structural (Duck) Typing<br />defdoIn(resource:{def open(): Unit;def close(): Unit }){<br />resource.open();<br />// do some useful stuff ... <br />resource.close();<br />}<br />val a =newScalaObject{<br />def open(){}<br />def close(){}<br />}<br />val b =newScalaObject{<br />def open(){}<br />// def close() {}<br />}<br />doIn(a)<br />doIn(b)// compilation error<br />
    37. 37. Multiple parameter lists<br />defdoIn(code:()=> Unit)<br /> (resource:{def open(): Unit;def close(): Unit }){<br />resource.open();<br /> code()<br />resource.close();<br />}<br />doIn(()=>{<br />// do some useful stuff ...<br />})(a)<br />defmyUsefulFunc(){<br />// lots of lots of wonderful code ..<br />}<br />doIn(myUsefulFunc)(a)<br />
    38. 38. Implicit Parameters<br />defdoIn(code:()=> Unit)<br />(implicit resource:{def open(): Unit;def close(): Unit }){<br />resource.open();<br /> code()<br />resource.close();<br />}<br />implicitval a =newScalaObject{<br />def open(){}<br />def close(){}<br />}<br />doIn(()=>{<br />// do some useful stuff ...<br />}) <br />(a)<br />
    39. 39. XML<br />val person =<personname="ŁukaszKuczera"><br /><addressvoivodeship="Pomorskie"><br /><city>Gdańsk</city><br /></address><br /></person><br />valname = person "@name”<br />valaddress = person "address"<br />valcity = address "city”<br />val_city = person "city"<br />
    40. 40. val xml =<br /><divclass="rsswidget"><br /><ul><br /><li><ahref="http://www.quotationspage.com/qotd.html"><br />Quotes of the Day</a></li><br /><li><ahref="http://www.quotationspage.com/quotes/C._P._Snow"><br />C. P. Snow</a></li><br /><li><ahref="http://www.quotationspage.com/quotes/ unknown">unknown</a></li><br /><li><ahref="http://www.quotationspage.com/quotes/Frederick_Locker- Lampson">Frederick Locker-Lampson</a></li><br /></ul><br /> </div><br />(xml "@href").slice(1, 4).foreach(url=>{<br />val quote =Source.fromURL(new URL(url.text)).mkString<br />println(quote)<br />})<br />
    41. 41. valxml =<br /><divclass="rsswidget"><br /><ul><li><br /> <ahref="http://www.quotationspage.com/qotd.html">Quotes of the Day</a></li><br /><li><ahref="http://www.quotationspage.com/quotes/C._P._Snow"><br />C. P. Snow</a></li><br /><li><ahref="http://www.quotationspage.com/quotes/unknown">unknown</a></li><br /><li><ahref="http://www.quotationspage.com/quotes/Frederick_Locker- Lampson">Frederick Locker-Lampson</a></li><br /></ul><br /> </div><br />(xml "@href").slice(1, 4).foreach(url=>{<br />valcon =new URL(url.toString).openConnection<br />val reader =newBufferedReader(newInputStreamReader(con.getInputStream,"ISO-8859-1"))<br />var line ="";<br />while(line !=null){<br /> line =reader.readLine<br />if(line !=null)println(line)<br />}<br />con.getInputStream.close<br />})<br />
    42. 42. Nulls - Java<br />Map<String, String>capitals=<br />newHashMap<String, String>();<br />capitals.put("Poland","Warsaw");<br />System.out.println(capitals.get("Polska").trim());<br />Exceptionin thread "main"java.lang.NullPointerException<br />
    43. 43. Nulls - Scala<br />val capitals = Map("Poland"->"Warsaw");<br />valcapitalOption:Option[String]=capitals.get("Polska")<br />capitalOptionmatch{<br />caseSome(value)=>println(value)<br />caseNone =>println("Not found")<br />case_=><br />}<br />if(capitalOption.isDefined)println(capitalOption.get)<br />println(capitalOptiongetOrElse"Not found")<br />
    44. 44. Who uses Scala already<br />
    45. 45. If I where to choose language other than<br />Java it would be <br />Scala<br />
    46. 46. „I can honestly say if someone had shown me the Programming in Scala book by Martin Odersky, Lex Spoon & Bill Venners back in 2003 I'd probably have never created <br />Groovy”<br />
    47. 47. „Scala, it must be stated, is the current heir apparent to the Java throne. No other language on the JVM seems as capable of being a "replacement for Java" as Scala, and the momentum behind Scala is now unquestionable”<br />
    48. 48. Scala Features<br />TypeInference<br />Uniform Access Principle<br />Closures<br />Curying<br />Higher Order Functions<br />PatternMatching<br />Actors<br />Generics (covariance, higher order kinds)<br />Native XML support<br />Abstractcontrolstructures<br />Implicitconversions and parameters<br />Advanced for expressions<br />Annotations<br />Combinatorparsing<br />Traits<br />Ducktyping<br />Null „safety” (Option Type)<br />
    49. 49. Join me and together we will rule the galaxy !<br />
    50. 50. About Me <br />l.kuczera@jextreme.pl<br />lkuczera<br /> http://acidbits.org/blog<br /> http://github.com/lkuczera/lift-blog<br />
    1. A particular slide catching your eye?

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

    ×