programmation fonctionnelle scala
DESCRIPTION
programmation fonctionnelle + introduction simple à scalaTRANSCRIPT
PROGRAMMATION FONCTIONNELLESCALA
Slim Ouertani
2
SLIM OUERTANI
site: ouertani.com
blog: Jtunisie jroller.com/ouertani
mail: [email protected]
twitter: ouertani
3
AGENDA
I - Motivation
II - Introduction à la PF
III - Introduction à Scala
4
5
MOTIVATION
Loi de Moore
Le nombre de transistors va doubler toute les deux années
Transistors Performance CPU
6
MOTIVATION
Le futur et le présent du hardware est dans le parallélisme
Reste que quelques routines en séquentielles
7
MAIS !
Diviser le travail est déjà une tâche de conception dure
L'interblocage
La famine
Etat invalide
8
Partager les états et la
mutabilité des variables
9
L’orienté objet n’est pas dédiée pour faire de l’objet
10
La source principale de ses problèmes est
programmation impérative
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é
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
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
14
Les langues qui encouragent
l'utilisation des fonctions pures sont
appelés langages fonctionnels
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
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
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
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
19
ActeursSTM
20
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
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
23
24
SCALA
● OO + FP● Java compatible
● Compiles to Java bytecode● Existing libraries/frameworks
● Simple● Objet Pure● Statiquement typé
25
;
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; }}
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 }}
28
VARIABLES
var i: Int = 42
29
VARIABLES
var i = 42
i = 3
i = "Hello world!" // erreur de compilation
30
VALEURS
val i = 42
i = 3 // erreur de compilation
31
MÉTHODES
def sum(a: Int, b: Int): Int = { return a + b}
32
MÉTHODES
def sum(a: Int, b: Int): Int = { a + b}
33
MÉTHODES
def sum(a: Int, b: Int) = { a + b}
34
MÉTHODES
def sum(a: Int, b: Int) = a + b
35
METHODS
def sayHello(name: String) { println("Hello, " + name)}
36
METHODS
"apple".charAt(0)
"apple" charAt 0
1 .+(2)
1 + 2
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
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]
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)
40
FONCTIONS
Scala collections:
val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter((i: Int) => i % 2 == 0)
=> List(2, 4)
41
FONCTIONS
Scala collections:
val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter(i => i % 2 == 0)
=> List(2, 4)
42
FONCTIONS
Scala collections:
val numbers = List(1, 2, 3, 4)val evenNums = numbers.filter(_ % 2 == 0)
=> List(2, 4)
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)
44
CLASSES AND CONSTRUCTORS
class Person(val age: Int)
45
CLASSES AND CONSTRUCTORS
class Person(val age: Int) {
def this() = this(42)
var name: String = _
override def toString = "My name is " + name}
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)
47
myObject match { case 1 => println("First") case 2 => println("Second") case _ => println("Unknown")}
PATTERN MATCHING
48
myObject match { case i: Int => println("Found number " + i) case s: String => println("Found text " + s) case _ => println("Unknown")}
PATTERN MATCHING
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
50
PATTERN MATCHING (2)
51
PATTERN MATCHING (2 )
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"
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]
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)
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)
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 }}
57
TRY-WITHRESOURCES
58
59
60
61
62
Q & A