Your SlideShare is downloading. ×

Scala 3camp 2011

1,098

Published on

Scala presentation for 3camp June 2011

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,098
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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
  • Transcript

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

    ×