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

Implement zipWith3 #163

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

Implement zipWith3 #163

wants to merge 3 commits into from

Conversation

TOTBWF
Copy link

@TOTBWF TOTBWF commented Jan 13, 2020

No description provided.

@hdgarrood
Copy link
Contributor

I'm not super keen on this actually: if we add zipWith3, then someone else will probably come along soon and ask for zipWith4, and so on. Is it not currently possible to implement this function with decent performance without increasing the API surface of this library?

@natefaubion
Copy link

You can chain zipWith calls (it's apply for some hypothetical ZipArray), however you must traverse the Array for each call. It's likely possible to fuse this though with a new type.

@natefaubion
Copy link

@hdgarrood Would you accept a PR for a generalized, fused ZipArray?
@TOTBWF Would you be willing to try and implement this? I'm happy to help.

@hdgarrood
Copy link
Contributor

Yes, that sounds great 👍

@TOTBWF
Copy link
Author

TOTBWF commented Jan 16, 2020

Yeah, that sounds like a much better solution. I'll hopefully have something by the end of day.

@ajnsit
Copy link

ajnsit commented Dec 10, 2021

@natefaubion I took a shot at implementing this and quickly realised that a law abiding pure is impossible with strict arrays. If the result of pure is of a finite size N then it will fail the identity law

pure identity <*> arr ≠ arr

for any arr with length > N.

Do you have any suggestions for tackling that?

I came up with slightly different semantics for zip that seems to make it possible to have both applicative and monad instances, and wrote up a sample implementation. This version of zip doesn't drop elements when one of the arrays is bigger than the other. Instead, it extends the shorter array by repeating its last element.

As far as I can tell, this satisfies the applicative and monad laws, and is still useful. For example, you can zip together arrays of the same size and get results identical to regular zip. There are other constructions that I considered, but this one has the advantage of being a newtype over Arrays.

Thoughts?

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.

4 participants