Skip to content

Add into_scalar method for Array0 - #535

Merged
jturner314 merged 1 commit into
rust-ndarray:masterfrom
jturner314:array0-into-scalar
Nov 14, 2018
Merged

jturner314 merged 1 commit into
rust-ndarray:masterfrom
jturner314:array0-into-scalar

Conversation

@jturner314

@jturner314 jturner314 commented Nov 11, 2018 •

Copy link
Copy Markdown
Member

Other possible names: into_first, into_inner, unwrap.

Edit: I just realized the implementation is broken and I need to fix it. Don't merge this yet!

Edit2: Fixed now! (See the first part of the array0_into_scalar test for the case where my original implementation was broken.)

@jturner314
jturner314 force-pushed the array0-into-scalar branch 2 times, most recently from 356204b to d859202 Compare November 12, 2018 03:58
@LukeMathWalker

Copy link
Copy Markdown
Member

This would avoid using the [()] which I think is quite confusing/difficult to find out. Good addition!

Comment thread src/impl_owned_array.rs
let index = (first - base) / size;
debug_assert_eq!((first - base) % size, 0);
// Remove the element at that index and return it.
self.data.0.remove(index)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gnarly but it works fine. Maybe a comment to explain that the len of the Vec might be bigger than the array's current len

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've added a explanatory comment. If someone changes the implementation in the future, the test will also help catch this edge case.

@jturner314

Copy link
Copy Markdown
Member Author

Do you think it would make sense to add .into_first() for all Arrays (since we have .first() and .first_mut() methods), or should we just have .into_scalar() for Array0?

@jturner314

Copy link
Copy Markdown
Member Author

Never mind. I think .into_scalar() for Array0 makes sense because .into_first() for general Arrays would have to return an Option.

@jturner314
jturner314 merged commit 245cfbb into rust-ndarray:master Nov 14, 2018
@jturner314
jturner314 deleted the array0-into-scalar branch November 14, 2018 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants