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

[12.x] Add step parameter to LazyCollection range method #53473

Merged
merged 3 commits into from
Nov 12, 2024

Conversation

Ashot1995
Copy link
Contributor

@Ashot1995 Ashot1995 commented Nov 12, 2024

The range method is designed to create a collection with integer values within a specified range, allowing the caller to define both a starting point ($from) and an endpoint ($to). An optional $step parameter specifies the increment or decrement for each iteration through the range.

Here's a breakdown of its behavior:

Parameters:
$from: The starting integer of the range.
$to: The ending integer of the range.
$step (optional): The amount by which the range increments or decrements. By default, this is set to 1.

Validation:
If $step is set to 0, an InvalidArgumentException is thrown. This validation ensures that there is no infinite loop caused by a zero increment, which would result in the range generator producing the same number indefinitely.

Range Generation Logic:
The method uses a generator to yield each value in the range without requiring memory to store all values at once, making it efficient for large ranges.
If $from is less than or equal to $to, it iterates by adding the absolute value of $step until reaching or surpassing $to.
If $from is greater than $to, it iterates by subtracting the absolute value of $step until it reaches or is less than $to.

Example Usage:

// Example for a forward range
$range = Collection::range(1, 10); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

// Example for a range with a step of 2
$range = Collection::range(1, 10, 2); // [1, 3, 5, 7, 9]

// Example for a reverse range
$range = Collection::range(10, 1); // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

// Example for a reverse range with a step of 2
$range = Collection::range(10, 1, 2); // [10, 8, 6, 4, 2]

This method provides a flexible way to generate ranges with custom increments or decrements, making it suitable for various use cases in numerical data processing or iteration.

@Ashot1995 Ashot1995 changed the base branch from 11.x to master November 12, 2024 07:43
@Ashot1995 Ashot1995 changed the title Step in collection range DRAFT Step in collection range Nov 12, 2024
@Ashot1995 Ashot1995 changed the title DRAFT Step in collection range Step in collection range Nov 12, 2024
@Ashot1995 Ashot1995 changed the title Step in collection range [12.x] Step in collection range Nov 12, 2024
@Ashot1995 Ashot1995 changed the title [12.x] Step in collection range [12.x] Add step parameter to LazyCollection range method Nov 12, 2024
@taylorotwell taylorotwell merged commit 61adf1a into laravel:master Nov 12, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants