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

JavaSlang JavaDayKiev 2016

Grzegorz Piwowarek
October 17, 2016
75

JavaSlang JavaDayKiev 2016

Grzegorz Piwowarek

October 17, 2016
Tweet

Transcript

  1. 7

  2. Java 8 issues: Only 3 functional control structures No memoization

    No lifting No pattern matching No tuples Optional not Serializable/Iterable Lack of Stream/Optional in APIs Checked exceptions in functions "Type pollution" list.stream().map(...).collect(toList()) ... 8
  3. List<String> foo(List<String> list) { if (this.state.isEmpty() || list.isEmpty()) { throw

    new IllegalStateException("Ouch..."); } list.add(this.state.get(0)); list.add(CONSTANT); this.moreState.addAll(list); OtherClass.mutableStaticField = list; return list; } 10
  4. Potential inputs: declared params instance state global state Potential outputs:

    declared returned value mutable instance state mutable global state mutable params exceptions 11
  5. "Functional programming is about writing pure functions, about removing hidden

    inputs and outputs as far as we can, so that as much of our code as possible just describes a relationship between inputs and outputs." http://blog.jenkster.com/2015/12/what-is-functional-programming.html 14
  6. 16

  7. 17

  8. Date date = new Date(); Set<Date> dates = new HashSet<>();

    dates.add(date); date.setTime(42); dates.contains(date); //false 19
  9. 20

  10. λ Function2<Integer, Integer, Integer> sum = (a, b) -> a

    + b; CheckedFunction2<Integer, Integer, Integer> sum = (a, b) -> a + b; composition lifting currying memoization 22
  11. Lifting Function2<Integer, Integer, Integer> divideBy = (a, b) -> a

    / b; Function2<Integer, Integer, Option<Integer>> lift = lift(divideBy); 23
  12. Currying Function2<Integer, Integer, Integer> sum = (a, b) -> a

    + b; Function1<Int, Function1<Int, Int>> curried = sum.curried(); Function1<Integer, Integer> add2 = curried.apply(2); 24
  13. Memoization Function2<Integer, Integer, Integer> sum = (a, b) -> a

    + b; Function2<Integer, Integer, Integer> memoizedSum = sum.memoized(); 25
  14. Tuple Tuple2<String, Integer> java8 = Tuple.of("Java", 8); Tuple2<String, Integer> that

    = java8.map( s -> s + "slang", i -> i / 4); String transform = java8.transform((s, i) -> s + i); Iterable<?> objects = java8.toSeq() 27
  15. 39

  16. String on = Match(i).of( Case(x -> x % 2 ==

    0, "Even"), Case(x -> x % 2 == 1, "Odd"), Case($(), "?")); Option<String> on = Match(i).option( Case(x -> x % 2 == 0, "Even"), Case(x -> x % 2 == 1, "Odd")); 42
  17. Match(localDate).of( Case(LocalDate(2016, 2, 13), () -> "2016-02-13"), Case(LocalDate(2016, $(), $_),

    m -> "month " + m + " in 2016"), Case(LocalDate($(2016), $(), $_), (y, m) -> "my" + m + y), Case($_, () -> "(catch all)") ); 44
  18. Match(i).of( Case(is(1), "1"), Case(isIn(2, 3), "2 or 3"), Case(anyOf(is(4), noneOf(is(5),

    is(6))), "4 or not (5 or 6)"), Case(instanceOf(String.class), "String content"), Case($(), "?") ); http://koziolekweb.pl/2016/06/18/pattern-matching-w-javie-z-javaslang-ii/ 46