-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add __length_hint__ support for iterators #1666
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6b537f1
Add objsequence::{opt_,}len
coolreader18 fc3ac16
Add length_hint functionality
coolreader18 c65dc33
Add _operator module
coolreader18 098393d
Add __length_hint__ to a bunch of iterators
coolreader18 a26e5f2
Add dict iterator __length_hint__
coolreader18 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add __length_hint__ to a bunch of iterators
- Loading branch information
commit 098393d4d6e4db91d31868800ef7431535825971
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
use std::cell::{Cell, RefCell}; | ||
use std::cmp::Ordering; | ||
use std::iter; | ||
use std::ops::{AddAssign, SubAssign}; | ||
use std::rc::Rc; | ||
|
||
use num_bigint::BigInt; | ||
use num_traits::sign::Signed; | ||
use num_traits::ToPrimitive; | ||
use num_traits::{One, Signed, ToPrimitive, Zero}; | ||
|
||
use crate::function::{Args, OptionalArg, OptionalOption, PyFuncArgs}; | ||
use crate::obj::objbool; | ||
|
@@ -166,11 +163,11 @@ impl PyItertoolsCount { | |
) -> PyResult<PyRef<Self>> { | ||
let start = match start.into_option() { | ||
Some(int) => int.as_bigint().clone(), | ||
None => BigInt::from(0), | ||
None => BigInt::zero(), | ||
}; | ||
let step = match step.into_option() { | ||
Some(int) => int.as_bigint().clone(), | ||
None => BigInt::from(1), | ||
None => BigInt::one(), | ||
}; | ||
|
||
PyItertoolsCount { | ||
|
@@ -183,7 +180,7 @@ impl PyItertoolsCount { | |
#[pymethod(name = "__next__")] | ||
fn next(&self, _vm: &VirtualMachine) -> PyResult<PyInt> { | ||
let result = self.cur.borrow().clone(); | ||
AddAssign::add_assign(&mut self.cur.borrow_mut() as &mut BigInt, &self.step); | ||
*self.cur.borrow_mut() += &self.step; | ||
Ok(PyInt::new(result)) | ||
} | ||
|
||
|
@@ -296,16 +293,11 @@ impl PyItertoolsRepeat { | |
|
||
#[pymethod(name = "__next__")] | ||
fn next(&self, vm: &VirtualMachine) -> PyResult { | ||
if self.times.is_some() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice :) |
||
match self.times.as_ref().unwrap().borrow().cmp(&BigInt::from(0)) { | ||
Ordering::Less | Ordering::Equal => return Err(new_stop_iteration(vm)), | ||
_ => (), | ||
}; | ||
|
||
SubAssign::sub_assign( | ||
&mut self.times.as_ref().unwrap().borrow_mut() as &mut BigInt, | ||
&BigInt::from(1), | ||
); | ||
if let Some(ref times) = self.times { | ||
if *times.borrow() <= BigInt::zero() { | ||
return Err(new_stop_iteration(vm)); | ||
} | ||
*times.borrow_mut() -= 1; | ||
} | ||
|
||
Ok(self.object.clone()) | ||
|
@@ -315,6 +307,14 @@ impl PyItertoolsRepeat { | |
fn iter(zelf: PyRef<Self>, _vm: &VirtualMachine) -> PyRef<Self> { | ||
zelf | ||
} | ||
|
||
#[pymethod(name = "__length_hint__")] | ||
fn length_hint(&self, vm: &VirtualMachine) -> PyObjectRef { | ||
match self.times { | ||
Some(ref times) => vm.new_int(times.borrow().clone()), | ||
None => vm.new_int(0), | ||
} | ||
} | ||
} | ||
|
||
#[pyclass(name = "starmap")] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is inv
2F93
alid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.