-
Notifications
You must be signed in to change notification settings - Fork 314
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
Efficiently map array to a (possibly) different type #1031
Comments
So for this use case, the element type is something with non-trivial Clone or not? Or is it just a Copy element type? |
For my use case pub fn mapv_into<F>(mut self, f: F) -> Self
where
S: DataMut,
F: FnMut(A) -> A,
A: Clone,
// ... |
@bluss, it's been a few days, so just checking again to see if If this just isn't a fit that's totally fine by me! I can focus on integrating this functionality into my own code or a separate, helper crate. |
I'm not currently working on ndarray, so days are currently a too short time scale 🙂 . Last months I did a lot of work on it. The idea makes sense, but I'd probably tweak the signature and name if I had a go at it. About your current implementation I must point out that Fortunately ndarray has just added an actual .into_iter(). |
@bluss, fair enough! As the most active committer I thought maybe you were the official maintainer of ndarray 😅. I guess I'll turn this into a pull request and solicit feedback that way. I'm not sure that I see the error in |
Well you're not wrong, I wrote most of ndarray and am one of the maintainers |
ArrayBase::mapv_into()
consumes self and reuses its memory allocation to compute a new array with the same shape and type. Avoiding unnecessary allocations is important when mapping very large arrays.When writing generic methods, it is often necessary to assume that two arrays may be of different types even though we might expect them to be the same type most of the time. For example, a method that is generic over complex numbers may be called with all real arguments, all complex arguments, or a mixture of real and complex arguments. This precludes direct use of
mapv_into()
since we cannot assume the arguments are the same type.Would it be worth adding something like this
trans_mapv_into()
, which maps an array with elements of one type to a (possibly) different type? If the types are the same it delegates tomapv_into()
, and if the types are different it falls back to using additional allocations. Type comparison is performed at runtime pending specialization.The text was updated successfully, but these errors were encountered: