Hardcore Functional Programming in JavaScript

Course Description: 

Demo Code: https://github.com/begriffs/immutube

Slides: https://goo.gl/photos/jnsbe2XPtjewwXxPA

Notes:

  • The core idea of Functional Programming is to write Pure functions that Work on Containers and return Containers so that they can be chained
  • Map is generic term for applying the Function over the value inside the Container(object or array) and not on the Container itself.
  • A Container is simply a Object style wrapper around a Primitive Value or Array or Object so that same functions can be applied to all 3. Eg Container(3) returns {val: 3}
  • Functors
    • Functors are Data Structures that can be Mapped over.
    • In other words, Functors are wrappers that allow a function (Eg. Map, Reduce or custom function) to be run over the value contained inside the functor wheter that value is primitive or object or array.
    • A Container is a Functor.
    • Elevate Values to Functors so that actions on them can be composed
  • Every function should be a set i.e. single valued collection of pairs (of I/P aka Domain and O/P ie Range)
  • Data (Arguments) needs to be separate from the Logic (function) and should be partially applicable over the logic. In other words, every function should return curried function (write curry helper or use lodash/ramda utility) if it’s not given all it’s arguments.
  • Use map, reduce and filter to replace Loops
  • Use Compose and to orchestrate composition:
    • Compose Does this: compose(f,p,qx){return f(p(x),q(x))}
    • Compose without using Glue Variables (This is called Pointfree Style: It is very common for functional programmers to write functions as a composition of other functions, never mentioning the actual arguments they will be applied to.) Sample Lib
    • Eg. avg = compose(_.divide, _.sum,_.size)
  • Use Fork for fork(p,q,x){return p(q(x))}
  • Maybe
    • It’s a Functor (container) that is null safe, ie it null checks that values that it wraps
  • Either
    • Functor used for Error Handling
    • It Maps the function over a Right and ignores the Left (Error Condition)
  • RAMDA IS THE BEST LIBRARY FOR JS FUNCTIONAL PROGRAMMING…
  • https://github.com/fantasyland/fantasy-land#sanctuary-types-return
  • http://ramdajs.com/docs
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s