Concatenating the result of a paginated API.
Showing all the objects two or more 1:N steps away from you in the object graph. The events your friends are attending, the issues your coworkers are working on, the people belonging to any of your same groups. Basically any time you would do a SELECT... JOIN in SQL.
With how many wrappers around paginated APIs to unpaginate them I must be wrong but it still bugs me.
But far more often, the pagination is forced - get 100 results, hit this link for the next 100 - in order to limit the load on the server.
Most trivial scenario, client runs a search that's too generic, you don't want to waste server resources actually preparing 999999 results.
For example: at work we deal with "roles" (professions), and underneath those there can be different specialisms. In some of our views we have filters that users can use to show a subset of the data. There is a filter for roles, and a filter for specialisms. But the specialisms filter should only show options that are relevant given the roles selected in the roles filter. So the code for generating the options to display in the specialisms filter dropdown is something like:
const availableSpecialisms = roles
.filter(role => selectedRoles.includes(role.id))
.flatMap(role => role.specialisms) parents.flatMap(parent => parent.children)It's basically the same as the list monad in haskell, and there are examples here https://en.wikibooks.org/wiki/Haskell/Understanding_monads/L... that can be followed even if you don't know any Haskell.
myFriends: { name: string, friends: friend[] }[]
If I wanted to get a list of friends of friends, I could do something like
myFriends.map(prop('friends')).flatten()
Or
myFriends.flatMap(prop('friends'))
My data is a list of lists but I really want a list of friends so I flatten.
Maybe the Friend object is poorly created/designed but often times you'll just have to deal with whatever the API gives you.
And of course you could call `flatmap` bind, if you prefer ;)
[ { name: "Saransh Kataria" , roles: ['system-admin', 'developer'] }, { name: "Wisdom Geek" , roles: ['basic'] }, ].flatMap(x => x.roles);
// Output => ["admin", "system-admin", "developer"]
Example in the article is meaningless to me:
because the right way would be array.map(x => x *2); anyway.What am I missing? What is a realistic scenario where an array needs to be flattened?