25
Mario Fusco 2010 X6 The feel of Scala

soft-shake.ch - The feel of Scala

Embed Size (px)

DESCRIPTION

Mario FuscoThe presentation is an introduction to the Scala language. It will show the most important characteristics and winning points of the language with special attention to some specific features like: the ability to painlessly mix functional and object oriented programming, its powerful collections framework, its type inference mechanism, the clear distinction between mutable and immutable data and how its actor model could make the concurrent programming more effective and less error-prone.

Citation preview

Page 1: soft-shake.ch - The feel of Scala

Mario Fusco

2010

X6

The feel of Scala

Page 2: soft-shake.ch - The feel of Scala

Agilité iPhone Java Incubateur

8:15 Accueil des participantsAccueil des participantsAccueil des participantsAccueil des participants

8:40 Mot des organisateurs & Criée des orateursMot des organisateurs & Criée des orateursMot des organisateurs & Criée des orateursMot des organisateurs & Criée des orateurs

9:00 Keynote de Nicolas Martignole (30 minutes)Keynote de Nicolas Martignole (30 minutes)Keynote de Nicolas Martignole (30 minutes)Keynote de Nicolas Martignole (30 minutes)

9:40

10:40

- A1 -Le terrain Agile

Jean-Philippe Vigniel

- I1-Hello iPhone

Stephane Tavera

- J1 -NOSQL also means RDF stores: an

Android case studyFabrizio Giudci

- X1 -Le développement durable

Dominic Williams

11:00

12:00

- A2 -Integration of User Centered Design

in Agile Development of RIAJ. Borkenhagen, J. Desmazières

- I2 -Développement d'une application

iPhone pilotée par les testsEmmanuel Etasse, Van-Charles Tran

- J2 -La Tequila du développement Web

Nicolas Martignole

- X2 -Cloud Computing: anatomie et

pratiqueMarc-Elian Bégin

12:20

13:20

- A3 -Adoption de l'Agilité par les usages

Xavier Warzee

- I3 -Distribution d'applications iPhone

en Entreprise: Réalisation d'un AppStore interne

Géraud de Laval

- J3 -Vaadin - Rich Web Applications in

Server-side Java without Plug-ins or JavaScript

Joonas Lehtinen

- X3 -Les DVCS sont vos amis

Sébastien Douche

Pause repas (50 minutes)Pause repas (50 minutes)Pause repas (50 minutes)Pause repas (50 minutes)

14h10 Keynote de Regis Medina (30 minutes)Keynote de Regis Medina (30 minutes)Keynote de Regis Medina (30 minutes)Keynote de Regis Medina (30 minutes)

14h50

15h50

- A4 - Scrum, introduction et mise en

oeuvre avec iceScrumClaude Aubry

- I4 -Agile iOS Development

Jérôme Layat, Alexander Osterwalder

- J4 -JAX-RS and Java EE 6

Paul Sandoz

- X4 -IT Design & Ergonomy

Pascal Petit, Aude Lussigny

16h10

17h10

- A5 -Agilité : 10 ans déjà

Thierry Cros

- I5 -Optimizing iOS applications

Marc-Antoine Scheurer

- J5 -Ecrivez et automatisez vos tests

fonctionnels avec jBehaveXavier Bourguignon

- X5 -NoSQL : Enfin de la biodiversité

dans l'écosystème des BDOlivier Mallassi

17h30

18h30

- A6 -Lean engineering

Jean-Christophe Dubail

- I6 -iPhone et Agile, l'amour vache

Guillaume Duquesnay

- J6 -Let's make this test suite run faster

David Gageot

- X6 -The feel of Scala

Mario Fusco

Mot de la fin & tombolaMot de la fin & tombolaMot de la fin & tombolaMot de la fin & tombola

Programme de la Conférence

www.soft-shake.ch

Page 3: soft-shake.ch - The feel of Scala

The Feel of

Mario Fusco

[email protected]

Twitter: @mariofusco

by

Page 4: soft-shake.ch - The feel of Scala

Mario Fusco

[email protected]

Twitter: @mariofusco

lambdaj.googlecode.com

Page 5: soft-shake.ch - The feel of Scala

Do we need a new language?

Keep It Simple

Vs.

Do More With Less

Page 6: soft-shake.ch - The feel of Scala

Why Scala?

object-oriented

functional

Java compatible

concise

extensible

statically typed

concurrent

scriptable

Page 7: soft-shake.ch - The feel of Scala

Functions and Closures

val isPositive = (x: Int) => x > 0 val numbers = List(-10, 5, 3, -2, 0, 1) val positiveNumber = numbers.filter(isPostive) val positiveNumber = numbers.filter(x => x > 0) val positiveNumber = numbers.filter(_ > 0)

Page 8: soft-shake.ch - The feel of Scala

Scala Collections

val animals = List(“dog”, “cat”, “horse”, “rabbit”) val romanNumbers = Map(1 -> “I”, 2 -> “II”, 3 -> “III”) animals.foreach(s => println(s)) animals.foreach(println _) animals.foreach(println) animals.map(s => s + “s”) animals.mkString(“, “) animals.count(s => s.length > 3) animals.remove(s => s.length > 3) animals.sort((s,t) => s.charAt(1) < t.charAt(1))

Page 9: soft-shake.ch - The feel of Scala

Tuples

val pair = (2, “items”) println(pair._1) // prints 2 println(pair._2) // prints items

