Monadic Warsaw: Monads in JavaScript

Piotr Kowalski @piecioshka

Monads in JavaScript

Who am I?

Kierownik Działu Aplikacji Webowych
Cyfrowy Polsat, Warsaw

JavaScript Ninja. Mac lover. Open source fan.
Organizer WarsawJS

"Kto chce szuka sposobu, kto nie chce szuka powodu."

Piotr Kowalski

Where am I?

Blog piecioshka.pl/blog
GitHub github.com/piecioshka
Twitter twitter.com/piecioshka
Soundcloud soundcloud.com/piecioshka

Short reminder:

What's monad?

The Monadic Laws

Eternal question:

If jQuery
is a monad?

jQuery (chaining) is not a monad

            $ ('div')
                  .addClass ('foo')
                  .text ('bar')
            $('div') // return jQuery object
            $($($('div'))) // returns the same
        

Simple Monad definition

            
                function wrap(value) {
                    return function () {
                        return value;
                    }
                }

                function bind(monad, f) {
                    return f(monad());
                }
            
        
            
                var monad = wrap('cookie');
                bind(monad, (value) => {
                    console.info(value);
                    return wrap(value);
                });
                monad = bind(monad, (value) => {
                    return wrap({ type: value });
                });
                bind(monad, (value) => {
                    console.log(value);
                });
            
        

Monads Types

Resources

  1. Monads & Gonads - Douglas Crockford (GoogleTechTalks)
  2. Monads in JavaScript
  3. Brian Beckman: Don't fear the Monad
  4. Monads and Objects
  5. So simple MONAD implementation
  6. Step by step into Monads
  7. Use monads in JavaScript without needing to know how they work.
  8. jQuery is not a monad

Fork me on GitHub