From the lodash documentation for reduce:
_.reduce(collection, [iteratee=_.identity], [accumulator])Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection).
Many lodash methods are guarded to work as iteratees for methods like _.reduce, _.reduceRight, and _.transform.
The guarded methods are: assign, defaults, defaultsDeep, includes, merge, orderBy, and sortBy
I can see how reduce can be applied to most of the other methods, since they roughly follow the method signature. E.g., assign and defaults are of the form (in informal TypeScript-ish pseudocode):
_.assign and _.defaults: (object: object, sources: collection) => object
^ accumulator ^ value ^ same type as accumulator
Similarly, sortBy also makes sense:
_.sortBy: (collection: object, iteratees: sort_fn[]) => object
^ accumulator ^ value ^ same type as accumulator
I can see useful use cases for these, e.g.:
console.log('reduce',
_.reduce([{a: 2, b: 4}, [123, 52], {a: 5}], _.assign),
_.reduce([{a: 2, b: 4}, [123, 52], {a: 5}], _.defaults),
_.reduce([[e => e.a], [e => e.b]], _.sortBy,
[{a: 3, b: 2}, {a: 3, b: 3}, {a: 2, b: 15}, {a: -2, b: 3}])
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
However, I'm not sure how this would be used with orderBy and includes. I.e.,
_.orderBy: (collection: object, iteratees: sort_fn[], orders: ('asc' | 'desc')[]) => object
^ accumulator ^ value ^ index??? ^ same type as accumulator
Looking at the source and following some of the method calls, it seems that this still works, despite the third parameter being the array of 'asc' | 'desc' values (because of the guarding, it treats it specially). Ultimately, with the guarding it seems that it defaults all of the orders to asc, so it acts just like sortBy.
console.log('reduce',
_.reduce([[e => e.a], [e => e.b]], _.orderBy,
[{a: 3, b: 2}, {a: 3, b: 3}, {a: 2, b: 15}, {a: -2, b: 3}])
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
Thus it seems that, if used this way, the guarding is not very useful, and just makes this an alterative to sortBy, effectively rendering the orders parameter useless.
For includes, I'm not even sure how this can be used as a reducer function, because its return type (a boolean) is not the same as its first parameter, e.g.:
_.includes: (collection: object, value: any) => boolean
^ accumulator ^ value ^ not same type as accumulator?
My question is: Is there any way to (usefully) use _.orderBy and _.includes as the _.reduce or _.transform iteratee? And if not, why does the lodash documentation list it there as being a guarded function that can be used this way?
Sorry if this sounds a bit like two questions, but I figured that they were related enough that they belonged together.