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

JavaSlang JavaDayKiev 2016

Grzegorz Piwowarek
October 17, 2016
62

JavaSlang JavaDayKiev 2016

Grzegorz Piwowarek

October 17, 2016
Tweet

Transcript

  1. Javaslang - Functional Java
    Done Right
    Grzegorz Piwowarek
    [email protected]
    1

    View Slide

  2. Me
    [email protected]
    Yoyo Player
    Musician
    2

    View Slide

  3. [email protected]
    Twitter
    GitHub
    ...everywhere else
    3

    View Slide

  4. pivovarit#2152
    4

    View Slide

  5. Java 8
    5

    View Slide

  6. "Functional" Java8:
    Lambda Expressions
    Optional, Stream, CompletableFuture
    ...
    6

    View Slide

  7. 7

    View Slide

  8. 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

    View Slide

  9. List foo(List list);
    9

    View Slide

  10. List foo(List 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

    View Slide

  11. Potential inputs:
    declared params
    instance state
    global state
    Potential outputs:
    declared returned value
    mutable instance state
    mutable global state
    mutable params
    exceptions
    11

    View Slide

  12. Potential inputs:
    declared params
    Potential outputs:
    declared returned value
    12

    View Slide

  13. Functional programming is not
    about lambdas.
    13

    View Slide

  14. "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

    View Slide

  15. Immutability
    Referential
    Transparency
    15

    View Slide

  16. 16

    View Slide

  17. 17

    View Slide

  18. Java Collections API:
    add()
    remove()
    clear()
    ...
    18

    View Slide

  19. Date date = new Date();
    Set dates = new HashSet<>();
    dates.add(date);
    date.setTime(42);
    dates.contains(date); //false
    19

    View Slide

  20. 20

    View Slide

  21. λ
    Tuple
    Value
    21

    View Slide

  22. λ
    Function2 sum = (a, b) -> a + b;
    CheckedFunction2 sum = (a, b) -> a + b;
    composition
    lifting
    currying
    memoization
    22

    View Slide

  23. Lifting
    Function2 divideBy = (a, b) -> a / b;
    Function2> lift = lift(divideBy);
    23

    View Slide

  24. Currying
    Function2 sum = (a, b) -> a + b;
    Function1> curried = sum.curried();
    Function1 add2 = curried.apply(2);
    24

    View Slide

  25. Memoization
    Function2 sum = (a, b) -> a + b;
    Function2 memoizedSum = sum.memoized();
    25

    View Slide

  26. Function0 mem = Function0.of(Math::random).memoized();
    double randomValue1 = memoizedRand.apply();
    double randomValue2 = memoizedRand.apply();
    then(randomValue1).isEqualTo(randomValue2);
    26

    View Slide

  27. Tuple
    Tuple2 java8 = Tuple.of("Java", 8);
    Tuple2 that = java8.map(
    s -> s + "slang",
    i -> i / 4);
    String transform = java8.transform((s, i) -> s + i);
    Iterable> objects = java8.toSeq()
    27

    View Slide

  28. Value
    Option
    Try
    Lazy
    Either
    Future
    Validation
    Collections API
    public interface Value extends Iterable
    28

    View Slide

  29. Option
    29

    View Slide

  30. final String result = Option.of(somethingNullable)
    .map(Object::toString)
    .map(String::toLowerCase)
    .getOrElse(() -> "DEFAULT VALUE");
    30

    View Slide

  31. List> list = ...
    list.stream()
    .filter(Optional::isPresent)
    .map(Optional::get)
    .collect(toList());
    List> list = ...
    list.flatMap(o -> o);
    Java 8
    Javaslang
    31

    View Slide

  32. Try
    32

    View Slide

  33. Try.of(() -> new URI("bebebe"))
    .map(URI::getScheme)
    .getOrElse(() -> "UNKNOWN");
    33

    View Slide

  34. Lazy
    34

    View Slide

  35. Lazy lazy = Lazy.of(this::veryLongComputation)
    .map(Object::toString)
    .map(String::toUpperCase);
    lazy.get();
    35

    View Slide

  36. Functional Data
    Structures
    36

    View Slide

  37. Functional Data Structures:
    Persistent
    Immutable
    with Referential Transparent methods
    37

    View Slide

  38. Seq
    38

    View Slide

  39. 39

    View Slide

  40. public static Stream fibonacci() {
    return Stream.of(1, 1)
    .appendSelf(self -> self.zip(self.tail())
    .map(t -> t._1 + t._2));
    }
    40

    View Slide

  41. Pattern Matching
    41

    View Slide

  42. String on = Match(i).of(
    Case(x -> x % 2 == 0, "Even"),
    Case(x -> x % 2 == 1, "Odd"),
    Case($(), "?"));
    Option on = Match(i).option(
    Case(x -> x % 2 == 0, "Even"),
    Case(x -> x % 2 == 1, "Odd"));
    42

    View Slide

  43. String on = Match(i).of(
    Case($(1), "One"),
    Case($(2), "Two"),
    Case($(), "Something else"));
    43

    View Slide

  44. 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

    View Slide

  45. String actual = Match(opt).of(
    Case(Some($()), String::valueOf),
    Case(None(), "no value"));
    45

    View Slide

  46. 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

    View Slide

  47. Match(param).of(
    Case(isIn("-h", "--help"), o -> run(outerWorld::displayHelp)),
    Case(isIn("-v", "--version"), o -> run(outerWorld::displayVersion)),
    Case($(), o -> {}));
    47

    View Slide

  48. Try.of(this::doSomething)
    .recover(x -> Match(x).of(
    Case(instanceOf(Exception_1.class), ...),
    Case(instanceOf(Exception_2.class), ...),
    Case(instanceOf(Exception_n.class), ...)
    ))
    .getOrElse(other);
    48

    View Slide

  49. www.javaslang.io
    49

    View Slide

  50. View Slide