Skip to content

Is it possible to collect a Zip into Array? #755

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

Closed
xiaoniu-578fa6bff964d005 opened this issue Nov 20, 2019 · 2 comments · Fixed by #797
Closed

Is it possible to collect a Zip into Array? #755

xiaoniu-578fa6bff964d005 opened this issue Nov 20, 2019 · 2 comments · Fixed by #797

Comments

@xiaoniu-578fa6bff964d005

Currently if we need to do elementwise operation on array, we can create a result array, and wrap it together with parameters arrays in Zip and use apply to assign the result. However, since Zip have the shape information, can we add a helper function to allocate a result array with correct shape, so that we don't need to create the result array manually?

@bluss
Copy link
Member

bluss commented Nov 20, 2019

Seems like a reasonable feature request long-term.

@bluss
Copy link
Member

bluss commented Apr 13, 2020

This seems to be enough to implement this feature on Zip with some limitations (Copy-only elements because we miss handling of partially filled arrays with elements that are uninit or need drop).

            /// Apply function and collect the results into a new array.
            pub fn apply_collect<R>(self, mut f: impl FnMut($($p::Item,)* ) -> R) -> Array<R, D>
                where R: Copy,
            {
                unsafe {
                    let mut output = Array::uninitialized(self.dimension.clone());
                    self.and(output.raw_view_mut())
                        .apply(move |$($p, )* output_| {
                                std::ptr::write(output_, f($($p ),*));
                        });
                    output
                }
            }

Edit: Should be fixed to at least pick the right layout out of c/f, and more if possible.

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

Successfully merging a pull request may close this issue.

2 participants