programmation fonctionnelle scala

62
PROGRAMMATION FONCTIONNELLE SCALA Slim Ouertani

Upload: slim-ouertani

Post on 20-Jun-2015

2.020 views

Category:

Education


1 download

DESCRIPTION

programmation fonctionnelle + introduction simple à scala

TRANSCRIPT

Page 1: Programmation fonctionnelle Scala

PROGRAMMATION FONCTIONNELLESCALA

Slim Ouertani

Page 2: Programmation fonctionnelle Scala

2

SLIM OUERTANI

site: ouertani.com

blog: Jtunisie jroller.com/ouertani

mail: [email protected]

twitter: ouertani

Page 3: Programmation fonctionnelle Scala

3

AGENDA

I - Motivation

II - Introduction à la PF

III - Introduction à Scala

Page 4: Programmation fonctionnelle Scala

4

Page 5: Programmation fonctionnelle Scala

5

MOTIVATION

Loi de Moore

Le nombre de transistors va doubler toute les deux années

Transistors Performance CPU

Page 6: Programmation fonctionnelle Scala

6

MOTIVATION

Le futur et le présent du hardware est dans le parallélisme

Reste que quelques routines en séquentielles

Page 7: Programmation fonctionnelle Scala

7

MAIS !

Diviser le travail est déjà une tâche de conception dure

L'interblocage

La famine

Etat invalide

Page 8: Programmation fonctionnelle Scala

8

Partager les états et la

mutabilité des variables

Page 9: Programmation fonctionnelle Scala

9

L’orienté objet n’est pas dédiée pour faire de l’objet

Page 10: Programmation fonctionnelle Scala

10

La source principale de ses problèmes est

programmation impérative

Page 11: Programmation fonctionnelle Scala

11

PI VS FP

Programmation impérative est un paradigme de programmation qui décrit le calcul en termes de déclarations qui modifient l'état des programmes

La programmation fonctionnelle est un paradigme de programmation qui décrit le calcul de l'évaluation des fonctions mathématiques en évitant les états et la mutabilité

Page 12: Programmation fonctionnelle Scala

12

FONCTION PURE

Composition de fonctions Pure

Avec les mêmes inputs on a toujours le même résultat

Boite noire : Pas de modifications Pas d’écriture de messages pour l’utilisateur Ni sur le disque

Pas d’effet de bord

Page 13: Programmation fonctionnelle Scala

13

EXEMPLES

f (x) = x2 + 1

Une fonction pure retourne des valeurs et ne fait rien d’autres

g (a; b; c) = (a + b) * (c / 2) + f (a )

h (t) = h (t -1) + h (t -2 )

Les fonctions pures se composent que des fonctions pures

Page 14: Programmation fonctionnelle Scala

14

Les langues qui encouragent

l'utilisation des fonctions pures sont

appelés langages fonctionnels

Page 15: Programmation fonctionnelle Scala

15

Les langages impératifs

Description du calcul en termes de changements de l'état

JAVA , C# , PHP

Séquencement d’opérations et action sur les variables en mémoire

Effet de bord

Page 16: Programmation fonctionnelle Scala

16

L’affectation est une source d’effet de bord

Les variables sont les causes des problèmes dans les environnements multi-thread

La mutabilité ainsi que le partage d’ état n’est plus la mode

Page 17: Programmation fonctionnelle Scala

17

Transparence référentielle

int n = 2; int inc(int k) { n = n + k; return

n; } incrémentation par effet de bord

inc(1) + inc(1) != 2 * inc (1 )

Avec les variables on oublie le temps

Page 18: Programmation fonctionnelle Scala

18

x[t] = x0x[t + 1] = x[t] + 1

A un instant donné, la valeur de x est immutable.

X est l’identité d’une séquence de valeurs dans le temps

Page 19: Programmation fonctionnelle Scala

19

ActeursSTM

Page 20: Programmation fonctionnelle Scala

20

SCALA

Page 21: Programmation fonctionnelle Scala

21

If I were to pick a language to use on the JVM today other than Java, it would be Scala.

– James Gosling, creator of Javahttp://www.adam-bien.com/roller/abien/entry/

java_net_javaone_which_programming

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.

