I’m +1. It’s not something that’s super common, but `100.times { … }` expresses
the intention far better than the rather cryptic `for _ in 0..<100 { … }`.
The only possible concern — and this has been expressed in the proposal to
remove `forEach` — is that closures have different semantics than `for`.
Mostly, if someone wants to return from a containing function, you can’t do
that from a closure.
— Radek
> On 18 Dec 2015, at 19:38, Cihat Gündüz via swift-evolution
> <[email protected]> wrote:
>
> 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:
> http://ruby-doc.org/core-2.2.4/Integer.html#method-i-times
> <http://ruby-doc.org/core-2.2.4/Integer.html#method-i-times>
>
> 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 <[email protected]
>> <mailto:[email protected]>>:
>>
>> (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
>>> <[email protected] <mailto:[email protected]>> 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
>>>> <[email protected] <mailto:[email protected]>> 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
>>>> [email protected] <mailto:[email protected]>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>
>>> _______________________________________________
>>> swift-evolution mailing list
>>> [email protected] <mailto:[email protected]>
>>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>> <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>
>
> _______________________________________________
> swift-evolution mailing list
> [email protected] <mailto:[email protected]>
> https://lists.swift.org/mailman/listinfo/swift-evolution
> <https://lists.swift.org/mailman/listinfo/swift-evolution>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution