Proposal: Add .times method to Integer type

I like how clean "100.times { doSomething() }" looks, but I'm concerned its
usefulness will be limited because control-flow statements like
break/continue/return won't work from inside a closure.

Jacob

···

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org> wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca>:

It doesn't need to be an underscore, but when it is not, the compiler
emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that
there is the clutter of an underscore at all what I don’t like and what
makes me feel the code isn’t as clean as it could be.

*/tmp/test.swift:3:7: **warning: **immutable value 'i' was never used;
consider replacing with '_' or removing it*

You can also use inclusive ranges instead if you're more comfortable with
that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few years
ago and I saw computer science students struggle with the exact number a
loop was being executed. So that’s the only reason I brought up that
example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds to
something that could so easily made simpler and more descriptive.

I think this is also a question of: *How many convenience methods do we
want to see in the Swift standard library?* In Ruby, at least, there
seemed to be enough people to find this one useful. And it’s the first
method I missed until now, so I took that as a sign before suggesting the
addition. I also don’t like when there are thousands of convenience methods
for things that could easily be written in other ways – but I don’t feel
that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive
Swift that you can pick up without knowing advanced concepts. However,
there is definitely value in helping people learn, and learning always
moves you a little bit out of your comfort zone. When do we remove the
training wheels? How long can we hide the fact that indices usually start
at 0? How long before you need to iterate an array using the same
range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners
ask for lots of things, but the people who ask about the for loop are
usually people with a background in another C-like language who try to use
the arguably less readable C-like for loop. I've never seen anyone before
say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices or
beginners. The fact that readability and expressiveness make a language
easier to learn for beginners IMHO is just a side effect of a well
thought-out and developed language. Maybe I wasn’t clear enough but I want
to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution < > swift-evolution@swift.org> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library
like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks *clean* nor *readable* especially
for beginners that there is an *underscore* in the closure. Also
beginners often get confused with the number of times some code is run when
*starting to count from 0* which is also why I think it shouldn’t
appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch>:

(or with a for in loop -- but i guess you have a reason for using
.foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution < > swift-evolution@swift.org> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution < > swift-evolution@swift.org> wrote:

Dear Swift-Community,

I’d like to propose an *addition of a useful method*, especially for
beginners that also makes Swift much more readable in some situations: The
addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the
scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent,
expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some
thousand times. I also like to use the Ruby language and one thing I love
about it is that it has some really handy methods integrated to the
language in situations like this which make the code very readable and
therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods
appear in Swift, too and this is the first I came across that I really
missed. So I’m asking myself, what if I could write the same code above
like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent,
expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for
example by using the .stride method like I used) without any side effects
and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did
everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

-1 from me, if nothing else because the name is confusable with multiplication in the context of integers.

···

On Dec 18, 2015, at 11:53 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org> wrote:

I like how clean "100.times { doSomething() }" looks, but I'm concerned its usefulness will be limited because control-flow statements like break/continue/return won't work from inside a closure.

Jacob

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca <mailto:felixcca@yahoo.ca>>:

It doesn't need to be an underscore, but when it is not, the compiler emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that there is the clutter of an underscore at all what I don’t like and what makes me feel the code isn’t as clean as it could be.

/tmp/test.swift:3:7: warning: immutable value 'i' was never used; consider replacing with '_' or removing it

You can also use inclusive ranges instead if you're more comfortable with that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few years ago and I saw computer science students struggle with the exact number a loop was being executed. So that’s the only reason I brought up that example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds to something that could so easily made simpler and more descriptive.

I think this is also a question of: How many convenience methods do we want to see in the Swift standard library? In Ruby, at least, there seemed to be enough people to find this one useful. And it’s the first method I missed until now, so I took that as a sign before suggesting the addition. I also don’t like when there are thousands of convenience methods for things that could easily be written in other ways – but I don’t feel that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive Swift that you can pick up without knowing advanced concepts. However, there is definitely value in helping people learn, and learning always moves you a little bit out of your comfort zone. When do we remove the training wheels? How long can we hide the fact that indices usually start at 0? How long before you need to iterate an array using the same range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners ask for lots of things, but the people who ask about the for loop are usually people with a background in another C-like language who try to use the arguably less readable C-like for loop. I've never seen anyone before say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices or beginners. The fact that readability and expressiveness make a language easier to learn for beginners IMHO is just a side effect of a well thought-out and developed language. Maybe I wasn’t clear enough but I want to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks clean nor readable especially for beginners that there is an underscore in the closure. Also beginners often get confused with the number of times some code is run when starting to count from 0 which is also why I think it shouldn’t appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch <mailto:kissling@oberon.ch>>:

