What is the shortest way to run same code n times in Swift?

I have a code that I need to run exactly n times in Swift. What is the shortest possible syntax for that?

I am currently using the for loop but it is a lot of typing.

for i in 0..<n { /* do something */ }

Is there a shorter/nicer way for running same code n times in Swift?

46847 次浏览

In Swift, what you have is the shortest syntax for performing a loop operation.

Swift provides two kinds of loop that perform a set of statements a certain number of times:

The for-in loop performs a set of statements for each item in a sequence.

The for loop performs a set of statements until a specific condition is met.

If you want to run it infinite times, well try using a while.

The only loop shorter than that is an infinite while loop:

while (true) {
}

But you would still have to increase a counter and check it in the loop to break the loop, and in the end it wouldn't be shorter.

Speaking of syntax, you might define your own shortest syntax:

extension Int {
func times(_ f: () -> ()) {
if self > 0 {
for _ in 0..<self {
f()
}
}
}
    

func times(_ f: @autoclosure () -> ()) {
if self > 0 {
for _ in 0..<self {
f()
}
}
}
}


var s = "a"
3.times {
s.append(Character("b"))
}
s // "abbb"




var d = 3.0
5.times(d += 1.0)
d // 8.0

Sticking with a for loop - you could extend Int to conform to SequenceType to be able to write:

for i in 5 { /* Repeated five times */ }

To make Int conform to SequenceType you'll could do the following:

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

You have several ways of doing that:

Using for loops:

for i in 1...n { `/*code*/` }

for i = 0 ; i < n ; i++ { `/*code*/` }

for i in n { `/*code*/` }

using while loops:

var i = 0
while (i < n) {
`/*code*/`
` i++`
}

var i = 0
repeat {
` /*code*/`
`i++`
} while(i <= n)

for-loops are a common way to repeat code. Here is an example of using a for-loop to hide six outlets, versus writing the same code for six outlets. Plus if you make another outlet all you have to do is add it to the array.

let array = [outLet0, outlet1, outlet2, outLet3, outLet4, outLet5]


for outlet in array {
outlet.hidden = true
}

Versus writing it like this:

outlet0.hidden = true
outlet1.hidden = true
outlet2.hidden = true
outlet3.hidden = true
outlet4.hidden = true
outlet5.hidden = true

Shorter and (I think) clearer:

for i in 1...n { } // note: this will fail if n < 1

or

for i in n { }
for _ in 1...5 {
//action will be taken 5 times.
}

ABakerSmith's answer updated for Swift 4:

extension Int: Sequence {
public func makeIterator() -> CountableRange<Int>.Iterator {
return (0..<self).makeIterator()
}
}

Use:

for i in 5 {
//Performed 5 times
}

You could do something like this:

10⨉{ print("loop") }

Using a custom operator and an extension on Int:

infix operator ⨉ // multiplication sign, not lowercase 'x'


extension Int {
static func ⨉( count:Int, block: () ->Void  ) {
(0..<count).forEach { _ in block() }
}
}

you could use functional programming on a range instead of a loop, for shorter and "nicer" syntax for example

(0..<n).forEach{print("Index: \($0)")}

Other answers mention defining your own syntax for that. So - that can be fine for a tiny personal project, or as a learning experience. But defining your own syntax for something so trivial and basic in a large project would be maintenance and readability hell.

There are a lot of answers here, highlighting just how creative you can be, with Swift.

I needed an array so I did this

extension Int {
func of<T>(iteration: (Int) -> T) -> [T] {
var collection = [T]()
for i in 0..<self {
collection.append(iteration(i))
}
return collection
}
}


fun strings() -> [String] {
return 4.of { "\($0) teletubby" }
}

ONLY 5 CHARACTERS (not including n or code)

r(){}

If you're just testing things and need a REALLY short line, try this. Emphasis on using this for testing, not in production, because no one will know what is going on without documentation.

define this somewhere globally

func r(_ n : UInt, _ c: @escaping () -> Void) { for _ in 0..<n { c() } }

call this when you want to run it

r(5) { /*code*/ }