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

Vavr - functional Java the easy way

David Schmitz
September 07, 2018
75

Vavr - functional Java the easy way

David Schmitz

September 07, 2018
Tweet

Transcript

  1. @koenighotze try { int i = 1/0; } catch (Throwable

    t) { … } Exceptions are goto-statements :(
  2. @koenighotze users.stream() .filter(user -> { try { return user.validate(); }

    catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  3. @koenighotze users.stream() .filter(user -> { try { return user.validate(); }

    catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  4. @koenighotze List.ofAll(users) .filter(user -> { try { return user.validate(); }

    catch (Exception ex) { return false; }}) .collect(Collectors.toList());
  5. @koenighotze List.ofAll(users) .filter(user -> { try { return user.validate(); }

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

    catch (Exception ex) { return false; }}); .collect(Collectors.toLis t());
  7. @koenighotze List.ofAll(users) .filter(user -> { try { return user.validate(); }

    catch (Exception ex) { return false; }}); .collect(Collectors.toLis t());
  8. @koenighotze Returning void == side-effect! interface Collection<E> { … void

    clear(); } interface Collection<E> { … void clear(); }
  9. @koenighotze Painting by Gustav Courbet java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection .add(Collections.java:1055) at

    java.util.stream.AbstractPipeline.<init> (AbstractPipeline.java:203) at java.util.stream.ReferencePipeline.<init> (ReferencePipeline.java:94)
  10. @koenighotze Painting by Gustav Courbet 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)
  11. @koenighotze User user = repo.findOne("id"); if (user != null) {

    Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  12. @koenighotze User user = repo.findOne("id"); if (user != null) {

    Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  13. @koenighotze User user = repo.findOne("id"); if (user != null) {

    Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  14. @koenighotze Option<User> user = repo.findOne("id"); if (user != null) {

    Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  15. @koenighotze Option<User> user = repo.findOne("id"); if (user != null) {

    Address address = user.getAddress(); if (null != address) { return address.getStreet(); } }
  16. @koenighotze 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()
  17. @koenighotze public static String check(String iban){ if (validationMagic(iban)) { return

    iban; } throw new IllegalArgumentException(“Peng”); }
  18. @koenighotze String iban; try { iban = check(“AL47…”); } catch

    (IllegalArgumentException ex) { iban = ""; }
  19. @koenighotze Declarative, infinite test cases Have you tested for all

    characters… …even for ? Have you tested usernames like… ౧ప唆⏑㐿䯰㨼ᮁ娤㮘?搃蘸阁㌃ᡧ㫈葷㖒ܪ匘ᤫ䳴㻅 ⦷♻痯ヲ㙃銐璚ೃⶕᄰ 䩰㒭㙵闆䩟嗀嗀侀
  20. @koenighotze 2017-03-21 10:45:59.913 INFO 45701 --- FrameworkServlet '': initialization started

    2017-03-21 10:45:59.925 INFO 45701 --- FrameworkServlet '': initialization completed in 12 ms Storing User@217b0952[username=rwxbeoigyesbeqqz,email=W`[email protected], Storing User@4e6280de[username=vptafghfwuwwrwall,[email protected],,, Storing User@2fca282c[username=qmhkjdtvbtjzfciwcceqgzfznzkhhcokiyoipdefbr,[email protected]. Storing User@64d53f0d[publicId=e9d7a121-9f23-483a-828a-f9e3045fc297,username=unflrpvztxtmi... ... Storing User@1b10f60e[publicId=6f084c18-415c-42c4-b1a8-00c5c1fc9e67,username=xwhpdpjowirsmjym... Storing User@4b916cc2[publicId=a2b9db2c-0189-4fe8-843d-e709ef3886fa,username=yxdidpexnayyjpzo... Should not go boom: OK, passed 1000 tests in 3719 ms.
  21. @koenighotze Should not go boom: Falsified after 23 passed tests

    in 3005 ms. java.lang.AssertionError: Expected satisfied check result but was Falsified(…, sample = ( 䒖 ꜏, 燤䠽㏊密ᵓ〞খᄀ ꌎ ⬆鹮 ۄ鄽㏫魨 ࠞ⽆⢕ 㣁㸎Ԩ 䏨➰ìឧ寅罟 溌椡ﲡࣇ欙ㄓῴﯯ缲ꢶꇞ⌽ꪂ惗 㛨蘄䏄⩂粻ㇼ?ವ㝓㭃, fzqlbkljxhfrghllzcthvgqiglaabihkzgsqwgfcichamyonmayiewwsfwmw ntzvozqqydkqillhpyi, [email protected] V74.E.-----2T.z97..3f1ZM6))