– Charlies Nutter, JRuby leadhttp://blog.headius.com/2009/04/future-part-one.html

My tip though for the long term replacement of javac is Scala. I'm very impressed with it! I can honestly say if someone had shown me the Programming in Scala book […] back in 2003

I'd probably have never created Groovy.

– James Strachan, creator of Groovyhttp://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html

Page 22: Programmation fonctionnelle Scala

22

“If Java programmers want to use features that aren't present in the language, I think they're

probably best off using another language that targets the JVM, such as Scala and Groovy“

Joshua Bloch

Page 23: Programmation fonctionnelle Scala

23

Page 24: Programmation fonctionnelle Scala

24

SCALA

● OO + FP● Java compatible

● Compiles to Java bytecode● Existing libraries/frameworks

● Simple● Objet Pure● Statiquement typé

Page 25: Programmation fonctionnelle Scala

25

;

Page 26: Programmation fonctionnelle Scala

26

public class Person { private int age; private String name;

public Person(int age, String name) { this.age = age; this.name = name; }

public int getAge() { return this.age; }

public void setAge(int age) { this.age = age; }

public String getName() { return this.name; }

public void setName(String name) { this.name = name; }}

Page 27: Programmation fonctionnelle Scala

27

public class Person { private int age private String name

public Person(int age, String name) { this.age = age this.name = name }

public int getAge() { return this.age }

public void setAge(int age) { this.age = age }

public String getName() { return this.name }

public void setName(String name) { this.name = name }}

Page 28: Programmation fonctionnelle Scala

28

VARIABLES

var i: Int = 42

Page 29: Programmation fonctionnelle Scala

29

VARIABLES

var i = 42

i = 3

i = "Hello world!" // erreur de compilation

Page 30: Programmation fonctionnelle Scala

30

VALEURS

val i = 42

i = 3 // erreur de compilation

Page 31: Programmation fonctionnelle Scala

31

MÉTHODES

def sum(a: Int, b: Int): Int = { return a + b}

Page 32: Programmation fonctionnelle Scala

32

MÉTHODES

def sum(a: Int, b: Int): Int = { a + b}

Page 33: Programmation fonctionnelle Scala

33

MÉTHODES

def sum(a: Int, b: Int) = { a + b}

Page 34: Programmation fonctionnelle Scala

34

MÉTHODES

def sum(a: Int, b: Int) = a + b

Page 35: Programmation fonctionnelle Scala

35

METHODS

def sayHello(name: String) { println("Hello, " + name)}

Page 36: Programmation fonctionnelle Scala

36

METHODS

"apple".charAt(0)

"apple" charAt 0

1 .+(2)

1 + 2

Page 37: Programmation fonctionnelle Scala

37

COLLECTIONS

val list = List("apple", "orange", "banana")

val map = Map(1 -> "one", 2 -> "two")

val array = Array(1, 2, 3, 4, 5)

list(1) // orange

map(2) // two

array(3) // 4

Page 38: Programmation fonctionnelle Scala

38

FONCTIONS

Google collections:Predicate<Integer> even = new Predicate<Integer>() {

@Overridepublic boolean apply(Integer i) {

return i % 2 == 0;}

};

List<Integer> numbers = … // 1, 2, 3, 4Iterable<Integer> evenNums = Iterables.filter(numbers, even);

=> [2, 4]

Page 39: Programmation fonctionnelle Scala

39

FONCTIONS

Scala collections:

val even = (i: Int) => i % 2 == 0

val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter(even)

=> List(2, 4)

Page 40: Programmation fonctionnelle Scala

40

FONCTIONS

Scala collections:

val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter((i: Int) => i % 2 == 0)

=> List(2, 4)

Page 41: Programmation fonctionnelle Scala

41

FONCTIONS

Scala collections:

val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter(i => i % 2 == 0)

=> List(2, 4)

Page 42: Programmation fonctionnelle Scala

42

FONCTIONS

Scala collections:

val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter(_ % 2 == 0)

=> List(2, 4)

Page 43: Programmation fonctionnelle Scala

43

FONCTIONS

Scala collections:Predicate<Integer> even = new Predicate<Integer>() {

@Overridepublic boolean apply(Integer i) {

return i % 2 == 0;}

};

val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter(_ % 2 == 0)

