Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support single argument to zip #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Support single argument to zip #17

wants to merge 2 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Jun 28, 2019

Zip currently requires at least two arguments.

zip can be seen as "create tuples consisting of an element of every argument". So, if one argument is passed, zip can still operate by creating tuples of only one element.

This can be very handy in situations where the splat operator is used:

$itemsDynamicallyGenerated1 = [[1, 2]];
$itemsDynamicallyGenerated2 = [[1, 2], [3, 4]];

assert(zip(...$itemsDynamicallyGenerated1) === [[1], [2]]);
assert(zip(...$itemsDynamicallyGenerated2) === [[1, 3], [2, 4]]);

A real world use case is having some dynamically sized list of lists, where these nested lists need to be zipped together, after which you want to calculate the total (sum) contained in each element:

assert(map(partial(rpartial(reduce, 0), sum), zip(...[[1, 2]])) === [1, 2]);
assert(map(partial(rpartial(reduce, 0), sum), zip(...[[1, 2], [3, 4]])) === [4, 6]);

Currently this fails if your list happens to be only one element. By allowing this, you can still safely map and reduce your list, as you will still have tuples contained in them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

0 participants