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

RFC: Support getindex for Repeated and Take iterators #15988

Closed
wants to merge 1 commit into from

Conversation

nalimilan
Copy link
Member

@nalimilan nalimilan commented Apr 21, 2016

This allows using repeated() to create a vector-like object.

The goal of this PR is to allow using repeated(1, N) as a more general solution to the idea of a Ones type (see JuliaStats/StatsBase.jl#135). The idea is to always write algorithms in their most general form accepting numeric weights, and make the non-weighted form use a special object returning only unit weights.

If I'm correct, with proper inlining, repeated(1, N) will/can be as efficient as as Ones type for which the return value is known at compile time. Though even if that doesn't work, this PR makes sense for other use cases.

I've marked it as RFC because I wonder whether it wouldn't be better to make repeated(::Any, ::Integer) return a new NRepeated type which would be a subtype of AbstractVector. Indeed, even if I added getindex methods, Take{Repeated}} objects can't be passed to functions expecting a vector, which could be annoying. Comments welcome!

This allows using repeat() to create a vector-like object.
@nalimilan nalimilan changed the title RFC: Support getindex for Repeat and Take iterators RFC: Support getindex for Repeated and Take iterators Apr 21, 2016
@mschauer
Copy link
Contributor

I think that use case is too much "ad hoc" to give getindex methods to some iterators.
In any case a shape-less iterator which is not an array is a poor substitute for ones and using an IteratorND of a Take of a Repeated is then very indirect approach...

Currently the separation, while not perfect, is that something which implements getindex-methods fits well into the Array-tree (and then of course also can implement the iterator protocol) but the iterator types themselves have no getindex methods, not even the IteratorND

@nalimilan
Copy link
Member Author

I agree that this PR isn't very systematic. OTOH Repeated is special in that indexing is always possible, which isn't the case for other iterators. But maybe iterators should remain distinct from AbstractArrays, and another type should be used (in Base or elsewhere) to represent arrays with a single value. I find it funny that we have types to represent ranges, but nothing to represent repeated values (which is a simpler case).

Feel free to close if you think that's the wrong approach.

@mschauer
Copy link
Contributor

I cannot close of course, I am not a julia member, but we can just wait for the next person to do the synthesis.

@nalimilan nalimilan deleted the nl/repeated branch June 27, 2019 13:47
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.

3 participants