(or with a for in loop -- but i guess you have a reason for using .foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Dear Swift-Community,

I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some thousand times. I also like to use the Ruby language and one thing I love about it is that it has some really handy methods integrated to the language in situations like this which make the code very readable and therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods appear in Swift, too and this is the first I came across that I really missed. So I’m asking myself, what if I could write the same code above like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for example by using the .stride method like I used) without any side effects and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

-Dave

I’d be in favor of deferring this until other discussions about the ability to return/break/continue from closures play out. With that in place, there’s even the notion that for…in could be replaced by a library function. At that point, 5.times, or repeat(5) as a library call, would look a lot more attractive.

P

···

On Dec 18, 2015, at 3:41 PM, Cihat Gündüz via swift-evolution <swift-evolution@swift.org> wrote:

I agree with Radek. I find `for i in 5 { doSomething() }` or `for 5 { doSomething() }` to be very confusing since it is neither close to human language nor to any common programming language I know of.

I like the idea of giving students a step by step introduction into things, but this is IMO not the right way/place to do that.

– Cihat

Am 18.12.2015 um 22:26 schrieb Radosław Pietruszewski via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>>:

An obvious question is, should it be equivalent to 1...n, or 0..<n?

I think that’s exactly why this isn’t a good idea. The semantics of `for i in 5` are not immediately clear at all.

If this was to be a language feature, `repeat 5`, suggested by Chris, seems like the least-ambiguous choice.

— Radek

On 18 Dec 2015, at 22:09, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

This can be done pretty easily, although I think the approach has the potential to cause confusion elsewhere in code. An obvious question is, should it be equivalent to 1...n, or 0..<n?

extension Int: SequenceType {
    public func generate() -> RangeGenerator<Int> {
        return (0..<self).generate()
    }
}

for i in 5 {
    print("hello \(i)")
}

Jacob Bandes-Storch

On Fri, Dec 18, 2015 at 1:03 PM, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
> I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

I’ve said it before, but I don’t think `times` is a good solution for learners. It teaches them a form of looping they will never use in practice and does not allow them to practice with ancillary loop skills like `break` and `continue`.

I think our best bet is to extend the `for` loop to allow a single number, meaning either `1…n` or `0..<n` (you can argue it either way), and also to allow the `variableName in` part to be omitted, meaning `_ in`. This gives us the pedagogical simplicity of a “do this N times” loop, but couches it in a form where, when the student moves on, more commonly used loop forms are a straightforward extension of that simple case.

        for 5 { print(“Hello!”) }
        for i in 5 { print(“Hello \(i)!”) }
        for i in 10..<20 { print(“Hello \(i)!”) }
        for i in 10...20 { print(“Hello \(i)!”) }

--
Brent Royal-Gordon
Architechies

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

@Jacob, @Radek: Seems like you’ve found a clear restriction to the methods usefulness given a simple implementation. You are right, of course. But isn’t that more a sign that Swift needs a way to make closures more useful by adding the possibility of breaking/continueing/returning from within them rather than a disadvantage of the `times`-syntax itself?

I mean, I find the closure-solution useful already. But if break/continue/return would work from within the curly braces somehow (either by a non-closure-based implementation like for-in loops or via a future addition of some kind of strong/weak return etc.) then I agree that it would be even more useful.

Do you think `times` wouldn’t be useful enough with the closure restriction?

···

Am 18.12.2015 um 20:53 schrieb Jacob Bandes-Storch <jtbandes@gmail.com>:

I like how clean "100.times { doSomething() }" looks, but I'm concerned its usefulness will be limited because control-flow statements like break/continue/return won't work from inside a closure.

Jacob

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca <mailto:felixcca@yahoo.ca>>:

It doesn't need to be an underscore, but when it is not, the compiler emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that there is the clutter of an underscore at all what I don’t like and what makes me feel the code isn’t as clean as it could be.

/tmp/test.swift:3:7: warning: immutable value 'i' was never used; consider replacing with '_' or removing it

You can also use inclusive ranges instead if you're more comfortable with that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few years ago and I saw computer science students struggle with the exact number a loop was being executed. So that’s the only reason I brought up that example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds to something that could so easily made simpler and more descriptive.

