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

Fight Complexity with Functional Programming

Fight Complexity with Functional Programming

More about this talk can be found here: https://overfullstack.github.io/posts/fight-complexity-with-fp/

5b88ad20a23d5cacecb20eb80f5f69d8?s=128

Gopal S Akshintala

August 21, 2019
Tweet

Transcript

  1. overfullstack.github.io

  2. @GopalAkshintala - Some Manager (Not Mine!) FP is cool ,

    coz it’s complex enough to make devs feel like they are doing something smart!
  3. @GopalAkshintala

  4. Dev Holy Wars @GopalAkshintala

  5. IDE Theme Light Vs Dark @GopalAkshintala

  6. Eclipse Vs Intellij @GopalAkshintala

  7. OOP Vs FP ? @GopalAkshintala

  8. @GopalAkshintala Immutability Referential Transparency Side-effects @ Boundaries FP Principles …………………………………………………

    ………………………………………………… OOP Principles
  9. Imperative Vs Declarative JavaDev @GopalAkshintala

  10. Imperative Code - - - Imperative @GopalAkshintala

  11. Let’s Concatenate last names in a team with a delimiter

    lastName1 lastName2 lastName2 @GopalAkshintala Team Building!
  12. @GopalAkshintala What about Corner cases?

  13. @GopalAkshintala lastName1 lastName2 lastName2

  14. @GopalAkshintala

  15. @GopalAkshintala Imperative Declarative

  16. @GopalAkshintala

  17. @GopalAkshintala Imperative Declarative

  18. @GopalAkshintala Imperative Declarative

  19. @GopalAkshintala Imperative Declarative

  20. @GopalAkshintala

  21. @GopalAkshintala

  22. Declarative Imperative Code - - - Imperative Vs Declarative @GopalAkshintala

  23. The FP part in Java The Java @GopalAkshintala

  24. How to Concatenate Last names in Parallel? @GopalAkshintala

  25. @GopalAkshintala

  26. How-to-dos What-to-dos Looping through the list Aggregating the results Validating

    the names Extracting last names @GopalAkshintala
  27. @GopalAkshintala

  28. The Context:Core Philosophy Do Things differently without doing Different things!

    @GopalAkshintala
  29. Stream Busted Stream Monad @GopalAkshintala

  30. @GopalAkshintala Reading about Functors, Monads, Applicatives ! Kotlin Functors, Applicatives,

    And Monads in Pictures (3 parts) @Aballano
  31. Source MayBe Monad Context Nothing Value @GopalAkshintala

  32. Source @GopalAkshintala

  33. Source @GopalAkshintala

  34. Source @GopalAkshintala

  35. Source @GopalAkshintala f(g(h(x)))

  36. Monad Composability @GopalAkshintala

  37. @GopalAkshintala Monad Composability

  38. Are Monads used in Enterprise Software? @GopalAkshintala

  39. Vader @GopalAkshintala

  40. REST APIs @GopalAkshintala

  41. REST Payload @GopalAkshintala

  42. Simple Data Validations Nested Validations Stateful Validations @GopalAkshintala

  43. The Batch Validation Framework @GopalAkshintala

  44. Valid/Invalid @GopalAkshintala

  45. • Configure the order of Validations • Cross-Share Common &

    Nested Validations • Fail-Fast or Error-Accumulation • Partial failures Framework Requirements @GopalAkshintala
  46. @GopalAkshintala Validation layer Service layer DB layer Aggregated Response

  47. • Accommodate a century validations • Unit testability • No

    compromise on Performance Framework Meta Requirements @GopalAkshintala
  48. @GopalAkshintala

  49. One Egg One Validation Many Eggs One Validation @GopalAkshintala

  50. Many Eggs Many Validations ! @GopalAkshintala

  51. Chaos @GopalAkshintala

  52. Chain of Responsibility @GopalAkshintala Is FP the Best Fit?

  53. @GopalAkshintala 2D Problem Eggs Validations

  54. Making of a Extensible Machine @GopalAkshintala Validations Configuration Orchestration (What-to-do)

    (How-to-do) (How-to-do)
  55. Immutable Eggs @GopalAkshintala

  56. @GopalAkshintala Heterogeneous DataTypes in pipeline

  57. Uniform DataTypes with Monad Boxes @GopalAkshintala

  58. Either Monad Egg Validation Failure Context io.vavr.control.Either @GopalAkshintala Left Right

  59. Standardize Validations @GopalAkshintala

  60. @GopalAkshintala static boolean validate1Simple(Map<Integer, ValidationFailure> badEggFailureBucketMap, int eggIndex, Iterator<Egg> iterator,

    Egg eggToBeValidated) { if (!simpleOperation1(eggToBeValidated)) { iterator.remove(); badEggFailureBucketMap.put(eggIndex, NO_EGG_TO_VALIDATE_1); return false; } return true; } static final Validator<ImmutableEgg, ValidationFailure> validate1Simple = validatedEgg "-> validatedEgg .filter(Operations"::simpleOperation1) .getOrElse(() "-> Either.left(NO_EGG_TO_VALIDATE_1)); validate1Simple
  61. @GopalAkshintala @FunctionalInterface public interface Validator<ValidatableT, FailureT> extends Function1<Either<FailureT, ValidatableT>, Either<FailureT,

    ValidatableT">> { } Validator
  62. @GopalAkshintala private static boolean validate2Throwable(Map<Integer, ValidationFailure> badEggFailureBucketMap, int eggIndex, Iterator<Egg>

    iterator, Egg eggToBeValidated) { try { if (!throwableOperation2(eggToBeValidated)) { iterator.remove(); badEggFailureBucketMap.put(eggIndex, TOO_LATE_TO_HATCH_2); return false; } } catch (Exception e) { iterator.remove(); badEggFailureBucketMap.put(eggIndex, ValidationFailure.withErrorMessage(e.getMessage())); return false; } return true; } static final ThrowableValidator<ImmutableEgg, ValidationFailure> validate2Throwable = validatedEgg "-> validatedEgg .filterOrElse(Operations"::throwableOperation2, ignore "-> TOO_LATE_TO_HATCH_2); validate2Throwable
  63. @GopalAkshintala @FunctionalInterface public interface ThrowableValidator<ValidatableT, FailureT> extends CheckedFunction1<Either<FailureT, ValidatableT>, Either<FailureT,

    ?">> { } ThrowableValidator
  64. Configure the order of Validations Cross-Share Common & Nested Validations

    Fail-Fast or Error-Accumulation Partial failures Framework How-to-dos @GopalAkshintala
  65. @GopalAkshintala Lift ThrowableValidator<ParentT, FailureT> Validator<ParentT, FailureT>

  66. Configure the order of Validations Cross-Share Common & Nested Validations

    Fail-Fast or Error-Accumulation Partial failures Framework How-to-dos @GopalAkshintala
  67. @GopalAkshintala Lift childValidation: Validator<ChildT, FailureT> ParentValidation: Validator<ParentT, FailureT>

  68. @GopalAkshintala Lift Validator<Yolk, FailureT> Validator<ChickenEgg, FailureT> Validator<Yolk, FailureT> Validator<DinoEgg, FailureT>

  69. Configure the order of Validations Cross-Share Common & Nested Validations

    Fail-Fast or Error-Accumulation Partial failures Framework How-to-dos @GopalAkshintala
  70. Transformation map flatMap filter Aggregation reduce collect groupBy zip,unzip foldLeft,foldRight

    scanLeft,scanRight @GopalAkshintala Dozen FP Operations for Daily Programming
  71. Configure the order of Validations Cross-Share Common & Nested Validations

    Fail-Fast or Error-Accumulation Partial failures Framework How-to-dos @GopalAkshintala
  72. Scalability?

  73. What about Complexity? @GopalAkshintala

  74. FP is NOT Complex It fights Cognitive-Complexity @GopalAkshintala

  75. @GopalAkshintala Min. Cognitive-Complexity Same Time-Complexity

  76. Railway-Oriented Programming Scott Wlaschin @GopalAkshintala

  77. @GopalAkshintala bit.ly/imp-vs-dec bit.ly/ro-validation bit.ly/fcwfp-slides { }

  78. @GopalAkshintala

  79. @GopalAkshintala gopal.akshintala@gmail.com

  80. @GopalAkshintala