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

functional JS

Avatar for wearefractal wearefractal
June 28, 2012
170

functional JS

Avatar for wearefractal

wearefractal

June 28, 2012
Tweet

Transcript

  1. Functional Programming • Based in math: optimally construct pure functions

    with no side effects • Functions as _primary_ abstraction vs. objects • Focus on iterating and applying functions to data structures in powerful ways • Can Still use objects and concepts from OOP
  2. Purity • A “pure function” is a function that takes

    an input and returns a value: f(x) = x + 1 where f(3) returns 4 • A pure function is said to be “referentially transparent”: given the same value, it will always return the same computed value: f(3) = 4 will always be true • A “pure” Functional Programming language
  3. Math • Algebra: f(x) = x + 1 f(3) =

    4 • CoffeeScript: f = (x) -> x + 1 f(3) # => 4
  4. Mutability • Mutable: can change • Immutable: cannot change, a

    new version must be reconstructed • Using Immutable structures allows for massive concurrency and parallelization
  5. Purity has it’s regrets • In basic math, when you

    define a function there is no notion of a persistent “world” outside of it • Whenever we modify program state or talk to something outside of a program this is called a Side Effect
  6. Functional benefits • Enables more powerful patterns, and less of

    them • Not as Easy, but it is Simpler • More appropriate resolution at which code is reusable (a function) • Functions and collections of functions are composable and chainable, like a circuit or protocol layer • Advanced data structures and iteration strategies • Map/Reduce type operations cover a wide range of use-cases