Please enable JavaScript.
Coggle requires JavaScript to display documents.
INTRO TO FUNCTIONAL PROGRAMMING (CONCEPTS (Purity (at least one…
INTRO TO
FUNCTIONAL
PROGRAMMING
CONCEPTS
Purity
at least one parameter
At least one return
Same output for same properties
No side-effect to other functions
Immutability
No variable
Just constants with the same value for their lifetime
locally stored values (in function lifespan)
No loops
Making copies of values
RECURSION
instead of loops.
No loop construct but recursion possible
No change to state if variables have the same value for ever
Refactoring
If 2 functions do the same thing to different types
create a single function and parametize the things that we need edited
Having 1 function is better than a lot
Easier to fix
Easier to maintain
Higher order function
Function with another function as a parameter
First class citizen = just another value
Factor your variables as much as possible
takes or return a function or both
if the function is returned, it is tabulated in the same function
Uses closure
the last tabulated function (child) has access to all variables from higher function (parent) but not the opposite.
Closure
=scope that remains alive by a reference to that function
2.FUNCTION COMPOSITION
function are the building block
Currying
Give one parameter ahead of time
Takes one parameter at a time
EX: var add = x => y => x + y
EX: var compose = (f, g) => x => f(g(x));
var mult5AfterAdd10 = compose(mult5, add(10));
var mult5AfterAdd10 = compose(mult5, add(10))
= call var compose that adds 10 to nominal value then multiplies it by 5
(f, g) => x => f(g(x))
= parameter f and g add x to parameters then g multiplies x then f multiplies gx
var compose
= variable called which is also a function
=> instead of normal function
EX: var add10 = value => value + 10;
value
= parameter
value + 10
= operation
var
= normal variable
f ∘ g
call F after G = f(g(x))
call the last thing you want done first with parentheses