Programmation fonctionnelle Scala
Upcoming SlideShare
Loading in...5
×
 

Programmation fonctionnelle Scala

on

  • 2,100 views

programmation fonctionnelle + introduction simple à scala

programmation fonctionnelle + introduction simple à scala

Statistics

Views

Total Views
2,100
Views on SlideShare
1,626
Embed Views
474

Actions

Likes
1
Downloads
16
Comments
0

9 Embeds 474

http://ouertani.com 174
http://www.jroller.com 151
http://www.mybestcv2.co.il 42
http://www.foundation-tn.org 39
http://jroller.com 34
http://www.linkedin.com 24
https://www.linkedin.com 7
http://hghltd.yandex.net 2
http://webcache.googleusercontent.com 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Programmation fonctionnelle Scala Programmation fonctionnelle Scala Presentation Transcript

  • PROGRAMMATION FONCTIONNELLESCALA Slim Ouertani
  • site: ouertani.com blog: Jtunisie jroller.com/ouertani mail: ouertani@gmail.comtwitter: ouertani 2
  • AGENDAI - MotivationII - Introduction à la PFIII - Introduction à Scala 3 View slide
  • 4 View slide
  • MOTIVATION Loi de Moore  Le nombre de transistors va doubler toute les deux années  Transistors  Performance CPU 5
  • MOTIVATIONLe futur et le présent du hardware est dans le parallélisme  Resteque quelques routines en séquentielles 6
  • MAIS ! Diviser le travail est déjà une tâche de conception dure  Linterblocage  La famine  Etat invalide 7
  • Partager les états et la mutabilité des variables 8
  • L’orienté objet n’est pas dédiée pour faire de l’objet 9
  • La source principalede ses problèmes est programmation impérative 10
  • 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 Laprogrammation 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é 11
  • 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 12
  • 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 13 fonctions pures
  • Les langues qui encouragent lutilisation desfonctions pures sont appelés langages fonctionnels 14
  •  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 15
  • 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 16
  • 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 17
  • x[t]= x0x[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 18
  • ActeursSTM 19
  • SCALA 20
  • If I were to pick a language to use on the JVM today other than Java, it would be Scala. – James Gosling, creator of Java http://www.adam- bien.com/roller/abien/entry/java_net_javaone_which_programming My tip though for the long term replacement of javac is Scala. Im very impressed with it! I can honestly say if someone had shown me the Programming in Scala book […] back in 2003 Id probably have never created Groovy. – James Strachan, creator of Groovy http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.htmlScala, 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. 21 – Charlies Nutter, JRuby lead http://blog.headius.com/2009/04/future-part-one.html
  • “If Java programmers want to use features that arent present in the language, I think theyreprobably best off using another language that targets the JVM, such as Scala and Groovy“ Joshua Bloch 22
  • 23
  • SCALA● OO + FP● Java compatible ● Compiles to Java bytecode ● Existing libraries/frameworks● Simple● Objet Pure● Statiquement typé 24
  • 25
  • 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; }} 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
  • VARIABLES var i: Int = 42 28
  • VARIABLES var i = 42 i = 3 i = "Hello world!" // erreur de compilation 29
  • VALEURS val i = 42 i = 3 // erreur de compilation 30
  • MÉTHODES def sum(a: Int, b: Int): Int = { return a + b } 31
  • MÉTHODES def sum(a: Int, b: Int): Int = { a + b } 32
  • MÉTHODES def sum(a: Int, b: Int) = { a + b } 33
  • MÉTHODES def sum(a: Int, b: Int) = a + b 34
  • METHODS def sayHello(name: String) { println("Hello, " + name) } 35
  • METHODS "apple".charAt(0) 1 .+(2) "apple" charAt 0 1 + 2 36
  • 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 37
  • FONCTIONSGoogle collections: Predicate<Integer> even = new Predicate<Integer>() { @Override public boolean apply(Integer i) { return i % 2 == 0; } }; List<Integer> numbers = … // 1, 2, 3, 4 Iterable<Integer> evenNums = Iterables.filter(numbers, even); => [2, 4] 38
  • FONCTIONSScala collections: val even = (i: Int) => i % 2 == 0 val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(even) => List(2, 4) 39
  • FONCTIONSScala collections: val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter((i: Int) => i % 2 == 0) => List(2, 4) 40
  • FONCTIONSScala collections: val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(i => i % 2 == 0) => List(2, 4) 41
  • FONCTIONSScala collections: val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(_ % 2 == 0) => List(2, 4) 42
  • FONCTIONSScala collections: Predicate<Integer> even = new Predicate<Integer>() { @Override public boolean apply(Integer i) { return i % 2 == 0; } }; val numbers = List(1, 2, 3, 4) val evenNums = numbers.filter(_ % 2 == 0) => List(2, 4) 43
  • CLASSES AND CONSTRUCTORS class Person(val age: Int) 44
  • CLASSES AND CONSTRUCTORS class Person(val age: Int) { def this() = this(42) var name: String = _ override def toString = "My name is " + name } 45
  • 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) 46
  • PATTERN MATCHING myObject match { case 1 => println("First") case 2 => println("Second") case _ => println("Unknown") } 47
  • PATTERN MATCHING myObject match { case i: Int => println("Found number " + i) case s: String => println("Found text " + s) case _ => println("Unknown") } 48
  • PATTERN MATCHING val email = """(.+)@(.+)""".r "scala@java.no" match { case email(name, domain) => println("User " + name + " at " + domain) case x => println(x + " is not an email") } 49
  • PATTERN MATCHING (2) 50
  • PATTERN MATCHING (2 ) 51
  • OPTION def 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") 52 } // => prints "5"
  • 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] 53
  • 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) 54
  • 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) 55
  • AUTOMATIC TYPE-DEPENDENT CLOSURECONSTRUCTION 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 } } 56
  • TRY-WITHRESOURCES 57
  • 58
  • 59
  • 60
  • 61
  • Q&A 62