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

[5.4] Create a static "times" method on the collection #18457

Merged
merged 1 commit into from Mar 25, 2017

Conversation

Projects
None yet
4 participants
@JosephSilber
Contributor

JosephSilber commented Mar 23, 2017

This is extremely useful to whip up a quick list of stuff, whether in a test or in production:

$slugs = Collection::times(5, function ($number) {
    return 'slug-'.$number;
});

// ['slug-1', 'slug-2', 'slug-3', 'slug-4', 'slug-5'];

You can also use it with factories, for example if you want sequential numbers:

$products = Collection::times(5, function ($number) {
    return factory(Product::class)->create(['name' => 'Product #'.$number]);
});

@JosephSilber JosephSilber changed the title from [5.4] Create a static range method on the collection to [5.4] Create a static "times" method on the collection Mar 23, 2017

@taylorotwell

This comment has been minimized.

Show comment
Hide comment
@taylorotwell

taylorotwell Mar 23, 2017

Member

The name of the method needs work maybe? times in Ruby is more like an each operation, so it's a little confusing to me?

Member

taylorotwell commented Mar 23, 2017

The name of the method needs work maybe? times in Ruby is more like an each operation, so it's a little confusing to me?

@JosephSilber

This comment has been minimized.

Show comment
Hide comment
@JosephSilber

JosephSilber Mar 23, 2017

Contributor

This is an each (plus a map).


There's nothing stopping you from just ignoring everything else:

Collection::times(5, function () {
    echo 'I will be logged 5 times.';
});
Contributor

JosephSilber commented Mar 23, 2017

This is an each (plus a map).


There's nothing stopping you from just ignoring everything else:

Collection::times(5, function () {
    echo 'I will be logged 5 times.';
});
@deleugpn

This comment has been minimized.

Show comment
Hide comment
@deleugpn

deleugpn Mar 23, 2017

Contributor

Collection::repeat?

Contributor

deleugpn commented Mar 23, 2017

Collection::repeat?

@jmarcher

This comment has been minimized.

Show comment
Hide comment
@jmarcher

jmarcher Mar 24, 2017

Contributor

Failing test:

    public function testTimesWithZeroMethod()
    {
        $this->assertEquals([], Collection::times(0, function ($number) {
            return 'do-not-enter-'.$number;
        })->all());
    }
Contributor

jmarcher commented Mar 24, 2017

Failing test:

    public function testTimesWithZeroMethod()
    {
        $this->assertEquals([], Collection::times(0, function ($number) {
            return 'do-not-enter-'.$number;
        })->all());
    }
@JosephSilber

This comment has been minimized.

Show comment
Hide comment
@JosephSilber

JosephSilber Mar 24, 2017

Contributor
Contributor

JosephSilber commented Mar 24, 2017

@jmarcher

This comment has been minimized.

Show comment
Hide comment
@jmarcher

jmarcher Mar 24, 2017

Contributor

Negative numbers do not make sense but if we I see a method called "times" and give a 0 as parameter I expect that it executes zero times

Contributor

jmarcher commented Mar 24, 2017

Negative numbers do not make sense but if we I see a method called "times" and give a 0 as parameter I expect that it executes zero times

@JosephSilber

This comment has been minimized.

Show comment
Hide comment
@JosephSilber

JosephSilber Mar 24, 2017

Contributor

@jmarcher makes sense. Added it 👍

Contributor

JosephSilber commented Mar 24, 2017

@jmarcher makes sense. Added it 👍

@taylorotwell taylorotwell merged commit 5e86420 into laravel:5.4 Mar 25, 2017

2 checks passed

continuous-integration/styleci/pr The StyleCI analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@JosephSilber JosephSilber deleted the JosephSilber:collection-range branch Mar 26, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment