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.

SeneJug java_8_prez_122015

573 views

Published on

Functional programing with Java8 with Yakhya DABO

Published in: Software
  • Be the first to comment

  • Be the first to like this

SeneJug java_8_prez_122015

  1. 1. Yakhya DABO Software craftsman && DevOps Engineer @yakhyadabo (Birmingham, United Kingdom)
  2. 2. Work & Interests
  3. 3. Continuous Delivery Work and Interests
  4. 4. Software craftsmanship Work and Interests
  5. 5. Don't just code Monkey (M. Fowler) → Knowledge → Responsibility → Impact Work and Interests
  6. 6. Devoxx France Paris Jug Socrates Germany London Software craftsmanship Communities Work and Interests
  7. 7. Functional Programming in Java 8
  8. 8. Avoid technical debt Java 7 is no longer supported Java 9 coming soon
  9. 9. A programming paradigm that ... ● Treats function as primitive ● Avoids state and mutable data ● Is declarative Definition of Functional Programming
  10. 10. To be a function function succ(int y){ return y++; } Definition
  11. 11. int x = 1; function dummy(int y){ x++; return x+y; } Not to be a function Definition
  12. 12. int x = 1; function dummy(int y){ x++; return x+y; } Not to be a function Definition
  13. 13. Functions as First-Class Citizens - High-Order Functions - Lambda - Top-Level Functions Immutable data (Hadi Hariri Devoxx France 2015) Functional Language
  14. 14. Write less and expressive code Why do we need it ?
  15. 15. The Context
  16. 16. “Software product is embedded in a cultural matrix of applications, users, laws, and machines vehicles. These all change continually, and their changes inexorably force change upon the software product.” Frederic brooks The context
  17. 17. Every 18 months a CPU's transistors will shrink in size by a factor of two. Moor Law The context
  18. 18. - In 2002, limitations in a CPU's circuitry. - The multi-core processor was born. Moor Law (New Context) The context
  19. 19. Lambda project
  20. 20. “Reduce the gap between the serial and parallel versions of the same computation” Brian Goetz (Lambda project leader) The context
  21. 21. public List<Output> processInputs(List<Input> inputs) throws InterruptedException, ExecutionException { int threads = Runtime.getRuntime().availableProcessors(); ExecutorService service = Executors.newFixedThreadPool(threads); List<Future<Output>> futures = new ArrayList<Future<Output>>(); for (final Input input : inputs) { Callable<Output> callable = new Callable<Output>() { public Output call() throws Exception { Output output = new Output(); // process your input here and compute the output return output; } }; futures.add(service.submit(callable)); } service.shutdown(); List<Output> outputs = new ArrayList<Output>(); for (Future<Output> future : futures) { outputs.add(future.get()); } return outputs; } Pallelizing with OOP The context
  22. 22. Output out = input.process(treatment); Output out = Input.processInParallel(treatment); Pallelizing with FP The context
  23. 23. => Lambda expression => Functional Interface => Optional API => Stream API …. Java 8 main new features
  24. 24. Lambda expressions
  25. 25. (argument_list) -> function_body Lambda expression
  26. 26. x -> x + 1 ; (int x, int y) -> x + y ; () -> System.out.println("I am a lambda"); Lambda expressions Lambda expressions
  27. 27. X= (int x) -> x + 1 ; (int x, int y) -> { z = x + y ; z++; } Lambda expression Lambda expressions
  28. 28. Functional Interface
  29. 29. Provide target types for lambda expressions ... Has a single abstract method, ..., to which the lambda expression's parameter and return types are matched or adapted. Functional Interface
  30. 30. int x -> x * 2; public interface IntOperation { int operate(int i); } Functional Interface
  31. 31. - Function - Consumer - Supplier Functional Interface Common Functional Interfaces
  32. 32. Functions accept one argument and produce a result. @FunctionalInterface Interface Function<T,R>{ R apply(T t); } Functional Interface Function
  33. 33. // String to an integer Function<String, Integer> stringToInt = x -> Integer.valueOf(x); int toto = stringToInt(“10”); Functional Interface Function
  34. 34. Suppliers produce a result of a given generic type. Unlike Functions, Suppliers don't accept arguments. @FunctionalInterface Interface Supplier<T>{ T get() } Functional Interface Supplier
  35. 35. Supplier<Person> personSupplier = () -> new Person(); Person persion = personSupplier.get(); Functional Interface Supplier
  36. 36. Consumers represents operations to be performed on a single input argument. @FunctionalInterface public interface Consumer<T>{ void accept(T t) } Functional Interface Consumer
  37. 37. Consumer<Person> personPrinter = (p) -> System.out.println("Hello, " + p.firstName); personPrinter.accept(new Person("Luke", "Skywalker")); Functional Interface Consumer
  38. 38. Predicates are boolean-valued functions of one argument. @FunctionalInterface Interface Predicate<T>{ boolean test(T t) } Functional Interface Predicate
  39. 39. Predicate<String> isNotEmpty = s -> s.isEmpty(); Predicate<String> isNotEmpty = isEmpty.negate(); Functional Interface Predicate
  40. 40. Type Inference
  41. 41. What is the type of [x -> 2 * x] ? public interface IntOperation { int operate(int i); } public interface DoubleOperation { double operate(double i); } Type Inference Type Inference
  42. 42. Java does have type inferencing when using generics. public <T> T foo(T t) { return t; } Type Inference Type Inference
  43. 43. ● DoubleOperation doubleOp = x -> x * 2; ● IntOperation intOp = x -> x * 2; Inference by declaration Type Inference
  44. 44. ● DoubleOperation doubleOp ; doubleOp = x -> x * 2; ● IntOperation intOp ; intOp = x -> x * 2; Inference by affectation Type InferenceType Inference
  45. 45. public Runnable toDoLater() { return () -> System.out.println("later"); } Inference by return type Type Inference
  46. 46. Object runnable = (Runnable) () -> { System.out.println("Hello"); }; Object callable = (Callable) () → { System.out.println("Hello"); }; Inference by cast Type Inference
  47. 47. Stream API
  48. 48. Collections support operations that work on a single element : add(), remove() and contains() Stream API
  49. 49. Streams have bulk operations that access all elements in a sequence. forEach(), filter(), map(), and reduce() Stream API
  50. 50. Three central concepts of functional programming Map/Filter/Reduce Stream API
  51. 51. map(f, [d0, d1, d2...]) -> [f(d0), f(d1), f(d2)...] Map Stream API
  52. 52. Legacy List<String> names = … List<Person> people = ... for (Person person : people){ names.add(person.getName()); } Map Stream API
  53. 53. List<Person> people = ... List<String> names = people.streams() .map(person -> person.getName()) .collect(Collectors.toList()); Functional Style Map Stream API
  54. 54. filter(is_even, [1, 2, 7, -4, 3, 12.0001]) -> [2, -4] Filter Stream API
  55. 55. List<Person> people = … List<Person> peopleOver50 = ... for (Person person : people){ if(person.getAge() >= 50){ peopleOver50.add(person.getName()); } } Legacy Filter Stream API
  56. 56. List<Person> people = … List<String> peopleOver50 = people.streams() .filter(person -> person.getAge()>50) .collect(Collectors.toList()); Functional Style Filter Stream API
  57. 57. reduce(add, [1, 2, 4, 8]) -> 15 Reduce Stream API
  58. 58. List<Integer> salaries = … int averageSalaries = ... for (Integer salary : salaries){ … } Legacy Stream API Reduce
  59. 59. List<Integer> salaries = … Int averageAge = salaries.streams() .average(); Functional Style Stream API Reduce
  60. 60. int sum = shapes.stream() .filter(s -> s.getColor() == BLUE) .mapToInt(s -> s.getWeight()) .sum(); Stream API Pipeline Structure
  61. 61. Optional API Forgetting to check for null references is a common source of bugs in application code. One way to eliminate NullPointerExceptions is to make sure that methods always return a non null value.
  62. 62. Example User user = userRepository.getUser(“id”); If (user != null){ user.doSomeThing(); } Optional API
  63. 63. With Optional Optional<User> user = userRepository.getUser(“id”); If (user.isPresent()){ user.doSomeThing(); } Optional API
  64. 64. Optional API Functional style Optional<User> user = userRepository.getUser(“id”); user.ifPresent(u - > u.doSomeThing());
  65. 65. Optional API Functional style Optional<User> user = userRepository.getUser(“id”); user.ifPresent(u - > u.doSomeThing()); userRepository.getUser(“id”) .ifPresent(user - > user.doSomeThing());
  66. 66. Optional<SomeObject> opt = Optional.empty(); Optional<SomeObject> opt = Optional.of(notNull); Optional<SomeObject> opt = Optional.ofNullable(mayBeNull); Declaration Optional API
  67. 67. Common Optional patterns Optional API
  68. 68. if (x != null && x.contains("ab")) { print(x); } Optional API If (condition) doSomething
  69. 69. if (x != null && x.contains("ab")) { print(x); } Optional API If (condition) doSomething opt .filter(x -> x.contains("ab")) .ifPresent(x -> print(x));
  70. 70. Optional API If (condition) return else return if (x !=null){ return x.length(); } else { return -1; }
  71. 71. Optional API If (condition) return else return opt .map(x -> x.length() ) .orElse(-1); if (x !=null){ return x.length(); } else { return -1; }
  72. 72. if (s != null && !s.isEmpty()){ return s.charAt(0); else throw new IllegalArgumentException(); } Optional API Throwing Exception
  73. 73. if (s != null && !s.isEmpty()){ return s.charAt(0); else throw new IllegalArgumentException(); } Optional API Throwing Exception opt .filter(s -> ! s.isEmpty()). .map(s -> s.charAt(0)). .orElseThrow(IllegalArgumentException::new);
  74. 74. if (x !=null){ print(x.length()); } else { Print(“-1”); } Optional API If (condition) do else … do
  75. 75. if (x !=null){ print(x.length()); } else { Print(“-1”); } Optional API If (condition) do else … do ???
  76. 76. Avoid having states Avoid using for, while Think twice before using if Summary
  77. 77. Curryfication, partial functions Dependency injection Monad ... Next step ...
  78. 78. Function<Integer,Function<Integer,Integer>> sum = x -> y -> x + y; Function<Integer, Integer> plus10 = sum.apply(10); Integer res = plus10.apply(5); … Next step Curryfication
  79. 79. ... … Next step Monad
  80. 80. ... … Next step Dependency injection
  81. 81. ... … Next step Future of GoF
  82. 82. Futher readings Devoxx France 2015 : Hadi Hariri (Refactoring to functional) UK 2014 : Raoul Gabriel Urma && Richard Warburton Pragmatic Functional Refactoring with Java 8 Books ● Functional programming for Java developers ● Java SE8 for the Really Impatient ● Java 8 in action

×