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

Add Indexable #3087

Merged
merged 1 commit into from
Aug 4, 2016
Merged

Add Indexable #3087

merged 1 commit into from
Aug 4, 2016

Conversation

asterite
Copy link
Member

@asterite asterite commented Aug 3, 2016

This was proposed many times in the past and I think it's time to implement this.

All started with me trying to implement the sieve exercise on exercism. I came up with a solution very similar to that of @jhass ( http://exercism.io/submissions/dd85bc5d9c6947dd98fcc807db62afe8 ), using Array, and then wanted to use BitArray only to find out it didn't have an each method. So I thought about adding it, but it was very similar to that of Array. And that of Slice, and Deque. So I extracted all those iterators into a separate class, but then thought many other methods were similar. Then I remembered about the idea of adding an Indexable module so all implementations can be in sync, and also to avoid a lot of boilerplate in every implementation.

Indexable needs a protected unsafe_at(index) to be implemented. This allows for some methods to be implemented much more efficiently (I did some benchmarks), to avoid some index bounds check when it's safe to avoid them.

I found a couple of compiler bugs: if you include a generic module that includes another generic module, that second generic module isn't seen in restrictions. Then protected methods aren't correctly considered in generic modules so for now I made the unsafe_at methods public. But we can change these when we fix the compiler bugs.

One drawback of this change is that now some Array methods are not immediately seen in the Array docs, but you need to jump to Indexable, so newcomers might be confused with this.

Indexable only provides read access, not write access. I didn't find the write methods in Array, Slice, StaticArray and Deque to be similar, and there's also the thing that Tuple is read-only.

@jhass
Copy link
Member

jhass commented Aug 3, 2016

Big 👍, I'm just not sure about the name, it makes me think of memory, probably because RAM stands for random access memory. What about my initial idea of Indexable? I think that fits well with Enumerable.

@asterite
Copy link
Member Author

asterite commented Aug 3, 2016

Ah, sounds good. Then it's also maybe more clear that you can index into it, but not necessarily mutate it.

@asterite asterite force-pushed the feature/random_access branch from 8839579 to 63b65cd Compare August 3, 2016 18:25
@asterite asterite changed the title Add RandomAccess Add Indexable Aug 3, 2016
@bcardiff
Copy link
Member

bcardiff commented Aug 3, 2016

I don't think the documentation issue you mention is a stopper. At most, is something might push the docs to handle better the inherited methods due to modules.

@RX14
Copy link
Member

RX14 commented Aug 3, 2016

Should Indexable(T) always include Enumerable(T)? I don't see a case where you would want one without another.

@@ -0,0 +1,469 @@
# A container that allows accessing elements via a numeric index.
Copy link
Member

Choose a reason for hiding this comment

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

It seems fair to asume index are 0..size-1 and that all indices should return a value without raising.

It should be in the docs for clarification I think

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea. I added the docs for this in the module, and in the unsafe_at method

@asterite asterite force-pushed the feature/random_access branch 2 times, most recently from 78f7777 to 6df4b09 Compare August 3, 2016 18:51
@asterite
Copy link
Member Author

asterite commented Aug 3, 2016

@RX14 In the next release I'll change Indexable to include Enumerable, right now there's a bug that prevents us from doing that, but it seems to be fixed in master. There's a comment in Indexable about this (the include line is commented)

@asterite asterite force-pushed the feature/random_access branch from 6df4b09 to af42af8 Compare August 3, 2016 20:17
@asterite asterite merged commit ed0e47b into master Aug 4, 2016
@ozra
Copy link
Contributor

ozra commented Aug 10, 2016

I can't see why unsafe_at shouldn't be public at all times. The unsafety of using it is pretty apparent from the name :-)

@asterite
Copy link
Member Author

@ozra Yes, definitely. The unsafe part already tells you that it is unsafe, and for optimizing some bottlenecks it can be useful to avoid a bounds check if you know you will always index in bounds.

@asterite asterite deleted the feature/random_access branch September 9, 2016 14:53
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.

None yet

5 participants