Page 10: soft-shake.ch - The feel of Scala

Clear distinction between

mutable and immutable data

val msg = “Hello,world!” // constant var value = 3 // variable

scala.collection

scala.collection.immutable scala.collection.mutable

Page 11: soft-shake.ch - The feel of Scala

Named and default parameters

class Person(name: String = "Goofy", age: Int = 30, location: String = "Milano") Person(name = "Mario", age = 36, location = "Lugano") Person(age = 36, location = "Lugano", name = "Mario") Person(age = 36, name = "Mario") Person(age = 36) Person()

Page 12: soft-shake.ch - The feel of Scala

Operator overloading

class Rational (n: Int, d: Int) { def this(n: Int) = this(n, 1) def + (that: Rational): Rational = new Rational(n * that.d + that.n * d, d * that.d) def + (i: Int): Rational = new Rational(n + i * d, d) }

Page 13: soft-shake.ch - The feel of Scala

Implicit conversion

val a = new Rational(2, 3) val b = a + 2 // = 8/3 val c = 2 + a // Compilation Error

implicit def intToRational(x: Int) = new Rational(x) Val c = 2 + a // = 8/3

Page 14: soft-shake.ch - The feel of Scala

Options

Tony Hoare, who invented the null reference in 1965 while

working on an object oriented language called ALGOL W, called

its invention his “billion dollar mistake”

val capitals = Map("Italy" -> "Rome", "Switzerland" -> "Bern", "Germany" -> "Berlin" , "France" -> "Paris")

println(capitals.get("Italy")) // Some(Rome) println(capitals.get("Spain")) // None

println(capitals.get("Italy").get) // Rome println(capitals.get("Spain").get) // thorws Exception println(capitals.get("Spain").getOrElse("Unknown")) // Unknown

Page 15: soft-shake.ch - The feel of Scala

Traits

class Animal { def eat(): Unit } trait Mammal extends Animal { def giveBirth(): Mammal } trait HasWings extends Animal { def fly(): Unit } trait HasLegs extends Animal { def walk(): Unit } class Snake extends Animal class Frog extends Animal with HasLegs class Cat extends Animal with Mammal with HasLegs class Bat extends Animal with Mammal with HasWings class Chimera extends Animal with Mammal with HasWings with HasLegs

Page 16: soft-shake.ch - The feel of Scala

Case classes

trait Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class Unop(op: String, arg: Expr) extends Expr case class Binop(op: String, l: Expr, r: Expr) extends Expr

Page 17: soft-shake.ch - The feel of Scala

Pattern matching

def simplify(expr: Expr): Expr = expr match { case Unop("-", Unop("-", e)) => e // Double negation case Binop("+", e, Number(0)) => e // Adding zero case Binop("*", e, Number(1)) => e // Multiplying by one case _ => expr }

// Simplify double negation: simplified = Var("x") val simplified = simplify(Unop("-", Unop("-", Var("x"))))

Page 18: soft-shake.ch - The feel of Scala

Structural Typing (duck typing done right)

class Duck { quack() { println "quack" } } doQuack(new Duck)

doQuack(d) { d.quack() }

class Dog { barf() { println "barf" } } doQuack(new Dog)

def doQuack(d:{ def quack():Unit }) = d.quack()

class Duck { def quack() = println "quack" } doQuack(new Duck)

class Dog { def barf() = println "barf" } doQuack(new Dog) runtime error

compilation

error

Duck typing is the dynamic mechanism that allows to discover a dog cannot

say quack only at runtime ... in production ... on friday evening

Page 19: soft-shake.ch - The feel of Scala

Actors

val printerActor = actor { loop { receive { case s: String => println("I got a String: " + s) case i: Int => println("I got an Int: " + i.toString) case _ => println(" I don’t know what I got ") } } }

printerActor ! "hi there“ // prints “I got a String: hi there” printerActor ! 23 // prints “I got an Int: 23” printerActor ! 3.33 // prints “I don’t know what I got”

Page 20: soft-shake.ch - The feel of Scala

The ecosystem

Page 21: soft-shake.ch - The feel of Scala

Who is using Scala ?

Page 22: soft-shake.ch - The feel of Scala

Norbert by LinkedIn

• Norbert is a framework written in Scala that

allows to write asynchronous, message based,

client/server applications

• Built on Apache ZooKeeper and JBoss Netty,

Norbert to make it easy to build a cluster aware

application

• Provides out of the box support for notifications

of cluster topology changes, application specific

routing, load balancing and partitioned workload

Page 23: soft-shake.ch - The feel of Scala

• Akka is a framework that allows to write simpler

concurrent (yet correct) applications

• It provides a higher level of abstraction for

writing concurrent and distributed systems

through (remote) actors

• It implements Software Transactional Memory

(STM) turning the Java heap into a transactional

data set with begin/commit/rollback semantic

• Fault-tolerant adopting the "Let it crash" /

"Embrace failure" model

Page 24: soft-shake.ch - The feel of Scala

SBT (simple-build-tool)

• Sbt is a build tool written in Scala

• Uses the same directory structure as

Maven for source files

• Uses Ivy to resolve dependencies

• Compatible with Maven configuration files

• Supports testing with ScalaTest

• Parallel task execution, including parallel

test execution

Page 25: soft-shake.ch - The feel of Scala

Happy Scala programming!

Thanks a lot

Mario Fusco

[email protected]

Twitter: @mariofusco