I think this is also a question of: How many convenience methods do we want to see in the Swift standard library? In Ruby, at least, there seemed to be enough people to find this one useful. And it’s the first method I missed until now, so I took that as a sign before suggesting the addition. I also don’t like when there are thousands of convenience methods for things that could easily be written in other ways – but I don’t feel that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive Swift that you can pick up without knowing advanced concepts. However, there is definitely value in helping people learn, and learning always moves you a little bit out of your comfort zone. When do we remove the training wheels? How long can we hide the fact that indices usually start at 0? How long before you need to iterate an array using the same range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners ask for lots of things, but the people who ask about the for loop are usually people with a background in another C-like language who try to use the arguably less readable C-like for loop. I've never seen anyone before say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices or beginners. The fact that readability and expressiveness make a language easier to learn for beginners IMHO is just a side effect of a well thought-out and developed language. Maybe I wasn’t clear enough but I want to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks clean nor readable especially for beginners that there is an underscore in the closure. Also beginners often get confused with the number of times some code is run when starting to count from 0 which is also why I think it shouldn’t appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch <mailto:kissling@oberon.ch>>:

(or with a for in loop -- but i guess you have a reason for using .foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Dear Swift-Community,

I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some thousand times. I also like to use the Ruby language and one thing I love about it is that it has some really handy methods integrated to the language in situations like this which make the code very readable and therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods appear in Swift, too and this is the first I came across that I really missed. So I’m asking myself, what if I could write the same code above like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for example by using the .stride method like I used) without any side effects and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

-1 from me, if nothing else because the name is confusable with multiplication in the context of integers.

Isn’t it a multiplication? A multiplication of the closure specified? I see it as such.

Also I don’t think many will confuse a method that takes a closure with the multiplication of two integers. I may be wrong, of course.

– Cihat

···

Am 18.12.2015 um 21:02 schrieb Dave Abrahams <dabrahams@apple.com>:

On Dec 18, 2015, at 11:53 AM, Jacob Bandes-Storch via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

I like how clean "100.times { doSomething() }" looks, but I'm concerned its usefulness will be limited because control-flow statements like break/continue/return won't work from inside a closure.

Jacob

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca <mailto:felixcca@yahoo.ca>>:

It doesn't need to be an underscore, but when it is not, the compiler emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that there is the clutter of an underscore at all what I don’t like and what makes me feel the code isn’t as clean as it could be.

/tmp/test.swift:3:7: warning: immutable value 'i' was never used; consider replacing with '_' or removing it

You can also use inclusive ranges instead if you're more comfortable with that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few years ago and I saw computer science students struggle with the exact number a loop was being executed. So that’s the only reason I brought up that example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds to something that could so easily made simpler and more descriptive.

I think this is also a question of: How many convenience methods do we want to see in the Swift standard library? In Ruby, at least, there seemed to be enough people to find this one useful. And it’s the first method I missed until now, so I took that as a sign before suggesting the addition. I also don’t like when there are thousands of convenience methods for things that could easily be written in other ways – but I don’t feel that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive Swift that you can pick up without knowing advanced concepts. However, there is definitely value in helping people learn, and learning always moves you a little bit out of your comfort zone. When do we remove the training wheels? How long can we hide the fact that indices usually start at 0? How long before you need to iterate an array using the same range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners ask for lots of things, but the people who ask about the for loop are usually people with a background in another C-like language who try to use the arguably less readable C-like for loop. I've never seen anyone before say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices or beginners. The fact that readability and expressiveness make a language easier to learn for beginners IMHO is just a side effect of a well thought-out and developed language. Maybe I wasn’t clear enough but I want to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks clean nor readable especially for beginners that there is an underscore in the closure. Also beginners often get confused with the number of times some code is run when starting to count from 0 which is also why I think it shouldn’t appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch <mailto:kissling@oberon.ch>>:

(or with a for in loop -- but i guess you have a reason for using .foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Dear Swift-Community,

I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some thousand times. I also like to use the Ruby language and one thing I love about it is that it has some really handy methods integrated to the language in situations like this which make the code very readable and therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods appear in Swift, too and this is the first I came across that I really missed. So I’m asking myself, what if I could write the same code above like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for example by using the .stride method like I used) without any side effects and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

-Dave

I think I'm -1 for this. It feels like something that should exist in a
utility library rather than in the standard one.

I do have an honest question that comes from ignorance rather than malice:
has anyone actually used .times in ruby outside of the context of learning
or testing? I've written a fair amount of ruby and I don't think I've ever
seen it in production code. It is definitely a cleaner construct for what
it's trying to do, but I'm having trouble convincing myself it's a common
enough pattern to justify it being standard.

···

On Fri, Dec 18, 2015 at 6:20 PM Cihat Gündüz <swift-evolution@swift.org> wrote:

