-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enlightenment on Higher-Order Functions (components) #40
Comments
@pankajpatel great question! thank you for asking it
Pure FunctionsAre functions that have "no side effects", meaning they don't change anything they aren't supposed to, they just do what they are told; for a given input they always have the same output. This is "impure" because it "mutates" i.e. changes the // this is an "impure" function that "mutates" state
var counter = 0;
function increment () {
return ++counter;
}
console.log(increment()); // 1
console.log(increment()); // 2
console.log(increment()); // 3 whereas // this is an "pure" function which "transforms" data that you input as an argument
// and thus given the same input *always* has the same output
var counter = 0;
function increment (my_counter) {
return my_counter + 1;
}
// because counter is not being "mutated" the output of the pure function is always the same
console.log(increment(counter)); // 1
console.log(increment(counter)); // 1
console.log(increment(counter)); // 1
// you can "feed" the output of one pure function into another to get the same result:
console.log(increment(increment(increment(counter)))); // 3 see: https://repl.it/FIpV Pure functions doe not mutate a "global" state and predictable and thus easy to test. Higher Order FunctionsHigher order functions are functions that "consume" other functions and "apply" them to other arguments you pass in ... this is useful for "composition", i.e. building a "pipeline" of function calls to compose more sophisticated functionality. // perform an action twice
function twice (func, value) {
return func(func(value));
}
// func can be any simple (preferably pure) function:
function func (value) {
return value + 3;
}
console.log(twice(func, 1)); // 7
console.log(twice(func, 7)); // 13 see: https://repl.it/FIps/1 Which can be re-written in "ES6" using "arrow functions": const twice = (func, value) => func(func(value));
const func = value => value + 3;
console.log(twice(func, 1)); // 7
console.log(twice(func, 7)); // 13 I probably haven't explained this very well, maybe one of the others can do a bettererer job. 🤔 Further reading:
React Component ContextReact Components follow the same principal they should not "Mutate State" which you store in the Redux Store. The Redux "reducer" should pass in the state and an "action" such that for a given input state and action the output is always the same.
|
nice explanation @nelsonic; this explains many terms. |
After going through #16; I got into confusion with Higher-Order Functions.
Are Pure Functions and Higher-Order alike? Or they differ?
And Higher-order Components are created by Higher-order functions?
The text was updated successfully, but these errors were encountered: