From Python
  to Scala

 Sébastien Pierre, ffunction inc.
@Montréal Python, April 2010

         www.ffctn.com


                                    ffunction
                                    inc.
Stuff I love about Python



●
    Expressive (and compact)
●
    Simple to learn
●
    Lots of smartly designed libraries




                                         ffunction
                                         inc.
Stuff that I miss in Python



●
    Closures (with syntax)
●
    Easy concurrency (model and syntax)
●
    … a compiler !!




                                          ffunction
                                          inc.
Scala - overview




                   ffunction
                   inc.
The language



●
    JVM-based, compiled + « interpreted »
●
    Functional + OO (post-functional)
●
    ~6 years old
●
    used by Twitter, FourSquare




                                            ffunction
                                            inc.
The library



●
    Anything in Java
●
    Immutable types library (safe for concurrent apps)
●
    Concurrency + actor model
●
    Quality community-contributed libraries




                                                    ffunction
                                                    inc.
The feel



●
    Make do with braces
●
    map/filter/apply (and fold)
●
    ~25% more lines than with Python
●
    Very good DSL-ability
●
    Fast !


                                       ffunction
                                       inc.
Scala – basics




                 ffunction
                 inc.
Rich types

        Scala               Python


Array   Array(1,2,3,4)      (1,2,3,4]


Tuple   (1,2,3,4)           (1,2,3,4)


List    List(1,2,3,4)       (1,2,3,4)


Map     Map(“a”>1, “b”>2)   {“a”:1, “c”:2}


Set     Set(1,2,3,4)        set(1,2,3,4)


                                             ffunction
                                             inc.
Closures


[1,2,3] map            { i => i + 1 }

{i:Int => i + 1 }                       # 1-param normal

(i:Int) => { i + 1 }                    # n-param normal

[1,2,3] map { _ + 1 }                   # implicit args


val f = { i:Int => i + 1}




                                                           ffunction
                                                           inc.
Classes + traits


class A
 {…}




                   ffunction
                   inc.
Classes + traits


 class A
  {…}




  class B
extends A
   {…}




                   ffunction
                   inc.
Classes + traits


 class A
  {…}




 class B
extends A   trait T
 with T      {…}
  {…}




                      ffunction
                      inc.
Classes + traits


 class A      trait U
  {…}          {…}




 class B
              trait T
extends A
            extends U
  with T
               {…}
  {…}




                        ffunction
                        inc.
Classes + traits


 class A      trait U
  {…}          {…}




 class B
              trait T
extends A
            extends U
  with T
               {…}
  {…}




                        ffunction
                        inc.
Batteries included
scala.xml.XML.load(
     "http://ffunction.posterous.com/rss.xml"
)  "item"  "title" toList

List[scala.xml.Node] = List(<title xmlns:media="http://search.yahoo.com/mrss/" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:posterous="http://posterous.com/help/rss/1.0" 
xmlns:atom="http://www.w3.org/2005/Atom">Notes on creating a multi­touch 
interface</title>, <title xmlns:media="http://search.yahoo.com/mrss/" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:posterous="http://posterous.com/help/rss/1.0" 
xmlns:atom="http://www.w3.org/2005/Atom">Formations en interface &amp; 
visualisation</title>, <title xmlns:media="http://search.yahoo.com/mrss/" 
xmlns:dc="http://purl.org/dc/elements/1.1/" 
xmlns:posterous="http://posterous.com/help/rss/1.0" 
xmlns:atom="http://www.w3.org/2005/Atom">Hello, world !</title>)




                                                                                  ffunction
                                                                                  inc.
Interpreter
scala> println(
  List("Hello","world !") match {
    case head :: tail =>
         tail.foldLeft(head)(
               (a:String,b:String)=>{a+", " +b})})

Hello, world !




                                                     ffunction
                                                     inc.
Scala – the sublime*


*SUBLIME : pleasure + pain, according to the Romantics




                                                         ffunction
                                                         inc.
The almost ML-grade type system

val f:List[Int] = List[1,2,3]

