from python to scala
TRANSCRIPT
![Page 1: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/1.jpg)
ffunctioninc.
From Pythonto Scala
Sébastien Pierre, ffunction inc.@Montréal Python, April 2010
www.ffctn.com
![Page 2: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/2.jpg)
ffunctioninc.
Stuff I love about Python
● Expressive (and compact)● Simple to learn● Lots of smartly designed libraries
![Page 3: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/3.jpg)
ffunctioninc.
Stuff that I miss in Python
● Closures (with syntax)● Easy concurrency (model and syntax)● … a compiler !!
![Page 4: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/4.jpg)
ffunctioninc.
Scala - overview
![Page 5: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/5.jpg)
ffunctioninc.
The language
● JVM-based, compiled + « interpreted »● Functional + OO (post-functional)
● ~6 years old● used by Twitter, FourSquare
![Page 6: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/6.jpg)
ffunctioninc.
The library
● Anything in Java● Immutable types library (safe for concurrent apps)
● Concurrency + actor model● Quality community-contributed libraries
![Page 7: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/7.jpg)
ffunctioninc.
The feel
● Make do with braces● map/filter/apply (and fold)
● ~25% more lines than with Python● Very good DSL-ability● Fast !
![Page 8: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/8.jpg)
ffunctioninc.
Scala – basics
![Page 9: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/9.jpg)
ffunctioninc.
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)
![Page 10: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/10.jpg)
ffunctioninc.
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}
![Page 11: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/11.jpg)
ffunctioninc.
Classes + traits
class A{ … }
![Page 12: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/12.jpg)
ffunctioninc.
Classes + traits
class A{ … }
class Bextends A
{ … }
![Page 13: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/13.jpg)
ffunctioninc.
Classes + traits
class A{ … }
class Bextends A
with T{ … }
trait T{ … }
![Page 14: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/14.jpg)
ffunctioninc.
Classes + traits
class A{ … }
class Bextends A
with T{ … }
trait Textends U
{ … }
trait U{ … }
![Page 15: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/15.jpg)
ffunctioninc.
Classes + traits
class A{ … }
trait Textends U
{ … }
trait U{ … }
class Bextends A
with T{ … }
![Page 16: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/16.jpg)
ffunctioninc.
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 multitouch 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 & 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>)
![Page 17: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/17.jpg)
ffunctioninc.
Interpreter
scala> println( List("Hello","world !") match { case head :: tail => tail.foldLeft(head)( (a:String,b:String)=>{a+", " +b})})
Hello, world !
![Page 18: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/18.jpg)
ffunctioninc.
Scala – the sublime*
*SUBLIME : pleasure + pain, according to the Romantics
![Page 19: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/19.jpg)
ffunctioninc.
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
![Page 20: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/20.jpg)
ffunctioninc.
Multiple method dispatch
def merge(
a:List[Object],b:List[Object]
):List[Object]
def merge(
a:Map[String,Object],b:Map[String,Object]
):Map[String,Object]
> makes it easy to specialize (existing) libraries
![Page 21: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/21.jpg)
ffunctioninc.
Pattern-matching
val sum = {_ match {
case head :: tail >head + sum (tail)
case Nil >0
}}
> amazing for message-based communication and list manipulation
![Page 22: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/22.jpg)
ffunctioninc.
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 AHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from A
Hello from AHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from A
Hello from AHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from A
Hello from AHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from A
Hello from AHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from A
Hello from AHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from AHello from BHello from A
![Page 23: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/23.jpg)
ffunctioninc.
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)}}}
![Page 24: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/24.jpg)
ffunctioninc.
Le mot de la fin
![Page 25: From Python to Scala](https://reader036.vdocuments.net/reader036/viewer/2022062405/5559de16d8b42a39498b492c/html5/thumbnails/25.jpg)
ffunctioninc.
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 !