Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Le retour de la fonction, HumanTalks le 11/10/2016

289 views

Published on

"Less is more", c'est une citation bien connue et qui a une portée très universelle (design, startup, mode...). Et si finalement, les nombreuses restrictions de la programmation fonctionnelle nous permettaient de faire plus et mieux ? L'objectif de ce talk est de démystifier la programmation fonctionnelle et introduire les éléments basiques qui vous permettront d'améliorer grandement vos programmes, peut importe la techno !

Published in: Software
  • Be the first to comment

  • Be the first to like this

Le retour de la fonction, HumanTalks le 11/10/2016

  1. 1. Le retour de la fonction @loicknuchel
  2. 2. High-order function Currying Pure function Functor Monad Monoid Immutable Récursif Programmation générique
  3. 3. for loop is everywhere function toUpperCase(list){ var ret = []; for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } return ret; } var names = ['Finn', 'Rey', 'Poe']; console.log(toUpperCase(names)); // ['FINN', 'REY', 'POE']
  4. 4. for loop is everywhere function toUpperCase(list){ var ret = []; for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } return ret; } var names = ['Finn', 'Rey', 'Poe']; console.log(toUpperCase(names)); // ['FINN', 'REY', 'POE'] Boilerplate !!!
  5. 5. Array.prototype.map = function(callback){ var array = this; var result = []; for(var i=0; i<array.length; i++){ result[i] = callback(array[i]); } return result; }; for loop is everywhere (High-order function)
  6. 6. for loop is everywhere function toUpperCase(list){ return list.map(function(item){ return item.toUpperCase(); }); }
  7. 7. for loop is everywhere function toUpperCase(list){ return list.map(item => item.toUpperCase()); }
  8. 8. Séparation technique vs métier Array.prototype.map = function(callback){ var array = this; var result = []; for(var i=0; i<array.length; i++){ result[i] = callback(array[i]); } return result; }; list.map(item => item.toUpperCase()); Générique / Réutilisable Haut niveau d’abstraction Concis / Expressif Focalisé sur le domaine
  9. 9. Scala collection API (en partie) def map[B](f: (A) => B): List[B] def filter(p: (A) => Boolean): List[A] def partition(p: (A) => Boolean): (List[A], List[A]) def zip[B](that: List[B]): List[(A, B)] def sliding(size: Int): Iterator[List[A]] def find(p: (A) => Boolean): Option[A] def exists(p: (A) => Boolean): Boolean def flatten[B]: List[B] def flatMap[B](f: (A) => List[B]): List[B] def groupBy[K](f: (A) => K): Map[K, List[A]] def grouped(size: Int): Iterator[List[A]] def fold[A1 >: A](z: A1)(op: (A1, A1) => A1): A1 def reduce[A1 >: A](op: (A1, A1) => A1): A1 def forall(p: (A) => Boolean): Boolean def take(n: Int): List[A] def drop(n: Int): List[A] def distinct: List[A]
  10. 10. The billion dollar mistake ! function toUpperCase(list){ var ret = []; for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } return ret; } Safe ?
  11. 11. The billion dollar mistake ! function toUpperCase(list){ var ret = []; for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } return ret; } Unsafe ! Cannot read property 'xxx' of undefined !!!
  12. 12. The billion dollar mistake ! function toUpperCase(list){ var ret = []; for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } return ret; } function toUpperCase(list){ var ret = []; if(Array.isArray(list)) { for(var i=0; i<list.length; i++){ if(typeof list[i] === 'string'){ ret[i] = list[i].toUpperCase(); } else { ret[i] = list[i]; } } } return ret; } Unsafe ! Unreadable !
  13. 13. The billion dollar mistake ! function toUpperCase(list){ var ret = []; for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } return ret; } function toUpperCase(list){ var ret = []; if(Array.isArray(list)) { for(var i=0; i<list.length; i++){ ret[i] = list[i].toUpperCase(); } } return ret; } function toUpperCase(list){ var ret = []; if(Array.isArray(list)) { for(var i=0; i<list.length; i++){ if(typeof list[i] === 'string'){ ret[i] = list[i].toUpperCase(); } else { ret[i] = list[i]; } } } return ret; } Unsafe ! Unreadable ! (not so) smart
  14. 14. The billion dollar mistake ! // no null ! def toUpperCase(list: List[String]) = list.map(_.toUpperCase) def toUpperCase(list: List[Option[String]]) = list.map(_.map(_.toUpperCase))
  15. 15. List.map() vs Option.map() ???
  16. 16. List.map() vs Option.map() ??? Fonctors !!!
  17. 17. def toWords(sentences: List[String]): List[List[[String]] = sentences.map(_.split(" ").toList) def toWords(sentences: List[String]): List[String] = sentences.flatMap(_.split(" ").toList)
  18. 18. def toWords(sentences: List[String]): List[List[[String]] = sentences.map(_.split(" ").toList) def toWords(sentences: List[String]): List[String] = sentences.flatMap(_.split(" ").toList) Applicative !
  19. 19. def toWords(sentences: List[String]): List[List[[String]] = sentences.map(_.split(" ").toList) def toWords(sentences: List[String]): List[String] = sentences.flatMap(_.split(" ").toList) Applicative ! Fonctor + Applicative = Monad
  20. 20. def toWords(sentences: List[String]): List[List[[String]] = sentences.map(_.split(" ").toList) def toWords(sentences: List[String]): List[String] = sentences.flatMap(_.split(" ").toList) Monads ! Option[A] List[A]Future[A] Page[A]
  21. 21. Take away ● Monter son niveau d’abstration (ASM -> IMP -> OOP -> FUN !) ● Bannir null !!! ● Remplacer les ‘for’ par des fonctions de plus haut niveau ● Séparer le code technique de la logique métier

×