Question: I want to implement javascript function like for-loop like construct, for_, using first-class functions. for_ takes 4 arguments: - cur: the initial current value -
I want to implement javascript function like for-loop like construct, for_, using first-class functions.
for_ takes 4 arguments:
- cur: the initial current value
- cond: the function that is used to decide whether or not we should execute the "loop" body. The cond function expects an argument whose type is the same as that of cur; the cond function is expected to return a boolean.
- next: the function that is used to compute the "next" current value. Like cond, next expects an argument whose type is the same as that of cur; the next function is expected to return a value of the same type.
- fbody: the function corresponding to the loop body. This function is called with the cur value as an argument.
EXAMPLE OF HOW TO USE for_ FUNCTION:
for_(0, i => i < 10, i => i+1, i => {
console.log(`yay: ${i}`);
});
// is computationally equivalent to:
for(let i = 0; i < 10; i++) {
console.log(`yay: ${i}`);
}
______________________________________________________________________________________________________________________
function for_(cur, cond, next, fbody) {
/**
/** **/
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