val m:Map[String,List[Int]] = Map(
    “a” → [1,2,3],
    “b” → [4,5,6]
)

type JSONable = { def toJSON():String }


                      > ensure constraints at compile-type, speed up programs




                                                                  ffunction
                                                                  inc.
Multiple method dispatch



def merge(                       def merge(

   a:Map[String,Object],             a:List[Object],
   b:Map[String,Object]              b:List[Object]

):Map[String,Object]             ):List[Object]


                           > makes it easy to specialize (existing) libraries




                                                                 ffunction
                                                                 inc.
Pattern-matching


val sum = {
   _ match {
      case head :: tail >
         head + sum (tail)
      case Nil >
         0
   }
}
          > amazing for message-based communication and list manipulation




                                                               ffunction
                                                               inc.
Actors
import scala.actors.Actor._

actor {
 while (true) {println ("Hello from A") ; Thread.sleep(1)}
}
actor {
 while (true) {println ("Hello from B") ; Thread.sleep(1)}
}
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from B   Hello from B   Hello from B   Hello from B   Hello from B   Hello from B
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from B   Hello from B   Hello from B   Hello from B   Hello from B   Hello from B
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from B   Hello from B   Hello from B   Hello from B   Hello from B   Hello from B
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from B   Hello from B   Hello from B   Hello from B   Hello from B   Hello from B
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from B   Hello from B   Hello from B   Hello from B   Hello from B   Hello from B
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A
Hello from B   Hello from B   Hello from B   Hello from B   Hello from B   Hello from B
Hello from A   Hello from A   Hello from A   Hello from A   Hello from A   Hello from A


                                                                                          ffunction
                                                                                          inc.
Message-passing
import scala.actors.Actor._
val repeater = actor {
    while (true) {
       receive {
          case msg:String =>
             println(this + ">" + msg) ; reply (msg)
}}}
actor {
    repeater ! "ping"
    while (true) { receive {
       case msg:String =>
             println(this + ">" + msg) ; reply (msg)
}}}




                                                       ffunction
                                                       inc.
Le mot de la fin




                   ffunction
                   inc.
Scala is cool !



●
    Very powerful
●
    Hard to learn, long time to master
●
    Amazing complement to Python for research &
    performance-critical and scalable projects
●
    Easy to interface with Python !



                                          ffunction
                                          inc.
Merci !

  www.ffctn.com
sebastien@ffctn.com


                      ffunction
                      inc.

