2 ms·
There are many cases of elegantly using reduce() with a different return type than the list's item type. Here is a JS function which computes the combined leng
by espadrine 12y ago
There are many cases of elegantly using reduce() with a different return type than the list's item type.
Here is a JS function which computes the combined length of all strings in a list:
stringsLen = (strings) => strings.reduce((acc, item) => acc += item.length, 0);
stringsLen(['hello', 'world']) //> 10
Sure, you can argue that this works, but it misses the point.
stringsLen = (strings) => strings.map(s => s.length).reduce((acc, n) => acc += n, 0);
stringsLen(['hello', 'world'])