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.
  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?
  3. @aahoogendoorn | Do or don't. Try there is not. Or

    is there? WHAT IS A TRY? AND WHERE IS THIS TALK GOING TO?
  4. INTRODUCTION A BRIEF, INCOMPLETE AND MOSTLY WRONG HISTORY OF PROGRAMMING

    LANGUAGES @aahoogendoorn | Do or don't. Try there is not. Or is there?
  5. 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?
  6. 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?
  7. 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?
  8. LAMBDA’S THEN OK, AND CLOSURES TOO @aahoogendoorn | Do or

    don't. Try there is not. Or is there?
  9. 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?
  10. 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?
  11. 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?
  12. 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?
  13. 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?
  14. TRYING TO EXPLAIN MONADS (TO NON-HASKELL OR F# PROGRAMMERS OF

    COURSE) @aahoogendoorn | Do or don't. Try there is not. Or is there?
  15. 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?
  16. Map( -> ) : FlatMap( -> ) : @aahoogendoorn |

    Do or don't. Try there is not. Or is there?
  17. 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?
  18. MONADS IN C# • SelectMany and LINQ @aahoogendoorn | Do

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

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

    don't. Try there is not. Or is there?
  21. 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?
  22. 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?
  23. 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
  24. NEVER STOP LEARNING BY DOING IT CONTINUOUSLY @aahoogendoorn | Do

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

    don't. Try there is not. Or is there?
  26. 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?