=> List(2, 4)

Page 44: Programmation fonctionnelle Scala

44

CLASSES AND CONSTRUCTORS

class Person(val age: Int)

Page 45: Programmation fonctionnelle Scala

45

CLASSES AND CONSTRUCTORS

class Person(val age: Int) {

def this() = this(42)

var name: String = _

override def toString = "My name is " + name}

Page 46: Programmation fonctionnelle Scala

46

CASE CLASSES

case class Person(firstName: String, lastName: String)  

val me = Person("slim", "ouertani")val first = me.firstName val last = me.lastName  

if (me == Person(first, last)) { println("Found myself!") println(me) }

Found myself!Person(slim,ouertani)

Page 47: Programmation fonctionnelle Scala

47

myObject match { case 1 => println("First") case 2 => println("Second") case _ => println("Unknown")}

PATTERN MATCHING

Page 48: Programmation fonctionnelle Scala

48

myObject match { case i: Int => println("Found number " + i) case s: String => println("Found text " + s) case _ => println("Unknown")}

PATTERN MATCHING

Page 49: Programmation fonctionnelle Scala

49

val email = """(.+)@(.+)""".r

"[email protected]" match { case email(name, domain) => println("User " + name + " at " + domain)

case x => println(x + " is not an email")}

PATTERN MATCHING

Page 50: Programmation fonctionnelle Scala

50

PATTERN MATCHING (2)

Page 51: Programmation fonctionnelle Scala

51

PATTERN MATCHING (2 )

Page 52: Programmation fonctionnelle Scala

52

OPTIONdef div(a:Int)(b:Int):Option[Int] = if (b <= 0) None else if (a < b) Some(0) else Some(1 + div(a - b)(b).get)

div(25)(5) // => Some(5)div(13)(0) // => None div(25)(-5) // => None

div(13)(0) match { case Some(x) => println(x) case None => println("Problems") } // => prints "Problems"

div(25)(5) match { case Some(x) => println(x) case None => println("Problems") } // => prints "5"

Page 53: Programmation fonctionnelle Scala

53

HIGHER-ORDER FUNCTIONS

def apply(f: Int => String, v: Int) => f(v)

class Decorator(left: String, right: String) { def layout[A](x: A) = left + x.toString() + right}object FunTest extends Application { def apply(f: Int => String, v: Int) = f(v) val decorator = new Decorator("[", "]") Console.println(apply(decorator.layout, 7))}

[7]

Page 54: Programmation fonctionnelle Scala

54

NESTED FUNCTIONS

object FilterTest extends Application { def filter(xs: List[Int], threshold: Int) = { def process(ys: List[Int]): List[Int] = if (ys.isEmpty) ys else if (ys.head < threshold) ys.head :: process(ys.tail) else process(ys.tail) process(xs) }Console.println(filter(List(1, 9, 2, 8, 3, 7, 4), 5))}

List(1,2,3,4)

Page 55: Programmation fonctionnelle Scala

55

CURRYING

object CurryTest extends Application { def filter(xs: List[Int], p: Int => Boolean): List[Int] = if (xs.isEmpty) xs else if (p(xs.head)) xs.head :: filter(xs.tail, p) else filter(xs.tail, p)

def modN(n: Int)(x: Int) = ((x % n) == 0) val nums = List(1, 2, 3, 4, 5, 6, 7, 8) println(filter(nums, modN(2))) println(filter(nums, modN(3)))}

List(2,4,6,8)List(3,6)

Page 56: Programmation fonctionnelle Scala

56

AUTOMATIC TYPE-DEPENDENT CLOSURE CONSTRUCTION

object TargetTest1 extends Application { def whileLoop(cond: => Boolean)(body: => Unit): Unit = if (cond) { body whileLoop(cond)(body) } var i = 10

whileLoop (i > 0) { Console.println(i) i = i - 1 }}

Page 57: Programmation fonctionnelle Scala

57

TRY-WITHRESOURCES

Page 58: Programmation fonctionnelle Scala

58

Page 59: Programmation fonctionnelle Scala

59

Page 60: Programmation fonctionnelle Scala

60

Page 61: Programmation fonctionnelle Scala

61

Page 62: Programmation fonctionnelle Scala

62

Q & A