20
loading...
This website collects cookies to deliver better user experience
function jumanjiPlayer(playerName){
let position = 0;
return function rollADice(){
position = position + Math.floor(Math.random() * 6) + 1
return `${playerName} is now on position: ${position}`;
}
}
const allanTurn = jumanjiPlayer('Allan');
const peterTurn = jumanjiPlayer('Peter');
const judyTurn = jumanjiPlayer('Judy');
console.log(allanTurn()); // "Allan is now on position: 4"
console.log(peterTurn()); // "Peter is now on position: 4"
console.log(judyTurn()); // "Judy is now on position: 1"
console.log(allanTurn()); // "Allan is now on position: 9"
console.log(peterTurn()); // "Peter is now on position: 7"
console.log(judyTurn()); // "Judy is now on position: 5"
console.log(allanTurn());
console.log(jumanjiPlayer('Allan')());
callbacks
function player(fn, ...args){
let position = 0;
return function (){
position = position + Math.floor(Math.random() * 6) + 1
return fn(...args, position);
}
}
function newJumanjiPlayer(playerName, position) {
return `${playerName} is now on position: ${position}`;
}
const allanTurn = player(newJumanjiPlayer, 'Allan');
const peterTurn = player(newJumanjiPlayer, 'Peter');
const judyTurn = player(newJumanjiPlayer, 'Judy');
console.log(allanTurn()); // "Allan is now on position: 4"
console.log(peterTurn()); // "Peter is now on position: 4"
console.log(judyTurn()); // "Judy is now on position: 1"
console.log(allanTurn()); // "Allan is now on position: 9"
console.log(peterTurn()); // "Peter is now on position: 7"
console.log(judyTurn()); // "Judy is now on position: 5"
newJumanjiPlayer
to hook our codes in a much more friendly manner.function player(fn, ...args){
let position = 0;
return function (...restArgs){
const toAdd = restArgs.length > 0 ? [...restArgs].reduce((a, b) => a + b, 0): Math.floor(Math.random() * 6) + 1;
position = position + toAdd;
return fn(...args, position);
}
}
function newJumanjiPlayer(playerName, position) {
return `${playerName} is now on position: ${position}`;
}
const allanTurn = player(newJumanjiPlayer, 'Allan');
const peterTurn = player(newJumanjiPlayer, 'Peter');
const judyTurn = player(newJumanjiPlayer, 'Judy');
console.log(allanTurn(5,3,2,1)); // "Allan is now on position: 11"
console.log(peterTurn(1)); // "Peter is now on position: 1"
console.log(judyTurn());
console.log(allanTurn());
console.log(peterTurn());
console.log(judyTurn());
const [todos, setTodos] = useState([]);
useEffect(() => {
(async function () {
const response = await fetch(
"https://jsonplaceholder.typicode.com/todos"
);
const list = await response.json();
setTodos(list);
})();
}, []);
const userTodos = (list) => (userId) => {
console.log("this will not run everytime we click the button");
const userTodoList = list.filter((item) => item.userId === userId);
return (completed) => {
console.log("this will run everytime we click the button");
return userTodoList.filter((item) => item.completed === completed);
};
};
const doFilterByStatus = userTodos(todos)(1); // 1 is userId
return (
<div className="App">
<button onClick={() => console.log(doFilterByStatus(false))}>
Filter false
</button>
<button onClick={() => console.log(doFilterByStatus(true))}>
Filter true
</button>
</div>
);
todos
contains hundred of thousands of data, in this curried approach we are getting all of todo
of user 1
only once, and we just filter for the completed
status against those already filtered list. Imagine it on other scenarios like for example data scraping or migration of data, like in e-commerce site, get all products that has a category of FOOD
.myFunc(a,b,c)
to become myfunc(a)(b)(c)
. This makes the code less redundant.