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

Swift

Swift Problem Overview


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?

Swift Solutions


Solution 1 - Swift

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

Solution 2 - Swift

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()
    }
}

Solution 3 - Swift

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)

Solution 4 - Swift

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

Solution 5 - Swift

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.

Solution 6 - Swift

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() }
	}
}

Solution 7 - Swift

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
}

Solution 8 - Swift

Shorter and (I think) clearer:

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

or

for i in n { }

Solution 9 - Swift

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.

Solution 10 - Swift

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" }
}

Solution 11 - Swift

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

Solution 12 - Swift

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.

Solution 13 - Swift

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*/ }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvgeniiView Question on Stackoverflow
Solution 1 - SwiftMatteo PiomboView Answer on Stackoverflow
Solution 2 - SwiftABakerSmithView Answer on Stackoverflow
Solution 3 - SwiftFlavio SilverioView Answer on Stackoverflow
Solution 4 - SwiftAndrew Paul SimmonsView Answer on Stackoverflow
Solution 5 - SwiftGreatWizView Answer on Stackoverflow
Solution 6 - SwiftnielsbotView Answer on Stackoverflow
Solution 7 - SwiftSunkasView Answer on Stackoverflow
Solution 8 - SwiftjglasseView Answer on Stackoverflow
Solution 9 - SwiftAli GajaniView Answer on Stackoverflow
Solution 10 - SwiftJonesyView Answer on Stackoverflow
Solution 11 - SwiftCaleb KleveterView Answer on Stackoverflow
Solution 12 - SwiftBadmintonCatView Answer on Stackoverflow
Solution 13 - SwiftquemefulView Answer on Stackoverflow