By the way and/or for all of you who like the idea of a .times method:

I’ve just setup a library for features like this `times` method and
implemented the suggested method there. Feel free to contribute code /
provide feedback via that third party library (as suggested by Chris) here:

GitHub - FlineDev/HandySwift: Handy Swift features that didn't make it into the Swift standard library.

– Cihat

Am 18.12.2015 um 23:42 schrieb Cihat Gündüz via swift-evolution < > swift-evolution@swift.org>:

As far as I can remember it was never a goal of Swift to be a purely
object-oriented language. Instead I can find expressiveness stated
explicitly amongst the three main goals behind the language here:
Swift.org - About Swift

Therefore I feel it is okay if the language becomes more expressive in a
way that is less object-oriented. I see the suggested method as a
functional construct.

– Cihat

Am 18.12.2015 um 23:22 schrieb Kenny Leung <kenny_leung@pobox.com>:

I don’t agree with this on the grounds that it isn’t very object-oriented.
That is, it does not conform with what one usually associates with an
integer. A number has certain intrinsic properties: 5 is greater than 4 but
less than 6. If you’re a synesthete, it may have the color blue. But I
never think of there being a big number 5 that takes something and repeats
it 5 times. I think of a person taking something and doing it over 5 times.

-Kenny

On Dec 18, 2015, at 10:25 AM, Cihat Gündüz via swift-evolution < > swift-evolution@swift.org> wrote:

Dear Swift-Community,

I’d like to propose an addition of a useful method, especially for
beginners that also makes Swift much more readable in some situations: The
addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the
scalability of an important piece of code and wrote this method:

   func testPerfQualityInPercentWithoutQualityImprovements() {
       self.measureBlock {
           let expectedQuality = 33.33
           0.stride(to: 5_000, by: 1).forEach { _ in
               XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent,
expectedQuality, accuracy: 0.1)
           }
       }
   }

As you can see what I basically wanted was to repeat the test some
thousand times. I also like to use the Ruby language and one thing I love
about it is that it has some really handy methods integrated to the
language in situations like this which make the code very readable and
therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods
appear in Swift, too and this is the first I came across that I really
missed. So I’m asking myself, what if I could write the same code above
like this:

   func testPerfQualityInPercentWithoutQualityImprovements() {
       self.measureBlock {
           let expectedQuality = 33.33
           5_000.times {
               XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent,
expectedQuality, accuracy: 0.1)
           }
       }
   }

I think it could be added to the Swift standard library very easily (for
example by using the .stride method like I used) without any side effects
and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did
everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I do have an honest question that comes from ignorance rather than malice: has anyone actually used .times in ruby outside of the context of learning or testing?

I've seen it used to retry a failing operation a certain number of times:

  5.times do
    return true if do_network_call
    sleep 5
  end
  raise NetworkCallFailedError

Of course, that requires you to be able to return out of the surrounding function from the `times` loop, which you can do in Ruby but not in Swift.

···

--
Brent Royal-Gordon
Architechies

It's a bit tangential but has there been any discussion about inlining
closures that anyone is aware of? So you could return from the outer
function within a closure, or break the outer loop.

···

On Fri, Dec 18, 2015, 3:09 PM Cihat Gündüz <swift-evolution@swift.org> wrote:

@Jacob, @Radek: Seems like you’ve found a clear restriction to the methods
usefulness given a simple implementation. You are right, of course. But
isn’t that more a sign that Swift needs a way to make closures more useful
by adding the possibility of breaking/continueing/returning from within
them rather than a disadvantage of the `times`-syntax itself?

I mean, I find the closure-solution useful already. But if
break/continue/return would work from within the curly braces somehow
(either by a non-closure-based implementation like for-in loops or via a
future addition of some kind of strong/weak return etc.) then I agree that
it would be even more useful.

Do you think `times` wouldn’t be useful enough with the closure
restriction?

Am 18.12.2015 um 20:53 schrieb Jacob Bandes-Storch <jtbandes@gmail.com>:

I like how clean "100.times { doSomething() }" looks, but I'm concerned
its usefulness will be limited because control-flow statements like
break/continue/return won't work from inside a closure.

Jacob

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org> > wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca>:

It doesn't need to be an underscore, but when it is not, the compiler
emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that
there is the clutter of an underscore at all what I don’t like and what
makes me feel the code isn’t as clean as it could be.

*/tmp/test.swift:3:7: **warning: **immutable value 'i' was never used;
consider replacing with '_' or removing it*

