Continued from Part 1.
3. Few data structures, many operations
- Control flow
- “On the other hand, declarative programs, specifically functional ones, raise the level of abstraction by using a minimally structured flow made up of independent black- box operations that connect in a simple topology.Method chaining”
- Separate control flow from computational logic, i.e. branches and iterations reduced or eliminated in favor of high-level abstractions
- Method Chaining
- OOP pattern where multiple methods are called in a single statement
- _Dash (Lodash): functional utility library
- Lazy evaluation of chainable functions
- “Point-free” style
- Mixins: https://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript- mixins/
- Parsing data structures is the “bread and butter” of FP
4. Toward modular, reusable code
- Chaining vs. Pipelines
- Chaining makes tight connections via object’s methods
- Pipeline links inputs and outputs of functions, requires compatibility in arity and type
- Tuples: Advantages of using to pass between functions
- Immutable
- Avoid creating ad hoc types
- Avoid creating mixed type arrays
- Currying
- Abstracts arity
- Encourages modularity and reuse
- Ramda.js’ R.curry
- Partial application
- Parameter binding
- Point-free programming
- Points = arguments
- Using composition or piping
- Functional Combinators
- higher-order functions that combine functions (e.g. other combinators)
- behave as control logic/orchestrate flow
- typically don’t declare an variables of their own or contain business logic
- Common combinators:
- Identity: returns value it was provided
- Tap (K-Combinator): receives input and fn that acts on input, returns unaltered input
- Alternation (OR-Combinator): (fn1, fn2) => fn1(val) || fn2(val)
- Sequence (S-Combinator): runs a sequence of fns on an input; does not return a value
- Fork (Join Combinator): processes 1 input in two different ways and combine the results with a join