Upgrade to Pro — share decks privately, control downloads, hide ads and more …

La programmation fonctionnelle

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

La programmation fonctionnelle

Avatar for Sandrine Banas

Sandrine Banas

December 09, 2020
Tweet

More Decks by Sandrine Banas

Other Decks in Technology

Transcript

  1. © 2020 CGI Inc. Interne Interne Intérêt ? • Adaptée

    aux problématiques de l’époque: concurrence, performance, gros systèmes multi- états, Big data... • Plusieurs façons de penser valent mieux qu’une. • Code plus court et concis, facile à comprendre et tester avec moins de bugs et souvent fun. • Effet ouaouh sur les autres 3
  2. © 2020 CGI Inc. Interne La programmation fonctionnelle Premier langage

    fonctionnel LISP 1958 Java 8 – 2014 Aws Lambda - 2014 React - 2017 4
  3. © 2020 CGI Inc. Interne Interne • Programmation impérative (comment)

    • Opérations en séquences d’instructions modifiant l’état du programme • Encapsulation • Programmation déclarative (quoi) • Composant logiciels (fonctions) indépendants du contexte et ne comportant aucun état interne. • Transparence référentielle 7 Paradigmes de programmation
  4. © 2020 CGI Inc. Interne Interne Syntaxe Javascript function myFunction(input1,

    input2) { return input1 + input2; } (input1, input2) => { //instructions return input1 + input2;} (input1, input2) => input1 + input2 9
  5. © 2020 CGI Inc. Interne Interne Syntaxe Java BiFunction<Integer, Integer,

    Integer> myFunction = new BiFunction<Integer, Integer, Integer>() { @Override public Integer apply(Integer input1, Integer input2) { return input1 + input2; }}; (input1, input2) -> { //instructions return input1 + input2;} (input1, input2) -> input1 + input2 10
  6. © 2020 CGI Inc. Interne Interne • Associée à un

    objet ou une classe et peut accéder à son état interne et le modifier • Programmation orientée objet • monPanier.ajouter(montant) • Mathématique => f(x) = 2x • Indépendante, n’accède qu’aux paramètres en entrée • Programmation fonctionnelle • (montant1, montant2) -> montant1 + montant2 11 Une méthode n’est pas une fonction (et vice versa).
  7. © 2020 CGI Inc. Interne Interne Fonction PURE • Transparence

    référentielle = Pour un même input, l’output ne changera pas. • Pas d’effets de bord • Single Responsability Principle • Ex Math.sqrt(value) Fonction IMPURE • Fonctions void, sans input et/ou output • Effets de bord • Plusieurs responsabilités Ex Math.random(); 17
  8. © 2020 CGI Inc. Interne Interne Citoyens de première classe

    Assignable à une variable Retournée par une méthode Passée en paramètre 19
  9. © 2020 CGI Inc. Interne Interne Les fonctions d’ordre supérieures

    Prend une ou plusieurs fonctions comme input et/ou retourne une fonction comme output 20
  10. © 2020 CGI Inc. Interne Interne Curryfication • Haskell Curry

    • Transformer une fonction en plusieurs arguments en une fonction à un seul argument qui retourne une fonction sur le reste des argument. • Simplifier, augmenter le niveau d’abstraction, créer des fonctions pures • Application partielle des paramètres 22
  11. © 2020 CGI Inc. Interne Interne Gestion des erreurs •

    Exceptions non prévisibles exemple erreur mémoire => effet de bord • Exceptions prévisibles ex parse Integer • L’erreur doit être retournée et non envoyée. 23
  12. © 2020 CGI Inc. Interne Interne Gestion des erreurs Try

    containeur d’une computation qui peut se terminer en exception. Peut se terminer en failure ou success Either => Left pour l’erreur, Right pour le succès 24
  13. © 2020 CGI Inc. Interne Interne INCONVENIENTS Problème de lisibilité

    si mauvaise utilisation Pas adaptée à tous les cas 26
  14. © 2020 CGI Inc. Interne © 2020 CGI Inc. Communiquez

    avec nous pour poursuivre la conversation ! Banas Expert technique senior [email protected] https://www.linkedin.com/in/sandrine-banas-60498aa8/ @cosjava