p-iteration icon indicating copy to clipboard operation
p-iteration copied to clipboard

Support for other types that implement Symbol.iterator

Open RaederDev opened this issue 6 years ago • 3 comments

Hi!

Would be a super awesome feature if the library would also support types other than plain Arrays that implement Symbol.iterator. If the library would use for ... of internally this could be fairly easy to implement for most methods.

RaederDev avatar Mar 07 '18 12:03 RaederDev

Thanks for the issue!

Yeah being able to use other JS iterable objects should be nice.

At the beginning I just wanted to keep it simple, as a replacement for ES5 Array methods. Other reason I kept it this way is that some of p-iteration methods maybe doesn't make sense for other iterable objects apart from Array.

What do you think about it? Does it make sense to you, for example, using map() with a Set?

Releasing a version 2.0.0 changing the API is also an option I've been thinking about.

toniov avatar Mar 08 '18 10:03 toniov

I think most methods would make sense for most supported data-types. You could have a Set of userIds and want to map them to a Set of Users for example.

await map(new Set([1, 2]), async uid => await fetchUser(uid));

I'm sure there are some cases where it doesn't make sense at all, but people often have strange use-cases. Especially map and forEach make sense for almost everything.

The reason I opened the issue is because I wanted to use the library to iterate over a Map like this:

const orders = new Map();
orders.set('o1', {superSecretData: 42});
orders.set('o2', {superSecretData: 42});

await forEach(orders, async ([,val]) => await sendToServer(val));

If you think about releasing a version 2.0, maybe it would also be a nice idea to switch the order of function and data to data last like lodash/fp does it. That way it would be very easy to partially apply a function for later use.

const logToServer = forEach.bind(null, async data => {
    await Promise.resolve('Server API dummy function ' + data);
});

logToServer([1, 2]);
logToServer([3, 4]);

RaederDev avatar Mar 08 '18 17:03 RaederDev

Thanks for your opinion and sorry for the super late reply.

I think that definitely I'll make it compatible with the rest of types.

If you think about releasing a version 2.0, maybe it would also be a nice idea to switch the order of function and data to data last like lodash/fp does it. That way it would be very easy to partially apply a function for later use.

This looks good but I would add it as an option (similar to lodash/fp), because I think that that order would be unfamiliar to most of the people.

toniov avatar May 19 '18 15:05 toniov