From Python to Scala

  • 1.
    From Python to Scala Sébastien Pierre, ffunction inc. @Montréal Python, April 2010 www.ffctn.com ffunction inc.
  • 2.
    Stuff I loveabout Python ● Expressive (and compact) ● Simple to learn ● Lots of smartly designed libraries ffunction inc.
  • 3.
    Stuff that Imiss in Python ● Closures (with syntax) ● Easy concurrency (model and syntax) ● … a compiler !! ffunction inc.
  • 4.
    Scala - overview ffunction inc.
  • 5.
    The language ● JVM-based, compiled + « interpreted » ● Functional + OO (post-functional) ● ~6 years old ● used by Twitter, FourSquare ffunction inc.
  • 6.
    The library ● Anything in Java ● Immutable types library (safe for concurrent apps) ● Concurrency + actor model ● Quality community-contributed libraries ffunction inc.
  • 7.
    The feel ● Make do with braces ● map/filter/apply (and fold) ● ~25% more lines than with Python ● Very good DSL-ability ● Fast ! ffunction inc.
  • 8.
    Scala – basics ffunction inc.
  • 9.
    Rich types Scala Python Array Array(1,2,3,4) (1,2,3,4] Tuple (1,2,3,4) (1,2,3,4) List List(1,2,3,4) (1,2,3,4) Map Map(“a”>1, “b”>2) {“a”:1, “c”:2} Set Set(1,2,3,4) set(1,2,3,4) ffunction inc.
  • 10.
    Closures [1,2,3] map { i => i + 1 } {i:Int => i + 1 } # 1-param normal (i:Int) => { i + 1 } # n-param normal [1,2,3] map { _ + 1 } # implicit args val f = { i:Int => i + 1} ffunction inc.
  • 11.
    Classes + traits classA {…} ffunction inc.
  • 12.
    Classes + traits class A {…} class B extends A {…} ffunction inc.
  • 13.
    Classes + traits class A {…} class B extends A trait T with T {…} {…} ffunction inc.
  • 14.
    Classes + traits class A trait U {…} {…} class B trait T extends A extends U with T {…} {…} ffunction inc.
  • 15.
    Classes + traits class A trait U {…} {…} class B trait T extends A extends U with T {…} {…} ffunction inc.
  • 16.
    Batteries included scala.xml.XML.load( "http://ffunction.posterous.com/rss.xml" ) "item" "title" toList List[scala.xml.Node] = List(<title xmlns:media="http://search.yahoo.com/mrss/"  xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:posterous="http://posterous.com/help/rss/1.0"  xmlns:atom="http://www.w3.org/2005/Atom">Notes on creating a multi­touch  interface</title>, <title xmlns:media="http://search.yahoo.com/mrss/"  xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:posterous="http://posterous.com/help/rss/1.0"  xmlns:atom="http://www.w3.org/2005/Atom">Formations en interface &amp;  visualisation</title>, <title xmlns:media="http://search.yahoo.com/mrss/"  xmlns:dc="http://purl.org/dc/elements/1.1/"  xmlns:posterous="http://posterous.com/help/rss/1.0"  xmlns:atom="http://www.w3.org/2005/Atom">Hello, world !</title>) ffunction inc.
  • 17.
    Interpreter scala> println( List("Hello","world !") match { case head :: tail => tail.foldLeft(head)( (a:String,b:String)=>{a+", " +b})}) Hello, world ! ffunction inc.
  • 18.
    Scala – thesublime* *SUBLIME : pleasure + pain, according to the Romantics ffunction inc.
  • 19.
    The almost ML-gradetype system val f:List[Int] = List[1,2,3] val m:Map[String,List[Int]] = Map( “a” → [1,2,3], “b” → [4,5,6] ) type JSONable = { def toJSON():String } > ensure constraints at compile-type, speed up programs ffunction inc.
  • 20.
    Multiple method dispatch defmerge( def merge( a:Map[String,Object], a:List[Object], b:Map[String,Object] b:List[Object] ):Map[String,Object] ):List[Object] > makes it easy to specialize (existing) libraries ffunction inc.
  • 21.
    Pattern-matching val sum ={ _ match { case head :: tail > head + sum (tail) case Nil > 0 } } > amazing for message-based communication and list manipulation ffunction inc.
  • 22.
    Actors import scala.actors.Actor._ actor { while (true) {println ("Hello from A") ; Thread.sleep(1)} } actor { while (true) {println ("Hello from B") ; Thread.sleep(1)} } Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from B Hello from B Hello from B Hello from B Hello from B Hello from B Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from B Hello from B Hello from B Hello from B Hello from B Hello from B Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from B Hello from B Hello from B Hello from B Hello from B Hello from B Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from B Hello from B Hello from B Hello from B Hello from B Hello from B Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from B Hello from B Hello from B Hello from B Hello from B Hello from B Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A Hello from B Hello from B Hello from B Hello from B Hello from B Hello from B Hello from A Hello from A Hello from A Hello from A Hello from A Hello from A ffunction inc.
  • 23.
    Message-passing import scala.actors.Actor._ val repeater= actor { while (true) { receive { case msg:String => println(this + ">" + msg) ; reply (msg) }}} actor { repeater ! "ping" while (true) { receive { case msg:String => println(this + ">" + msg) ; reply (msg) }}} ffunction inc.
  • 24.
    Le mot dela fin ffunction inc.
  • 25.
    Scala is cool! ● Very powerful ● Hard to learn, long time to master ● Amazing complement to Python for research & performance-critical and scalable projects ● Easy to interface with Python ! ffunction inc.
  • 26.
    Merci ! www.ffctn.com sebastien@ffctn.com ffunction inc.