Really Cool Stuff That Makes Slow Presentation Material – Underscore and Lodash – What do you get?



Really Cool Stuff That Makes Slow Presentation Material – Underscore and Lodash – What do you get?

0 0


functionaljs-presentation

A first presentation on functional programming in Javascript

On Github cmstead / functionaljs-presentation

This page intentionally left blank

C, Java, PHP, Python or Ruby

Clojure, F#, Haskell, Lisp or ML

It's aΛ Thing

Really Cool Stuff That Makes Slow Presentation Material

Javascript was meant to be Scheme in a browser

Javascript was invented 3 years after the birth of Haskell

Lambda Calculus, the math behind functional programming, was invented in the 1930's

Lambda Calculus is where "lambdas" get their name

Underscore and Lodash

  • A gentle introduction to functional ideas
  • An object-oriented approach to these ideas
  • Not built for a strong functional programming style
                            
        var myDataSet = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            evens,
            odds;

        evens = _(myDataSet)
                .filter(function(value){
                    return value % 2 === 0;
                })

        odds = _(myDataSet)
               .filter(function(value){
                    return value % 2 === 1;
               })
                            
                        

This needs some help!

  • Arguments in the wrong order
  • Partial application
  • Predicates
  • Function composition
                            
        var myDataSet = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

            filterEvens = j.partial(j.filter, j.isEven),
            filterOdds = j.partial(j.filter, j.compose(j.not, j.isEven)),

            evens = filterEvens(myDataSet),
            odds = filterOdds(myDataSet);
                            
                        

We can do better.

This is a cute way to introduce some fun toys, but we can make this better. Let's simplify.

                        
    var myDataSet = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],

        evens = j.filter(j.isEven, myDataSet),
        odds = j.filter(j.isOdd, myDataSet);
                        
                    

What do you get?

Referential transparency

                            
    var myArray = [1, 2, 3];

    for(var i = 0; i < myArray.length; i++){
        myArray[i] = myArray[i] * 3;
    }
                            
                        
Versus
                            
    var multiplyBy3 = j.partial(j.multiply, 3),
        myArray = j.map(multiplyBy3, [1, 2, 3]);
                            
                        

Declarative instead of imperative programming

                            
    var finalValue = (passedValue !== undefined && passedValue !== null) ?
                     defaultValue : transformation(sanitizedValue);
                            
                        
Versus
                            
    var sanitizedValue = j.maybe(defaultValue, transformation, passedValue);
                            
                        

Stateless Interactions

                            
    //Stateful data management
    myObj.prototype = {
        processObj: function(tempObj){
            this.finalObj.keyname = tempObj.keyName;
        }
    };
                            
                        
Versus
                            
    //Stateless data management
    function processObj(tempObj){
        j.pluck('keyName', tempObj);
    }
                            
                        

Why is this important?

Testability

Maintainability

Asynchronicity and concurrency

References on Functional Programming

Books

  • Functional Javascript -- Michael Fogus
  • Land of Lisp -- Conrad Barski
  • Learn You A Haskell for Great Good! -- Miran Lipovaca
  • An Introduction to Functional Programming Through Lambda Calculus -- Greg Michaelson

On The Web

chrisstead.com/presentations/functionaljs/

@cm_stead

www.chrisstead.com

github.com/cmstead