You can also use inclusive ranges instead if you're more comfortable with
that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few
years ago and I saw computer science students struggle with the exact
number a loop was being executed. So that’s the only reason I brought up
that example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds
to something that could so easily made simpler and more descriptive.

I think this is also a question of: *How many convenience methods do we
want to see in the Swift standard library?* In Ruby, at least, there
seemed to be enough people to find this one useful. And it’s the first
method I missed until now, so I took that as a sign before suggesting the
addition. I also don’t like when there are thousands of convenience methods
for things that could easily be written in other ways – but I don’t feel
that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive
Swift that you can pick up without knowing advanced concepts. However,
there is definitely value in helping people learn, and learning always
moves you a little bit out of your comfort zone. When do we remove the
training wheels? How long can we hide the fact that indices usually start
at 0? How long before you need to iterate an array using the same
range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners
ask for lots of things, but the people who ask about the for loop are
usually people with a background in another C-like language who try to use
the arguably less readable C-like for loop. I've never seen anyone before
say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices
or beginners. The fact that readability and expressiveness make a language
easier to learn for beginners IMHO is just a side effect of a well
thought-out and developed language. Maybe I wasn’t clear enough but I want
to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution < >> swift-evolution@swift.org> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library
like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks *clean* nor *readable* especially
for beginners that there is an *underscore* in the closure. Also
beginners often get confused with the number of times some code is run when
*starting to count from 0* which is also why I think it shouldn’t
appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch>:

(or with a for in loop -- but i guess you have a reason for using
.foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution < >> swift-evolution@swift.org> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution < >> swift-evolution@swift.org> wrote:

Dear Swift-Community,

I’d like to propose an *addition of a useful method*, especially for
beginners that also makes Swift much more readable in some situations: The
addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the
scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.
qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some
thousand times. I also like to use the Ruby language and one thing I love
about it is that it has some really handy methods integrated to the
language in situations like this which make the code very readable and
therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods
appear in Swift, too and this is the first I came across that I really
missed. So I’m asking myself, what if I could write the same code above
like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.
qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for
example by using the .stride method like I used) without any side effects
and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did
everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

But isn’t that more a sign that Swift needs a way to make closures more useful by adding the possibility of breaking/continueing/returning from within them rather than a disadvantage of the `times`-syntax itself?

Perhaps — there’s a thread, somewhere, with possible solutions to this.

FWIW, I’m just as concerned about allowing returning/etc in closures, as with the lack of this ability. Ruby has different forms of “closures”, far too many of them actually, and some of them truly act like functions (just like in Swift), and in some, return/etc changes the calling function. And… albeit useful… this can be _really_ confusing.

Do you think `times` wouldn’t be useful enough with the closure restriction?

I’d still like it, but it’s just this tiny little thing. Swift standard library is currently very bare-bones, unlike, say, Ruby’s, which has *a ton* of stuff on Arrays, Strings, etc. Unless the Core Team is OK with expanding those standard types with more useful helper methods more broadly, there’s no reason why `times` in particular should go in.

/ccing Chris on this question.

— Radek

···

On 18 Dec 2015, at 21:09, Cihat Gündüz via swift-evolution <swift-evolution@swift.org> wrote:

@Jacob, @Radek: Seems like you’ve found a clear restriction to the methods usefulness given a simple implementation. You are right, of course. But isn’t that more a sign that Swift needs a way to make closures more useful by adding the possibility of breaking/continueing/returning from within them rather than a disadvantage of the `times`-syntax itself?

I mean, I find the closure-solution useful already. But if break/continue/return would work from within the curly braces somehow (either by a non-closure-based implementation like for-in loops or via a future addition of some kind of strong/weak return etc.) then I agree that it would be even more useful.

Do you think `times` wouldn’t be useful enough with the closure restriction?

Am 18.12.2015 um 20:53 schrieb Jacob Bandes-Storch <jtbandes@gmail.com <mailto:jtbandes@gmail.com>>:

I like how clean "100.times { doSomething() }" looks, but I'm concerned its usefulness will be limited because control-flow statements like break/continue/return won't work from inside a closure.

Jacob

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca <mailto:felixcca@yahoo.ca>>:

It doesn't need to be an underscore, but when it is not, the compiler emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that there is the clutter of an underscore at all what I don’t like and what makes me feel the code isn’t as clean as it could be.

/tmp/test.swift:3:7: warning: immutable value 'i' was never used; consider replacing with '_' or removing it

You can also use inclusive ranges instead if you're more comfortable with that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few years ago and I saw computer science students struggle with the exact number a loop was being executed. So that’s the only reason I brought up that example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds to something that could so easily made simpler and more descriptive.

