feat(corelib): core::iter::chain#7064
Conversation
orizi
left a comment
There was a problem hiding this comment.
Reviewed 1 of 5 files at r1, all commit messages.
Reviewable status: 1 of 5 files reviewed, 3 unresolved discussions (waiting on @hudem1)
corelib/src/test/iter_test.cairo line 4 at r1 (raw file):
#[test] fn test_iterator_chain() {
add test chaining different types.
corelib/src/iter/adapters/chain_adapter.cairo line 40 at r1 (raw file):
/// assert_eq!(iter.next(), None); /// ``` pub fn chain<
shouldn't this be a method?
corelib/src/iter/adapters/chain_adapter.cairo line 44 at r1 (raw file):
B, impl IntoIterA: IntoIterator<A>, impl IntoIterB: IntoIterator<B>[IntoIter: IntoIterA::IntoIter],
this seems to test the Iterators are of the same type - not their Items.
Code quote:
impl IntoIterA: IntoIterator<A>,
impl IntoIterB: IntoIterator<B>[IntoIter: IntoIterA::IntoIter],
orizi
left a comment
There was a problem hiding this comment.
Reviewed all commit messages.
Reviewable status: 1 of 11 files reviewed, 4 unresolved discussions (waiting on @hudem1)
a discussion (no related file):
definitely do not edit the IntoIterator trait for this support.
orizi
left a comment
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 5 unresolved discussions (waiting on @hudem1)
corelib/src/iter/adapters/chain_adapter.cairo line 49 at r2 (raw file):
B, impl IntoIterA: IntoIterator<A>, impl IntoIterB: IntoIterator<B>[Item: IntoIterA::Item],
Suggestion:
impl IntoIterA: IntoIterator<A>,
impl IntoIterB: IntoIterator<B>,
+TypeEqual<IntoIterA::IntoIter::Item, IntoIterB::IntoIter::Item>
Previously, Orizi wrote...
Oh okay! I tried to do like Rust and thought IntoIterator trait might not have been fully finished. Sorry about that, I will revert the commit related to that and thank you for your suggestion after! I wasn't aware of this syntax. |
Previously, orizi wrote...
Done. |
Previously, orizi wrote...
I tried to do like the PR core::iter::zip #7050. In this PR, the zip is not created as a method to |
orizi
left a comment
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 5 unresolved discussions (waiting on @hudem1)
corelib/src/iter/adapters/chain_adapter.cairo line 40 at r1 (raw file):
Previously, hudem1 wrote…
Previously, orizi wrote...
/// assert_eq!(iter.next(), None); /// ``` pub fn chain<shouldn't this be a method?
I tried to do like the PR core::iter::zip #7050. In this PR, the zip is not created as a method to
Iterator<T>. Am I missing something maybe ? Should I then go and add the methodchaintoIterator<T>?
i guess it should be the same answer for both.
the question is - why is it that way in rust, and should we keep the same - or do we have specific reasons to diverge.
This reverts commit 3353f49.
|
Previously, orizi wrote…
So I think |
julio4
left a comment
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 5 unresolved discussions (waiting on @hudem1 and @orizi)
corelib/src/iter/adapters/chain_adapter.cairo line 49 at r2 (raw file):
B, impl IntoIterA: IntoIterator<A>, impl IntoIterB: IntoIterator<B>[Item: IntoIterA::Item],
I believe we can't access directly the associated item without the trait impl, but something like this can work:
+TypeEqual<IntoIterA::Iterator::<IntoIterA::IntoIter>::Item, IntoIterB::Iterator::<IntoIterB::IntoIter>::Item>I tried to implement chain as well before but I was stuck on this, I was not aware of this TypeEqual syntax, good to know!
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 5 unresolved discussions (waiting on @julio4 and @orizi)
corelib/src/iter/adapters/chain_adapter.cairo line 49 at r2 (raw file):
Previously, julio4 (Julio) wrote…
I believe we can't access directly the associated item without the trait impl, but something like this can work:
+TypeEqual<IntoIterA::Iterator::<IntoIterA::IntoIter>::Item, IntoIterB::Iterator::<IntoIterB::IntoIter>::Item>I tried to implement
chainas well before but I was stuck on this, I was not aware of this TypeEqual syntax, good to know!
Yes indeed, I tried orizi's suggestion and there was a slight Path problem.
Thank you for your suggestion @julio4! I tried and believe we can omit ::<IntoIterA::IntoIter> and ::<IntoIterB::IntoIter>, and using the code below seems to work:
Code snippet:
+TypeEqual<IntoIterA::Iterator::Item, IntoIterB::Iterator::Item>
orizi
left a comment
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 4 unresolved discussions (waiting on @hudem1 and @julio4)
corelib/src/iter/adapters/chain_adapter.cairo line 40 at r1 (raw file):
Previously, julio4 (Julio) wrote…
So
iter::adaptersby definition takes iterator(s) to return a new iterator.
For most cases when it performs a transformation over a single iterator it makes sense to only have a method, i.emap:iter.map();
But when it concerns multiple iterators we can optionally export the adapters as a function as well, as the syntax is a bit more readable, i.e.zip: considerzip(iterA, iterB)vsiterA.zip(iterB). The first one is clearly understandable for example in for loops, the later one should still be possible for chaining iterator adapters (iterA.map().zip(iterB))I think
chainshould be both a function and method of Iterator
i think it shouldn't be a function, in zip as well - as a place holder for when we have proper inline macros.
chain! and zip! for an arbitrary number of arguments are much better than binary chain and zip.
julio4
left a comment
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 4 unresolved discussions (waiting on @hudem1 and @orizi)
corelib/src/iter/adapters/chain_adapter.cairo line 40 at r1 (raw file):
Previously, orizi wrote…
i think it shouldn't be a function, in zip as well - as a place holder for when we have proper inline macros.
chain!andzip!for an arbitrary number of arguments are much better than binarychainandzip.
For arbitrary number of arguments it would be better I agree.
Is the inlining macro system not good enough to be able to do this now?
Should we keep these function or remove them for now?
orizi
left a comment
There was a problem hiding this comment.
Reviewable status: 1 of 11 files reviewed, 4 unresolved discussions (waiting on @hudem1 and @julio4)
corelib/src/iter/adapters/chain_adapter.cairo line 40 at r1 (raw file):
Previously, julio4 (Julio) wrote…
For arbitrary number of arguments it would be better I agree.
Is the inlining macro system not good enough to be able to do this now?
Should we keep these function or remove them for now?
not good enough now - but i hope will be better soon enough.
i think we should remove now, as we can always add, but removing after any release is much harder.
Not complete yet: an error persists in the Iterator::chain method
orizi
left a comment
There was a problem hiding this comment.
Reviewed 1 of 5 files at r1, 9 of 11 files at r3, all commit messages.
Reviewable status: 10 of 12 files reviewed, 6 unresolved discussions (waiting on @hudem1 and @julio4)
corelib/src/iter/traits/collect.cairo line 95 at r3 (raw file):
/// Trying to convert an already existing iterator into an iterator /// just returns the iterator itself
seems really unrelated to PR.
Code quote:
/// Trying to convert an already existing iterator into an iterator
/// just returns the iterator itselfcorelib/src/iter/adapters/chain_adapter.cairo line 8 at r3 (raw file):
pub struct Chain<A, B> { // These are "fused" with `Option` so we don't need separate state to track which part is // already exhausted, and we may also get niche layout for `None`.
WDYM?
Code quote:
and we may also get niche layout for `None`.corelib/src/iter/adapters/chain_adapter.cairo line 12 at r3 (raw file):
// Only the "first" iterator is actually set `None` when exhausted. a: Option<A>, b: Option<B>,
if the second one is never None - let us:
Suggestion:
// These are "fused" with `Option` so we don't need separate state to track which part is
// already exhausted, and we may also get niche layout for `None`.
//
// Only the "first" iterator is actually set `None` when exhausted.
a: Option<A>,
b: B,corelib/src/iter/adapters/chain_adapter.cairo line 49 at r3 (raw file):
self.b = Option::Some(second_container); value
Suggestion:
// First iterate over first container values.
if let Option::Some(first) = self.a {
if let Option::Some(value) = first.next() {
self.a = Option::Some(first);
return Option::Some(value)
} else {
self.a = Option::None;
}
}
// Then iterate over second container values.
self.b.next()corelib/src/iter.cairo line 3 at r3 (raw file):
mod adapters; mod traits; pub use adapters::{Chain, chained_iterator};
this is only for usage in the trait - right?
if so:
Suggestion:
pub use adapters::Chain;
pub(crate) use adapters::chained_iterator;
orizi
left a comment
There was a problem hiding this comment.
this is possibly is just the LS - unless you get this error when compiling as well now.
Reviewable status: 10 of 12 files reviewed, 5 unresolved discussions (waiting on @hudem1 and @julio4)
julio4
left a comment
There was a problem hiding this comment.
Reviewed 1 of 11 files at r3.
Reviewable status: 11 of 12 files reviewed, 6 unresolved discussions (waiting on @hudem1 and @orizi)
corelib/src/iter/adapters/chain_adapter.cairo line 0 at r3 (raw file):
After removing fn chain you can rename the module to only chain instead of chain_adapter
orizi
left a comment
There was a problem hiding this comment.
Reviewed 1 of 11 files at r3.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @hudem1)
orizi
left a comment
There was a problem hiding this comment.
please rebase - there is still some problematic issue with using an Iterator directly instead of an IntoIter as an arg - but for the time being should mostly work.
Reviewable status: 12 of 13 files reviewed, all discussions resolved (waiting on @hudem1)
orizi
left a comment
There was a problem hiding this comment.
@hudem1 please rebase and check - it is ok not having tests with direct extrainto_iter() - it is a known bug (and it is actually nice not needing the extra "clipply" style warning for removing it) - but it seems to not really hinder us for the time being.
Reviewable status: 12 of 13 files reviewed, all discussions resolved (waiting on @hudem1)
Hi ! Sorry my late reply. I'll be more careful to reply faster! I merged main into my branch, and removed the unnecessary When i run the tests, i still have the following errors:
It seems like the condition |
orizi
left a comment
There was a problem hiding this comment.
no need to appologize :) great work here.
thanks for the update - will continue looking into it now.
indeed meant removing the not required .into_iter() from the test.
Reviewed 8 of 9 files at r7, all commit messages.
Reviewable status: 12 of 13 files reviewed, 3 unresolved discussions (waiting on @hudem1)
corelib/src/test/iter_test.cairo line 2 at r7 (raw file):
use core::iter::PeekableTrait; use core::ops::Range;
corelib/src/test/iter_test.cairo line 148 at r7 (raw file):
let b: Range<u8> = 0..5; let mut iter = a.into_iter().chain(b);
Suggestion:
let a: Array<u8> = array![7, 8, 9];
let mut iter = a.into_iter().chain(0..5_u8);corelib/src/iter/traits/iterator.cairo line 2 at r7 (raw file):
use crate::iter::adapters::{ Chain, Enumerate, Filter, Map, Peekable, Zip, chained_iterator, enumerated_iterator, filter_iterator, mapped_iterator,
run ./scripts/cairo_fmt.sh.
hudem1
left a comment
There was a problem hiding this comment.
Thank you :)
Sure, let me know if there is anything else to modify!
Reviewable status: 11 of 13 files reviewed, 3 unresolved discussions (waiting on @orizi)
corelib/src/iter/traits/iterator.cairo line 2 at r7 (raw file):
Previously, orizi wrote…
run
./scripts/cairo_fmt.sh.
Done
corelib/src/test/iter_test.cairo line 2 at r7 (raw file):
use core::iter::PeekableTrait; use core::ops::Range;
Done
corelib/src/test/iter_test.cairo line 148 at r7 (raw file):
let b: Range<u8> = 0..5; let mut iter = a.into_iter().chain(b);
Done. I actually inlined a as well and in the other test as well
orizi
left a comment
There was a problem hiding this comment.
Reviewed 2 of 2 files at r8, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @hudem1)
orizi
left a comment
There was a problem hiding this comment.
you have other bugs - can you first makes sure the corelib itself compiles?
run ./scripts/cairo_test.sh.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @hudem1)
corelib/src/iter/traits/iterator.cairo line 722 at r7 (raw file):
/// assert_eq!(iter.next(), Option::None); /// ``` fn chain<U, impl IntoIterU: IntoIterator<U>, +TypeEqual<Self::Item, IntoIterU::Iterator::Item>>(
probably the cause for the actual failure.
Suggestion:
fn chain<U, impl IntoIterU: IntoIterator<U>, +TypeEqual<Self::Item, IntoIterU::Iterator::Item>, +Destruct<T>>(
orizi
left a comment
There was a problem hiding this comment.
any updates?
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @hudem1)
orizi
left a comment
There was a problem hiding this comment.
please update - or tomorrow i will need to redo the PR.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @hudem1)
Oh no, late reply a 2nd time ! :( Anyway, it now compiles and tests pass, great!! Thank you for the fix! |
orizi
left a comment
There was a problem hiding this comment.
Reviewed 3 of 3 files at r10, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @hudem1)
orizi
left a comment
There was a problem hiding this comment.
Reviewable status:
complete! all files reviewed, all discussions resolved (waiting on @hudem1)
a discussion (no related file):
@gilbens-starkware @TomerStarkware for 2nd eye.
gilbens-starkware
left a comment
There was a problem hiding this comment.
Reviewed 4 of 13 files at r6, 6 of 9 files at r7, 3 of 3 files at r10, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @hudem1)
TomerStarkware
left a comment
There was a problem hiding this comment.
Reviewable status:
complete! all files reviewed, all discussions resolved (waiting on @hudem1)



pub fn chain<A, B>(a: A, b: B) -> Chain<A, B>Converts the arguments to iterators and links them together, in a chain.
Example