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

Javaslang

 Javaslang

David Schmitz

March 23, 2017
Tweet

More Decks by David Schmitz

Other Decks in Technology

Transcript

  1. #Devoxx #Javaslang @koenighotze Senacor Technologies Programmer! That’s me coding Scala

    David Schmitz / @koenighotze Senacor Technologies Programmer! That’s me coding Scala
  2. #Devoxx #Javaslang @koenighotze Side-effects are evil try { int i

    = 1/0; } catch (Throwable t) { … } Exceptions are goto-statements :(
  3. #Devoxx #Javaslang @koenighotze Enter Java 8 (a) -> a +

    2 list.stream().filter… Excitement…
  4. #Devoxx #Javaslang @koenighotze Excitement?…until users.stream() .filter(user -> { try {

    return user.validate(); } catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  5. #Devoxx #Javaslang @koenighotze Fixing Things…. users.stream() .filter(user -> { try

    { return user.validate(); } catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  6. #Devoxx #Javaslang @koenighotze Functional collections List.ofAll(users) .filter(user -> { try

    { return user.validate(); } catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  7. #Devoxx #Javaslang @koenighotze No need for Collectors List.ofAll(users) .filter(user ->

    { try { return user.validate(); } catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  8. #Devoxx #Javaslang @koenighotze No need for Collectors List.ofAll(users) .filter(user ->

    { try { return user.validate(); } catch (Exception ex) { return false; }}); .collect(Collectors.toLis t());
  9. #Devoxx #Javaslang @koenighotze Wrapping Exceptions List.ofAll(users) .filter(user -> { try

    { return user.validate(); } catch (Exception ex) { return false; }}); .collect(Collectors.toLis t());
  10. #Devoxx #Javaslang @koenighotze Mutable Collections are Evil Returning void ==

    Side-Effect! interface Collection<E> { … void clear(); } interface Collection<E> { … void clear(); }
  11. #Devoxx #Javaslang @koenighotze List<~> t = List.of(“F95”); List<~> t2 =

    t.prepend(“FCK”); F95 / FCK t t2 Persistent and Efficient
  12. #Devoxx #Javaslang @koenighotze Streams are glorified Iterators Stream<String> jdk =

    Stream.of("a", “B"); jdk.map(String::toUpperCase); jdk.map(String::toLowerCase);
  13. #Devoxx #Javaslang @koenighotze java.lang.IllegalStateException: stream has already been operated upon

    or closed at java.util.stream.AbstractPipeline.<init>(AbstractPipeline.java:203) at java.util.stream.ReferencePipeline.<init>(ReferencePipeline.java:94) at java.util.stream.ReferencePipeline$StatelessOp.<init>(ReferencePipeline.java :618) at java.util.stream.ReferencePipeline$3.<init>(ReferencePipeline.java:187) at java.util.stream.ReferencePipeline.map(ReferencePipeline.java:186)
  14. #Devoxx #Javaslang @koenighotze Javaslang Streams Stream<String> slang = Stream.of("a", "B");

    slang.map(String::toUpperCase); // “A”,“B” slang.map(String::toLowerCase); // “a”,“b”
  15. #Devoxx #Javaslang @koenighotze Javaslang Streams Stream<String> slang = Stream.of("a", "B");

    slang.map(String::toUpperCase) .take(2); slang.map(String::toLowerCase) .take(2);
  16. #Devoxx #Javaslang @koenighotze Javaslang Streams Stream<String> slang = Stream.of("a", "B");

    slang.map(String::toUpperCase) .take(2);// “A”,“B” slang.map(String::toLowerCase) .take(2);// “a”,“b”
  17. #Devoxx #Javaslang @koenighotze “Find a user and, if an address

    is available, fetch the user’s street”
  18. #Devoxx #Javaslang @koenighotze Cascading Pile of Shame User user =

    repo.findOne("id"); if (user != null) { Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  19. #Devoxx #Javaslang @koenighotze Fixing the Pile of Shame User user

    = repo.findOne("id"); if (user != null) { Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  20. #Devoxx #Javaslang @koenighotze Option all the Things User user =

    repo.findOne("id"); if (user != null) { Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  21. #Devoxx #Javaslang @koenighotze Option all the Things Option<User> user =

    repo.findOne("id"); if (user != null) { Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  22. #Devoxx #Javaslang @koenighotze Option all the Things Option<User> user =

    repo.findOne("id"); if (user != null) { Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  23. #Devoxx #Javaslang @koenighotze Option all the Things Option<User> user =

    repo.findOne("id"); user.flatMap(User::getAddress) Address address = user.getAddress(); if (null != address) { return address.getStreet(); } } Option<User> user = repo.findOne("id"); user.flatMap(User::getAddress) Address address = user.getAddress(); if (null != address) { return address.getStreet(); } } Option<Address> getAddress()
  24. #Devoxx #Javaslang @koenighotze Option all the Things Option<User> user =

    repo.findOne("id"); user.flatMap(User::getAddress) Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  25. #Devoxx #Javaslang @koenighotze Option all the Things Option<User> user =

    repo.findOne("id"); user.flatMap(User::getAddress) Address address = user.getAddress(); .map(Address::getStreet) return address.getStreet(); } }
  26. #Devoxx #Javaslang @koenighotze Nice Validation Code public static String check(String

    iban){ if (validationMagic(iban)) { return iban; } throw new IllegalArgumentException(“Peng”); }
  27. #Devoxx #Javaslang @koenighotze Awesome WTF Code String iban; try {

    iban = check("AL47"); } catch (IllegalArgumentException ex) { iban = ""; }
  28. #Devoxx #Javaslang @koenighotze From Exceptions to Options String iban =

    lift(Iban::check) .apply("AL47...") .getOrElse("");
  29. #Devoxx #Javaslang @koenighotze From Exceptions to Options String iban =

    lift(Iban::check) .apply("AL47...") .getOrElse("");
  30. #Devoxx #Javaslang @koenighotze HTTP Handling Fixed *Note: some type details

    missing Match(res.getStatusCode()) .of(
 Case($(OK), res.getBody()),
 Case($(), emptyList())
 );
  31. #Devoxx #Javaslang @koenighotze HTTP Handling Fixed *Note: some type details

    missing Match(res.getStatusCode()) .of(
 Case($(OK), res.getBody()),
 Case($(), emptyList())
 );
  32. #Devoxx #Javaslang @koenighotze HTTP Handling Fixed *Note: some type details

    missing Match(res.getStatusCode()) .of(
 Case($(OK), res.getBody()),
 Case($(), emptyList())
 );