Functional Programming - alexanderteplov/computer-science GitHub Wiki
is a property of an expression to have the same output with an equal input. This term is applicable, e.g., to functions or REST API methods. Notice, that idempotent function still can have side effects.
Side effects are any effects produced during an execution of some function/method which is not reflected in its output and usually not its direct goal. Examples: modifying a value of a global variable, any other I/O (screen, file, etc.), calling other side-effect functions.
Pure functions are idempotent functions without side effects. The purity simplifies a testing process, allows chaining of function calls, allows cashing because of referential transparency.
is a property of an expression to stay the same after replacing it with its corresponding value (and vice-versa). Pure functions are always referentially transparent, and we can easily replace them with their output.
Immutability in OOP and FP is a property of an object state being unable to change.
The state of an object consists of two parts: its fields (shape/type) and its values (appearance/instance). If an object's shape cannot be changed but its appearance can - we call this weak immutability. In JavaScript, objects are high mutable by design. We can alter their shape as well as their appearance.
In contrast, strong immutability means the impossibility of changing anything about an object's state, neither fields nor values. In JavaScript, under the hood, e.g., strings are strongly immutable (but in the language user's point of view, all the primitives in JS are weakly immutable till you do not force them to strong immutability with the const keyword).
First-class entities refer to first-class citizens. So functions may be treated as any other regular programming objects: may be stored in variables, passed as an argument to a function, returned from a function.
Functions which takes another function(s) as an argument, return function(s), or do both.
The process of repeatedly calling the function itself is called recursion and such a function is called recursive.
is about a recursive function which calls itself in exactly the last step of executing.
+ If it's pure, intermediate results can be cached.
+ Tailed recursive function can be transformed to cycle by compiler (in some languages, including JavaScript).
+ Easier to read solution of problem.
- Possible performance issues (each step allocates the whole function environment in memory again).
- Limited in calls number.
is a process of representation of a function of multiple arguments as a chain of functions of one argument.
f(x,y) -> g(x)(y)
is a process of creating a function from a curried function by passing an argument to it and storing the result in a variable.
h(x) = g(x)(a)
is a mechanism for combining functions, in which the output of each function is passed into the next one, and the output of the last function is the final result.
Sometimes it's hard to read multiply nested functions in a composition, so it's a bit more clear with the helpers like compose.
A useful method for function composition in JavaScript is reduceRight.