Monday, September 19, 2016

functional programming concepts

A functor is nothing more than a data structure you can map functions over with the purpose of lifting values from a container, modifying them, and then putting them back into a new container.

Provides the abstract interface for monadic operations

  • Type constructor—Creates monadic types (similar to the Wrapper constructor).
  • Unit function—Inserts a value of a certain type into a monadic structure (similar to the wrap and empty functions you saw earlier). When implemented in the monad, though, this function is called of.
  • Bind function—Chains operations together
  • Join operation—Flattens layers of monadic structures into one. This is especially important when you’re composing multiple monad-returning functions.
Maybe and Either are monads.

Functional programming advocates:
  • pure function 
    • Pure functions return a value computed using only the inputs passed to it. Outside variables and global states may not be used and there may be no side effects. In other words, it must not mutate the variables passed to it for input. Therefore, pure functions are only used for their returned value.
  • immutable data

If a function consistently yields the same result on the same input, it’s said to be referentially transparent. Referential transparency is a more formal way of defining a pure function.

High-order functions
High-order functions are the functions accept a function as the parameter and can return a function as return object.

Functions are first-class in javascript, they can be declared directly, without class. In java,  a class have to be declared before a function can be declared.  Class is the first-class in java.

lenses in FP is a way to manage object changes immutably using a functional approach.
Lenses, also known as functional references, are functional programming’s solution to accessing and immutably manipulating attributes of stateful data types.
copy-on-write strategy
Ramda lens is one implementation.

FP Benefit
A functional flow gives you a clear picture as to the purpose of the program without revealing any of its internal details.
Function Programming let you focusing on business logic instead of control flow.
Function programming is good for testing

  • pure function does not depend on global state and mutations, which makes testing easy. (isolation, predictability, and repeatability)
    • unit test shall get same result, no matter how many times it is executed. Depending on global state cannot guarantee this.
    • change unit test order shall not change the result, if depending on global, changing order may get different result.
  • unit tests are designed to test the small functions.

Map, Filter, Reduce method
Map method - you can also think of map method as a gate that allows you to plug in a lambda expression with specific behaviour that transforms an encapsulated value.

FP error handling
The structured mechanism of throwing and catching exceptions in imperative JavaScript code has many drawbacks and is incompatible with the functional design.
Functions that throw exceptions

  • Can’t be composed or chained like other functional artifacts.
  • Violate the principle of referential transparency that advocates a single, predictable value, because throwing exceptions constitutes another exit path from your function calls.
  • Cause side effects to occur because an unanticipated unwinding of the stack impacts the entire system beyond the function call.
  • Violate the principle of non-locality because the code used to recover from the error is distanced from the originating function call. When an error is thrown, a function leaves the local stack and environment
  • Put a great deal of responsibility on the caller to declare matching catch blocks to manage specific exceptions instead of just worrying about a function’s single return value.
  • Are hard to use when multiple error conditions create nested levels of exception handling blocks:

No comments:

Post a Comment