-
Notifications
You must be signed in to change notification settings - Fork 70
Support for cycle #17
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -326,6 +326,33 @@ function repeat($value, $num = INF) { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Takes an iterator and cycles through its elements a given number of times | ||
| * | ||
| * If working with a generator it is best to make it rewindable if the expected | ||
| * number of elements is large | ||
| * | ||
| * Examples: | ||
| * | ||
| * iter\cycle([1, 2, 3], 3) | ||
| * => iter(1, 2, 3, 1, 2, 3, 1, 2, 3) | ||
| * | ||
| * @param mixed $iterable The iterator to cycle | ||
| * @param $num The number of items to cycle | ||
| * | ||
| * @return \Iterator | ||
| */ | ||
| function cycle($iterable, $num = INF) { | ||
| if ($iterable instanceof \Generator) { | ||
| $iterable = toArray($iterable); | ||
| } | ||
|
Owner
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. The main thing I'm unsure about here is this automatic toArray conversion. The iterator below preserves keys, so this should probably be toArrayWithKeys -- however, converting generators with keys will very often not produce the desired result due to overwritten keys. One could store keys and values in separate arrays to avoid that, though that'll make the implementation quite ugly. |
||
| for ($i = 0; $i < $num; ++$i) { | ||
| foreach ($iterable as $key => $value) { | ||
| yield $key => $value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the keys of an iterable. | ||
| * | ||
|
|
||
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.
won't this run endless in case $num is not provided?
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.
Yes it will :) it is designed as
repeatis and how the pythoncyclefunction is designed.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.
@staabm It is useful because if you create conditions that stop iterating the generator then it wont run forever. In other words you get a chance to break the iteration after each yield.
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.
But shouldn't there be a break or something like that after all values have been yielded?ä
How would the caller of this function otherwise break the endless loop?
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.
It cycles through the values of the iterator yielding each value until
$numcycles are complete. If$numisINFthen the generator will continue yielding values from the iterator until iteration of the generator stops (which may be never).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.
Here is a non-useful but informative example: