Programmation fonctionnelle Scala
-
Upload
slim-ouertani -
Category
Education
-
view
2.012 -
download
1
description
Transcript of Programmation fonctionnelle Scala
![Page 1: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/1.jpg)
PROGRAMMATION FONCTIONNELLESCALA
Slim Ouertani
![Page 2: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/2.jpg)
2
SLIM OUERTANI
site: ouertani.com
blog: Jtunisie jroller.com/ouertani
mail: [email protected]
twitter: ouertani
![Page 3: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/3.jpg)
3
AGENDA
I - Motivation
II - Introduction à la PF
III - Introduction à Scala
![Page 4: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/4.jpg)
4
![Page 5: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/5.jpg)
5
MOTIVATION
Loi de Moore
Le nombre de transistors va doubler toute les deux années
Transistors Performance CPU
![Page 6: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/6.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/7.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/8.jpg)
8
Partager les états et la
mutabilité des variables
![Page 9: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/9.jpg)
9
L’orienté objet n’est pas dédiée pour faire de l’objet
![Page 10: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/10.jpg)
10
La source principale de ses problèmes est
programmation impérative
![Page 11: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/11.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/12.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/13.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/14.jpg)
14
Les langues qui encouragent
l'utilisation des fonctions pures sont
appelés langages fonctionnels
![Page 15: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/15.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/16.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/17.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/18.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/19.jpg)
19
ActeursSTM
![Page 20: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/20.jpg)
20
SCALA
![Page 21: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/21.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/22.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/23.jpg)
23
![Page 24: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/24.jpg)
24
SCALA
● OO + FP● Java compatible
● Compiles to Java bytecode● Existing libraries/frameworks
● Simple● Objet Pure● Statiquement typé
![Page 25: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/25.jpg)
25
;
![Page 26: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/26.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/27.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/28.jpg)
28
VARIABLES
var i: Int = 42
![Page 29: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/29.jpg)
29
VARIABLES
var i = 42
i = 3
i = "Hello world!" // erreur de compilation
![Page 30: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/30.jpg)
30
VALEURS
val i = 42
i = 3 // erreur de compilation
![Page 31: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/31.jpg)
31
MÉTHODES
def sum(a: Int, b: Int): Int = { return a + b}
![Page 32: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/32.jpg)
32
MÉTHODES
def sum(a: Int, b: Int): Int = { a + b}
![Page 33: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/33.jpg)
33
MÉTHODES
def sum(a: Int, b: Int) = { a + b}
![Page 34: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/34.jpg)
34
MÉTHODES
def sum(a: Int, b: Int) = a + b
![Page 35: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/35.jpg)
35
METHODS
def sayHello(name: String) { println("Hello, " + name)}
![Page 36: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/36.jpg)
36
METHODS
"apple".charAt(0)
"apple" charAt 0
1 .+(2)
1 + 2
![Page 37: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/37.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/38.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/39.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/40.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/41.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/42.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/43.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/44.jpg)
44
CLASSES AND CONSTRUCTORS
class Person(val age: Int)
![Page 45: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/45.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/46.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/47.jpg)
47
myObject match { case 1 => println("First") case 2 => println("Second") case _ => println("Unknown")}
PATTERN MATCHING
![Page 48: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/48.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/49.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/50.jpg)
50
PATTERN MATCHING (2)
![Page 51: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/51.jpg)
51
PATTERN MATCHING (2 )
![Page 52: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/52.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/53.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/54.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/55.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/56.jpg)
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](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/57.jpg)
57
TRY-WITHRESOURCES
![Page 58: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/58.jpg)
58
![Page 59: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/59.jpg)
59
![Page 60: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/60.jpg)
60
![Page 61: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/61.jpg)
61
![Page 62: Programmation fonctionnelle Scala](https://reader036.fdocuments.in/reader036/viewer/2022062313/558528bcd8b42a86388b45a4/html5/thumbnails/62.jpg)
62
Q & A