I think this is also a question of: How many convenience methods do we want to see in the Swift standard library? In Ruby, at least, there seemed to be enough people to find this one useful. And it’s the first method I missed until now, so I took that as a sign before suggesting the addition. I also don’t like when there are thousands of convenience methods for things that could easily be written in other ways – but I don’t feel that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive Swift that you can pick up without knowing advanced concepts. However, there is definitely value in helping people learn, and learning always moves you a little bit out of your comfort zone. When do we remove the training wheels? How long can we hide the fact that indices usually start at 0? How long before you need to iterate an array using the same range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners ask for lots of things, but the people who ask about the for loop are usually people with a background in another C-like language who try to use the arguably less readable C-like for loop. I've never seen anyone before say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices or beginners. The fact that readability and expressiveness make a language easier to learn for beginners IMHO is just a side effect of a well thought-out and developed language. Maybe I wasn’t clear enough but I want to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks clean nor readable especially for beginners that there is an underscore in the closure. Also beginners often get confused with the number of times some code is run when starting to count from 0 which is also why I think it shouldn’t appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch <mailto:kissling@oberon.ch>>:

(or with a for in loop -- but i guess you have a reason for using .foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Dear Swift-Community,

I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some thousand times. I also like to use the Ruby language and one thing I love about it is that it has some really handy methods integrated to the language in situations like this which make the code very readable and therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods appear in Swift, too and this is the first I came across that I really missed. So I’m asking myself, what if I could write the same code above like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for example by using the .stride method like I used) without any side effects and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

This thread: [swift-evolution] Remove forEach?

— Radek

···

On 18 Dec 2015, at 21:40, Dennis Lysenko via swift-evolution <swift-evolution@swift.org> wrote:

It's a bit tangential but has there been any discussion about inlining closures that anyone is aware of? So you could return from the outer function within a closure, or break the outer loop.

On Fri, Dec 18, 2015, 3:09 PM Cihat Gündüz <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:
@Jacob, @Radek: Seems like you’ve found a clear restriction to the methods usefulness given a simple implementation. You are right, of course. But isn’t that more a sign that Swift needs a way to make closures more useful by adding the possibility of breaking/continueing/returning from within them rather than a disadvantage of the `times`-syntax itself?

I mean, I find the closure-solution useful already. But if break/continue/return would work from within the curly braces somehow (either by a non-closure-based implementation like for-in loops or via a future addition of some kind of strong/weak return etc.) then I agree that it would be even more useful.

Do you think `times` wouldn’t be useful enough with the closure restriction?

Am 18.12.2015 um 20:53 schrieb Jacob Bandes-Storch <jtbandes@gmail.com <mailto:jtbandes@gmail.com>>:

I like how clean "100.times { doSomething() }" looks, but I'm concerned its usefulness will be limited because control-flow statements like break/continue/return won't work from inside a closure.

Jacob

On Fri, Dec 18, 2015 at 11:36 AM, Cihat Gündüz <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Am 18.12.2015 um 20:13 schrieb Félix Cloutier <felixcca@yahoo.ca <mailto:felixcca@yahoo.ca>>:

It doesn't need to be an underscore, but when it is not, the compiler emits an educative warning steering you towards _:

It’s not about the underscore as a character, it’s about the fact that there is the clutter of an underscore at all what I don’t like and what makes me feel the code isn’t as clean as it could be.

/tmp/test.swift:3:7: warning: immutable value 'i' was never used; consider replacing with '_' or removing it

You can also use inclusive ranges instead if you're more comfortable with that: 1...5000 will do just that.

I’m comfortable with ranges but I also used to teach Java back a few years ago and I saw computer science students struggle with the exact number a loop was being executed. So that’s the only reason I brought up that example to have an additional argument.

But again, for me it is more about the clutter that the 1… or 0..< adds to something that could so easily made simpler and more descriptive.

I think this is also a question of: How many convenience methods do we want to see in the Swift standard library? In Ruby, at least, there seemed to be enough people to find this one useful. And it’s the first method I missed until now, so I took that as a sign before suggesting the addition. I also don’t like when there are thousands of convenience methods for things that could easily be written in other ways – but I don’t feel that way with the suggested .times method.

I don't mean to come across as dismissive, and I'm all for an inclusive Swift that you can pick up without knowing advanced concepts. However, there is definitely value in helping people learn, and learning always moves you a little bit out of your comfort zone. When do we remove the training wheels? How long can we hide the fact that indices usually start at 0? How long before you need to iterate an array using the same range-based for loop?

