-
Notifications
You must be signed in to change notification settings - Fork 1k
[Variant] Make VariantArray
iterable
#8613
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
[Variant] Make VariantArray
iterable
#8613
Conversation
a5da641
to
f8c109b
Compare
afa37a5
to
a254af8
Compare
For context this was removed in #8392 |
a254af8
to
3cdd28b
Compare
arrow::Array
for VariantArray
VariantArray
iterable
Thanks for clarifying |
e188b4b
to
d65a5c3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! this looks very useful to me -- thanks @friendlymatthew
|
||
/// An iterator over [`VariantArray`] | ||
/// | ||
/// This iterator returns `Option<Option<Variant<'a, 'a>>>` where: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for the comments
out | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I double checked the definition of size_hint and this looks good:
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.size_hint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! One code cleanup to consider.
if self.head_i == self.tail_i { | ||
return None; | ||
} | ||
|
||
let out = if self.is_null(self.head_i) { | ||
Some(None) | ||
} else { | ||
Some(Some(self.array.value(self.head_i))) | ||
}; | ||
|
||
self.head_i += 1; | ||
|
||
out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if self.head_i == self.tail_i { | |
return None; | |
} | |
let out = if self.is_null(self.head_i) { | |
Some(None) | |
} else { | |
Some(Some(self.array.value(self.head_i))) | |
}; | |
self.head_i += 1; | |
out | |
(self.head_i < self.tail_i) | |
.then(|| self.value_opt(self.head_i)) | |
.inspect(|_| self.head_i += 1) |
(same story for DoubleEndedIterator
below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi hi, I think using combinators are great, but in this case we're 1) hurting readability and 2) using Option::inspect
to mutate state
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree the inspect
call was too clever, but why would this hurt readability?
(self.head_i < self.tail_i).then(|| {
let out = self.value_opt(self.head_i);
self.head_i += 1;
out
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think cases like the following are completely valid and very readable. It's simply inspecting the state
(self.head_i < self.tail_i).then(|| self.value_opt(self.head_i));
In the closure you posted above, I'm just a bit surprised as a reader that the closure is inspecting the state and mutating it. Plus, I think the following form is simpler to reason through
// base case
if self.head_i == self.tail_i {
return None;
}
// else
let out = self.value_opt(self.head_i);
self.head_i += 1;
Some(out)
But then again, it's just personal preference. I don't think there's anything wrong with either approach
d65a5c3
to
3c5bcd4
Compare
Thanks @scovich and @friendlymatthew |
Which issue does this PR close?
VariantArray
#8609VariantArray
iterable #8612This PR introduces an Iterator over
VariantArray
. SinceVariantArray
does notimpl Array
, we can't make use ofArrayIter