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

Do or don’t. Try there is not. Or is there? The power of monads explained. Sort of

Do or don’t. Try there is not. Or is there? The power of monads explained. Sort of

One of the great things about being a programmer is that you never stop learning. Even after having programmed for almost 35 years, I still improve on the way I write code. Recently the way I write code changed once again when I started to apply monads and especially the Try class.

During a recent project, my team created a small library that ports the behavior of the Scala Try monad. Although at first, this new monad didn't appeal to me, I soon really started to appreciate this style of programming, where we concatenate series of Map() and FlatMap() methods, using lambda’s, and avoiding abundant try-catch blocks, and many if statements and null checks.

In the meantime, I have contaminated many programmers with this style. Developers make it a sport to always start every method with a return statement. During this talk I’ll discuss lambda’s, closures and monads, and demonstrate the power of this simple monad, using many code examples (in Java, C#, and TypeScript). Don't hesitate to join in.

Sander Hoogendoorn

December 07, 2020
Tweet

More Decks by Sander Hoogendoorn

Other Decks in Technology

Transcript

  1. @aahoogendoorn | Do or don't. Try there is not. Or is there?
    DO OR DON’T. TRY THERE IS NOT.
    OR IS THERE?
    EXPLAINING MONADS. SORT OF.

    View Slide

  2. SANDER HOOGENDOORN
    CODE PHILOSOPHER
    Independent dad, speaker, author, traveler
    Freelance CTO, trainer, architect, programmer
    CURRENTLY
    CTO iBood
    [Chief architect Quby]
    Chief Architect MendriX
    BEFORE
    CTO ANVA
    CTO Klaverblad Verzekeringen
    Global agile thought leader Capgemini
    sanderhoogendoorn.com
    aahoogendoorn
    aahoogendoorn
    aahoogendoorn
    [email protected]
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  3. @aahoogendoorn | Do or don't. Try there is not. Or is there?
    WHAT IS A TRY?
    AND WHERE IS THIS TALK GOING TO?

    View Slide

  4. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  5. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  6. INTRODUCTION
    A BRIEF, INCOMPLETE AND
    MOSTLY WRONG HISTORY OF
    PROGRAMMING LANGUAGES
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  7. 1959 - After losing a bet with L. Ron Hubbard,
    Grace Hopper and several other sadists
    invent the Capitalization Of Boilerplate
    Oriented Language (COBOL) .
    Grace Hopper
    COBOL
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  8. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  9. 1964 - John Kemeny and Thomas Kurtz
    create BASIC, an unstructured programming
    language for non-computer scientists.
    John Kemeny
    BASIC
    1965 - Kemeny and Kurtz go to 1964.
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  10. 1968 - Go to statement considered harmful
    The go to statement as it stands is just too primitive;
    it is too much an invitation to make a mess of one's
    program.
    Edsger
    Dijkstra
    GOTO
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  11. LAMBDA’S
    THEN
    OK, AND CLOSURES TOO
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  12. 1970 - Guy Steele and Gerald Sussman create
    Scheme. Their work leads to a series of "Lambda the
    Ultimate" papers culminating in "Lambda the
    Ultimate Kitchen Utensil."
    Lambdas are relegated to relative obscurity until Java
    makes them popular by not having them.
    Guy Steele
    LAMBDA’S
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  13. A lambda expression is an anonymous function that you can use
    to create delegates or expression tree types. By using lambda
    expressions, you can write local functions that can be passed as
    arguments or returned as the value of function calls.
    Lambda expressions are particularly helpful for writing LINQ query
    expressions.
    From: C# Programming Guide
    LAMBDA EXPRESSION
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  14. PURE
    FUNCTIONS
    (JUST BRIEFLY, DON’T WORRY)
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  15. 1990 - A committee formed by Simon Peyton-Jones, Paul
    Hudak, Philip Wadler, Ashton Kutcher, and People for the
    Ethical Treatment of Animals creates Haskell, a pure, non-strict,
    functional language.
    Haskell gets some resistance due to the complexity of using
    monads to control side effects. Wadler tries to appease critics
    by explaining that "a monad is a monoid in the category of
    endofunctors, what's the problem?"
    Philip Wadler
    HASKELL
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  16. A pure function is a function where the return value is only
    determined by its input values, without observable side effects.
    This is how functions in math work: Math.cos(x) will, for the same
    value of x, always return the same result.
    Computing it does not change x. It does not write to log files, do
    network requests, ask for user input, or change program state. It’s
    a coffee grinder: beans go in, powder comes out, end of story.
    PURE FUNCTION
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  17. @aahoogendoorn | Do or don't. Try there is not. Or is there?
    IDEMPOTENT FUNCTIONS

    View Slide

  18. So if my programming
    language only supports
    pure functions,
    how do I handle
    any side effects?
    Using
    monads?
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  19. TRYING
    TO EXPLAIN
    MONADS
    (TO NON-HASKELL OR F#
    PROGRAMMERS OF COURSE)
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  20. Monads are amazing. They are simple things, almost
    trivially implemented, with enormous power to
    manage complexity.
    But understanding them is surprisingly difficult, and
    most people, once they have that ah-ha moment,
    seem to lose the ability to explain them to others.
    Douglas
    Crockford
    MONADS EXPLAINED…
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  21. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  22. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  23. Unit
    Bind Bind
    Get
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  24. Map( -> ) :
    FlatMap( -> ) :
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  25. MAYBE
    MY FIRST MONAD
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  26. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  27. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  28. The billion-dollar mistake
    In 1965, I was designing the first comprehensive type system
    for references in an object oriented language (ALGOL W). My
    goal was to ensure that all use of references should be
    absolutely safe, with checking performed automatically by the
    compiler.
    But I couldn’t resist the temptation to put in a null reference,
    simply because it was so easy to implement. I call it my billion-
    dollar mistake.
    Tony Hoare
    NULL
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  29. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  30. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  31. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  32. MONADS IN C#
    • SelectMany and LINQ
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  33. MONADS IN C#
    • Task
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  34. MONADS IN JAVASCRIPT / TYPESCRIPT
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  35. MONADS IN JAVASCRIPT / TYPESCRIPT
    • Promise
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  36. MONADS IN JAVA
    • Optional
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  37. CASE
    BUILDING A SIMPLE
    SWITCH MONAD
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  38. BUILDING A SIMPLE SWITCH MONAD
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  39. BUILDING A SIMPLE SWITCH MONAD
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  40. BUILDING A SIMPLE SWITCH MONAD
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  41. THE TRY CLASS
    ONE BAD-ASS MONAD
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  42. 1975 - IF-THEN-ELSE considered harmful
    Although the if-then-else construct for flow of control
    is simple in some senses, and certainly superior to
    unrestricted go to logic, the indefinite replication of
    this structure leads to undesirable program
    constructs.
    Gerald
    Weinberg
    IF
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  43. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  44. TRY
    SUCCESS FAILURE
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  45. @aahoogendoorn | Do or don't. Try there is not. Or is there?
    TRY

    View Slide

  46. @aahoogendoorn | Do or don't. Try there is not. Or is there?
    SUCCESS

    View Slide

  47. @aahoogendoorn | Do or don't. Try there is not. Or is there?
    FAILURE

    View Slide

  48. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  49. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  50. USING TRY MAKES TESTING LIGHTER
    • No need to test for Exceptions or Errors
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  51. TRY IN TYPESCRIPT
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  52. IN
    RETROSPECTIVE
    SOME FINAL THOUGHTS
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  53. WHY MONADS?
    Robust code
    Reduce complexity
    Focus on the real issue
    Hide implementation details
    Remove side effects
    Hardly any try-catch and if-else-then
    blocks
    Reduce duplication of code
    Improved readability of code
    Easier to maintain
    Easier to test, no need to test for side
    effects
    Once you go Try, you never go back

    View Slide

  54. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  55. NEVER STOP LEARNING
    BY DOING IT CONTINUOUSLY
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  56. AND NEVER FORGET TO HAVE FUN
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  57. FURTHER READING
    A brief, Incomplete, and Mostly Wrong History of Programming Languages
    http://james-iry.blogspot.nl/2009/05/brief-incomplete-and-mostly-wrong.html
    You Could Have Invented Monads! (And Maybe You Already Have)
    http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html
    Monads explained in C#
    http://mikhail.io/2016/01/monads-explained-in-csharp/
    Monads
    https://ericlippert.com/category/monads/
    Do. Or do not. There's no Try.
    https://github.com/aahoogendoorn/Monads
    There’s tons of category theory, monad, flatMap, pure function, Maybe, Optional, Try explanations
    @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  58. @aahoogendoorn | Do or don't. Try there is not. Or is there?

    View Slide

  59. REFERENCES
    AND QUESTIONS
    www.sanderhoogendoorn.com
    aahoogendoorn
    aahoogendoorn
    aahoogendoorn
    [email protected]
    quby.com/careers
    www.sanderhoogendoorn.com/decks-and-handouts

    View Slide

  60. SANDER HOOGENDOORN
    www.sanderhoogendoorn.com
    aahoogendoorn
    aahoogendoorn
    aahoogendoorn
    [email protected]

    View Slide