I spend a lot of time on Stack Overflow and I've seen lots of beginners ask for lots of things, but the people who ask about the for loop are usually people with a background in another C-like language who try to use the arguably less readable C-like for loop. I've never seen anyone before say that it looks unclean or unreadable.

I understand what you mean but I don’t think that this is about indices or beginners. The fact that readability and expressiveness make a language easier to learn for beginners IMHO is just a side effect of a well thought-out and developed language. Maybe I wasn’t clear enough but I want to see the .times method in Swift for my own usage, not for beginners. :)

Le 18 déc. 2015 à 13:38:59, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> a écrit :

I agree with both of you about the alternative implementations.

That’s exactly what I’d love to see integrated to the standard library like Ruby is here:
Class: Integer (Ruby 2.2.4)

My main problem is that it neither looks clean nor readable especially for beginners that there is an underscore in the closure. Also beginners often get confused with the number of times some code is run when starting to count from 0 which is also why I think it shouldn’t appear. The .times method would solve both of these problems.

Am 18.12.2015 um 19:33 schrieb Etan Kissling <kissling@oberon.ch <mailto:kissling@oberon.ch>>:

(or with a for in loop -- but i guess you have a reason for using .foreach)

        for _ in 0..<5_000 {
            print("asdf")
        }

On 18 Dec 2015, at 19:31, Etan Kissling via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

You don't need stride for this.

    func foo() {
        (0..<5_000).forEach { _ in
            print("asdf")
        }
    }

On 18 Dec 2015, at 19:25, Cihat Gündüz via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

Dear Swift-Community,

I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

