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

Chain all iterator in array that isn't determinate in compilation-time #6532

Closed
xqyww123 opened this issue Aug 13, 2018 · 1 comment
Closed

Comments

@xqyww123
Copy link
Contributor

xqyww123 commented Aug 13, 2018

I have an array of iterators depends on user's input, so cannot be determinate in compilation-time, and now I want to chain all of them together.

arrs = [] of Array(Int32)
gets.not_nil!.to_i.times {|a| arrs << [a] }

def chain_all1(arrs)
    arrs.each.map(&.each).reduce{|a,b| a.chain b}
end
def chain_all2(arrs)
    iter = arrs.each
    ret = iter.next
    return if ret.is_a? Iterator::Stop
    ret = ret.each
    until (a = iter.next).is_a? Iterator::Stop
        ret = ret.chain a.each
    end
    ret
end
chain_all1(arrs).each {|a| p a }
chain_all2(arrs).each {|a| p a }

The first one chain_all1 triggers a compilation error :

type must be Indexable::ItemIterator(Array(Int32), Int32), not (Indexable::ItemIterator(Array(Int32), Int32) | Iterator::Chain(Indexable::ItemIterator(Array(Int32), Int32), Indexable::ItemIterator(Array(Int32), Int32), Int32, Int32))

And for the second one chain_all2, compiler traps into a long time silence and ends in generic type too nested.

It's yet another recursive type problem, because Iterator tries to expand all code and it's impossible.

But 'chains all' is in fact practicable, by a struct stores all the iterators and implements next and rewind via a loop.

The code to implement a struct could be long, so I think it's should be implemented in std-lib, and a method chain(iterators : Enumerable(Iterator)) may be provided.

@xqyww123
Copy link
Contributor Author

xqyww123 commented Aug 13, 2018

I just implemented a version with simple spec. here

xqyww123 added a commit to xqyww123/crystal that referenced this issue Aug 20, 2018
RX14 pushed a commit that referenced this issue Sep 10, 2018
* Iterator could chain indetermined number of iterators now

Fixes #6532

* Formatted the code

* Simplified the spec & Improved comments.

Finally, I remain `#chain` in the same, for the purpose of performance, but added some comments to instruct this feature.
Maybe I had tried my best to improve linguistics, but I'm sorry I'm not good at foreign languages. If it's still not good enough, helps is really really appreciated, thanks.

* Improved comment linguistic, agian...

* Iterator could chain iterators of different type

* Type of ChainAll could be inferred by compiler
ezrast pushed a commit to ezrast/crystal that referenced this issue Oct 2, 2018
…ng#6570)

* Iterator could chain indetermined number of iterators now

Fixes crystal-lang#6532

* Formatted the code

* Simplified the spec & Improved comments.

Finally, I remain `#chain` in the same, for the purpose of performance, but added some comments to instruct this feature.
Maybe I had tried my best to improve linguistics, but I'm sorry I'm not good at foreign languages. If it's still not good enough, helps is really really appreciated, thanks.

* Improved comment linguistic, agian...

* Iterator could chain iterators of different type

* Type of ChainAll could be inferred by compiler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants