Closed LukeDefeo closed 4 years ago
Hey @LukeDefeo good question. Shades is broken into two pieces, the lens portion and the utilities for collections (map, filter, blah blah blah).
My design philosophy for the latter has been that shade's shouldn't replace your favorite collection processing library (Ramda, lodash/fp), but augment it. To this end, I only include a function if I think I can offer a variant that offers something new, which it typically does in two ways:
Most collections libraries that offer a map function offer it as a function that takes a List<A>
and produces a List<B>
. Shades however allows you to use the map
function (or filter
, find
, etc.) to take in any collection type, and get that same collection type back. So if I map an object I get back and object, if I map a Map I get back a Map. Most importantly, it does this in a way that TypeScript can understand.
Most collections libraries (with the notable exception of Lodash) only work with functions, so you end up with noisy functions like:
R.groupBy(x => x.age)
shades' tries to accept a rich language for specifying an operation (strings, objects, functions), so that you can write things like:
const beatles = [{name: 'John', hits: 10}, {name: 'Paul', hits: 12}, {name: 'George', hits: 6}, {name: 'Ringo', hits: 1}]
R.pipe(
beatles,
filter({hits: greaterThan(9))
map('name')
)
// ['John', 'Paul']
Again, it does this in a way that TypeScript can understand.
groupBy
is pretty well covered by Ramda and lodash/fp. From an interface perspective, it only really makes sense for groupBy
to take a collection and return an object, so there's no benefit to shades' polymorphic signatures. It might be nice to have the shorthand syntax for the function argument (i.e. to say:
groupBy('age')
), but shades exports into
so you could always just do:
R.groupBy(into('age'))
TL;DR: Shades doesn't have anything to add to the groupBy
function. Just pick your favorite from Ramda or lodash and use that!
Hey im new to the library so apologies if this question is dumb or if this isn't the correct place.
Is it possible to do a group by like operation with shades. E.g in remeda you have