For example recently in one of my projects I wanted to test the scalability of an important piece of code and wrote this method:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            0.stride(to: 5_000, by: 1).forEach { _ in
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

As you can see what I basically wanted was to repeat the test some thousand times. I also like to use the Ruby language and one thing I love about it is that it has some really handy methods integrated to the language in situations like this which make the code very readable and therefore fun to use.

I’m an even bigger fan of Swift so I’d love to see such useful methods appear in Swift, too and this is the first I came across that I really missed. So I’m asking myself, what if I could write the same code above like this:

    func testPerfQualityInPercentWithoutQualityImprovements() {
        self.measureBlock {
            let expectedQuality = 33.33
            5_000.times {
                XCTAssertEqualWithAccuracy(self.crossword.qualityInPercent, expectedQuality, accuracy: 0.1)
            }
        }
    }

I think it could be added to the Swift standard library very easily (for example by using the .stride method like I used) without any side effects and has enough advantages to be part of Swift itself. What do you think?

I wish you all the best,
Cihat

P.S.: This is my very first mail in such a mailing list so I did everything correctly. ^.^

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

Summary of feedback

Cons
Block based API prevents return/break/continue
Can be implemented by an extension without language changes
Vaguely confusable with multiplication

Pros
Is easier for new learners
Removes ambiguity of ..< vs ... (again mostly new learners)
shorter than existing syntax

Alternatives
for _ in 0..< 5_000 (current solution)
repeat 5_000 (alternative syntax)
for 5_000

My $.02

If we are going to introduce a new "something" it should either provide a feature that is currently impossible or it should provide a significantly better way to do something that can already be done. With that as my metric I find 5_000.times to be moderately better at best and does not meet my standard for inclusion.

Andrew Hoos

···

On Dec 19, 2015, at 16:35, Brent Royal-Gordon via swift-evolution <swift-evolution@swift.org> wrote:

I do have an honest question that comes from ignorance rather than malice: has anyone actually used .times in ruby outside of the context of learning or testing?

I've seen it used to retry a failing operation a certain number of times:

  5.times do
    return true if do_network_call
    sleep 5
  end
  raise NetworkCallFailedError

Of course, that requires you to be able to return out of the surrounding function from the `times` loop, which you can do in Ruby but not in Swift.

--
Brent Royal-Gordon
Architechies

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

My personal opinion on this is that 5.times { stuff} offers no benefits over “repeat 5 { stuff }”, so I’d rather see the later (if anything).

This is all shades of gray with no clear answer. We generally want to have standard APIs pay for themselves and avoid confusion. I agree with DaveA’s points upthread. If you contrast it with forEach, forEach (barely!) pays for itself by allowing things like:

  collection.forEach(curriedMethod)

That benefit doesn’t translate to “.times".

-Chris

···

On Dec 18, 2015, at 12:19 PM, Radosław Pietruszewski <radexpl@gmail.com> wrote:

But isn’t that more a sign that Swift needs a way to make closures more useful by adding the possibility of breaking/continueing/returning from within them rather than a disadvantage of the `times`-syntax itself?

Perhaps — there’s a thread, somewhere, with possible solutions to this.

FWIW, I’m just as concerned about allowing returning/etc in closures, as with the lack of this ability. Ruby has different forms of “closures”, far too many of them actually, and some of them truly act like functions (just like in Swift), and in some, return/etc changes the calling function. And… albeit useful… this can be _really_ confusing.

Do you think `times` wouldn’t be useful enough with the closure restriction?

I’d still like it, but it’s just this tiny little thing. Swift standard library is currently very bare-bones, unlike, say, Ruby’s, which has *a ton* of stuff on Arrays, Strings, etc. Unless the Core Team is OK with expanding those standard types with more useful helper methods more broadly, there’s no reason why `times` in particular should go in.

My personal opinion on this is that 5.times { stuff} offers no benefits over “repeat 5 { stuff }”, so I’d rather see the later (if anything).

This is all shades of gray with no clear answer. We generally want to have standard APIs pay for themselves and avoid confusion. I agree with DaveA’s points upthread. If you contrast it with forEach, forEach (barely!) pays for itself by allowing things like:

  collection.forEach(curriedMethod)

That benefit doesn’t translate to “.times".

-Chris

Thanks for weighing in!

The benefit of `5.times` vs `repeat 5` is that the former is (and can easily be) defined in Swift, not as a language-level feature. OTOH `repeat 5` is a bit more useful because it allows returning/breaking/etc.

But I also understand the argument that this feature isn’t worth it at all. (There’s already a lot of libraries extending stdlib with things of this sort!)

— Radek

It’s important to differentiate “not interesting” vs “not appropriate to include in the swift standard library”. I love that you can express things like that directly in the language, and if someone felt compelled to do that in their own code (or in a SPM package someday) that would be fine with me. It just shouldn’t (again, IMO) come with swift out of the box.

-Chris

···

On Dec 18, 2015, at 12:32 PM, Radosław Pietruszewski <radexpl@gmail.com> wrote:

My personal opinion on this is that 5.times { stuff} offers no benefits over “repeat 5 { stuff }”, so I’d rather see the later (if anything).

This is all shades of gray with no clear answer. We generally want to have standard APIs pay for themselves and avoid confusion. I agree with DaveA’s points upthread. If you contrast it with forEach, forEach (barely!) pays for itself by allowing things like:

  collection.forEach(curriedMethod)

That benefit doesn’t translate to “.times".

-Chris

Thanks for weighing in!

The benefit of `5.times` vs `repeat 5` is that the former is (and can easily be) defined in Swift, not as a language-level feature. OTOH `repeat 5` is a bit more useful because it allows returning/breaking/etc.

But I also understand the argument that this feature isn’t worth it at all. (There’s already a lot of libraries extending stdlib with things of this sort!)

I see your points and agree that this is a small feature. My thinking was that it can be added to the standard library pretty easily just because of this without real disadvantages – but it may of course not pay for itself and maybe even cause confusion.

As I stated earlier implementing the method also is about what the standard library should be and what it should not. I learned more about it now and understand that the suggested method may not be appropriate for it. I will probably stick with libraries for a `times` method in the future then.

Thank you very much Radek, Chris and the others for your feedback and consideration!

– Cihat

···

Am 18.12.2015 um 21:37 schrieb Chris Lattner <clattner@apple.com>:

On Dec 18, 2015, at 12:32 PM, Radosław Pietruszewski <radexpl@gmail.com> wrote:

My personal opinion on this is that 5.times { stuff} offers no benefits over “repeat 5 { stuff }”, so I’d rather see the later (if anything).

This is all shades of gray with no clear answer. We generally want to have standard APIs pay for themselves and avoid confusion. I agree with DaveA’s points upthread. If you contrast it with forEach, forEach (barely!) pays for itself by allowing things like:

  collection.forEach(curriedMethod)

That benefit doesn’t translate to “.times".

-Chris

Thanks for weighing in!

The benefit of `5.times` vs `repeat 5` is that the former is (and can easily be) defined in Swift, not as a language-level feature. OTOH `repeat 5` is a bit more useful because it allows returning/breaking/etc.

But I also understand the argument that this feature isn’t worth it at all. (There’s already a lot of libraries extending stdlib with things of this sort!)

It’s important to differentiate “not interesting” vs “not appropriate to include in the swift standard library”. I love that you can express things like that directly in the language, and if someone felt compelled to do that in their own code (or in a SPM package someday) that would be fine with me. It just shouldn’t (again, IMO) come with swift out of the box.

-Chris