Lazy Chain
Description:
Method chaining is a useful technique in JavaScript, allowing better composability and readability of functions.
One problem with standard JavaScript chaining is that chained methods are immediately executed upon creation.
let a = [ 1, 2, 3].map(x => x * 2).reverse();
// a = [ 6, 4, 2]
The problem with this is that it opens values up to possible mutation before being called.
a.join("");
// a = "642"
function doSomething(array) {
// Does something to an array
}
doSomething(a); // TypeError - a is not an array
To combat this we can create a function lazyChain that accepts an argument and allows chaining, but delays execution to when the value method is called.
let a = lazyChain([1,2,3])
.invoke('map', x => x * 2)
.invoke('reverse');
console.log(a.value());
// [ 6, 4, 2]
a.join("");
// TypeError
doSomething(a.value());
// valid function call
Your task is to create the function lazyChain which accepts any value, and allows method chaining through the use of invoke, and execution with the value method.
Keep in mind that invoke is general enough to accept any prototype methods from the standard language.
Similar Kata:
Stats:
Created | Nov 21, 2016 |
Published | Nov 21, 2016 |
Warriors Trained | 555 |
Total Skips | 13 |
Total Code Submissions | 1900 |
Total Times Completed | 257 |
JavaScript Completions | 257 |
Total Stars | 25 |
% of votes with a positive feedback rating | 92% of 72 |
Total "Very Satisfied" Votes | 64 |
Total "Somewhat Satisfied" Votes | 5 |
Total "Not Satisfied" Votes | 3 |
Total Rank Assessments | 11 |
Average Assessed Rank | 5 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 6 kyu |