Tips for golfing in Swift

24

4

What are some tips for code-golfing in Swift? Its focus on safety seems to make it difficult to golf in, but that makes little tips and even more useful. Are there any features in Swift that may help it excel in code-golf in certain applications?

Please post one tip per answer.

addison

Posted 2016-05-03T01:09:22.017

Reputation: 993

11You can count on us--if it's meant to be safe, it will be made d̶a̶n̶g̶e̶r̶o̶u̶s̶ golfy! – Conor O'Brien – 2016-05-03T01:21:13.320

9Hopefully some good tips come in swiftly. – James – 2016-05-03T01:27:48.827

4Swift golfing? I thought golf was supposed to be a slow-paced, calm game... – Jojodmo – 2016-05-03T04:29:20.987

Answers

6

Ranges

One thing that is really helpful is creating ranges using the ... or ..< operators

For example

array[0..<n] //first n elements of array
array[k..<n] //kth through nth elements of array
array[k...n] //kth through n-1 elements of array

So, to get the 2nd through 4th values of an array

let myArray = ["ab", "cd", "ef", "gh", "ij", "kl", "mn", "op"]
print(myArray[1..<4]) //["cd", "ef", "gh"]

Practical Use

let a = [3, 1, 4, 1, 5, 9]

Using

for v in a[0...3]{print(v)}

Is 8 bytes shorter than

for n in 0...3{let v=a[n];print(v)}

Jojodmo

Posted 2016-05-03T01:09:22.017

Reputation: 1 569

4…is for n in 0...3{print(a[n])} not valid? – Julian Wolf – 2017-05-11T21:00:19.827

4

Closures:

The use of variables the hold a function vs. using a function itself can help:

65 bytes:

var r:(String,Int)->String={return String(repeating:$0,count:$1)}

66 bytes:

func r(s:String,i:Int)->String{return String(repeating:s,count:i)}

Small difference here, but it'll show more in some puzzles.

Shortening Functions:

Looking at the previous example reminds me of something. Sometimes, if you will be using a function enough times, it is worth the space to rename it:

This:

String(repeating:$0,count:$1)

To this:

var r:(String,Int)->String={return String(repeating:$0,count:$1)}

Or, actually, this is better:

var r=String.init(repeating:count:)

That way you just call r("Hello World",8) instead of String(repeating:"Hello World",count:8)

Leaving Out Type Declarations:

I once created a closure without setting the argument type, thus creating a shorter answer:

var f={(i)->Int in i-1+i%2*2}

The compiler inferred that i is in Int.

Create Arrays the Fast Way:

If you need an array of Ints, use a Range to create it:

Array(0...5)

This does the same thing as:

[0,1,2,3,4,5]

Arrays Instead of If or Switch:

Instead of doing this:

if n==0{return "a"}else if n==1{return "b"}else{return "c"}

You can probably do this:

return ["a","b","c"][n]

Shorten Types:

If you are using type conversion a lot, you might want to create a type alias:

typealias f=Float

Map:

Remember that you often don't need to use the return keyword in the map function.

Running Swift Online:

Although Try It Online does not support Swift It does now!

Caleb Kleveter

Posted 2016-05-03T01:09:22.017

Reputation: 647

2

Thanks for the post. Helped me a lot. https://tio.run/nexus now works with swift :)

– palme – 2019-03-08T15:59:44.597

3

Reducing Arrays

Iterating with for-in loops through an array to get a single value such as the sum of the elements inside, or the product of its elements may be too long for how simple it actually is. You can just use the reduce() method. Some examples:

var array = [1,2,3,4,5,6,7]

Adding up the elements in an array with for-in loops:

var sum = 0

for item in array{
    sum += item
}
print(sum)

can be simplified to:

print(array.reduce(0, +))

And getting the product of the elements inside of the array with for-in loops:

var multiplier = 1
for item in array{
    multiplier *= item
}
print(multiplier)

can also be reduced to:

print(array.reduce(1, *))

Mr. Xcoder

Posted 2016-05-03T01:09:22.017

Reputation: 39 774

Don't you lose more bytes by declaring the ** function than you would by just calling pow manually? – JAL – 2017-03-25T00:47:52.633

@JAL yes, but if you call pow 10 times, it saves a small amount of bytes. Not really the best golfing technique, but it's just another potential help. It was not meant for pow specifically, but for other operations such as searching for prime numbers, if you do that 3 times, it saves a huge amount of bytes – Mr. Xcoder – 2017-03-25T09:27:19.927

3

try

In Swift 2.x and above, functions that traditionally handled errors by passing a pointer to an NSError object as a function parameter now throw their error.

This means that this:

var regex = NSRegularExpression(pattern: "\"((http)s?://.*?)\"", options: nil, error: nil)

now looks like:

do {
    let regex = try NSRegularExpression(pattern: "\"((http)s?://.*?)\"", options: [])
} catch {
    print(error)
}

This can be shortened by using try? or try!. try? will evaluate the expression to nil if an error is thrown. try! will crash your program if an error is thrown, and should be only used in cases where there will never be an error thrown.

let regex = try! NSRegularExpression(pattern: "\"((http)s?://.*?)\"", options: [])

try? and try! save at least 13 bytes from the do-try-catch loop. Note that you also save at least one more byte by passing in an empty array ([]) for options instead of nil as well.

JAL

Posted 2016-05-03T01:09:22.017

Reputation: 304

2

Swift's ternary operator is very terse: condition ? action : otheraction

If the condition is true, do one thing, if not, do something else.

textColor = bgIsBlack ? .white : .black

This makes the textColor white if the background is black, or black if the background is any other color.

The nil coalescing operator is even terser: a ?? b

Let's say you're checking JSON for a certain key so you can present the key's value as title text. If the key isn't present (i.e value is nil), we want to give the title default text.

title = keysValue ?? "Not Available"

Martyav

Posted 2016-05-03T01:09:22.017

Reputation: 61

2

.map()

Combining .map() with trailing closure syntax can tighten up for-loops. We can put the things we want to iterate over into an array, then use .map() to perform some action on each element.

For example, we can use .map() to write that old chestnut, Fizzbuzz, in one line.

var d: [Int] = Array(1...100)

d.map{$0%15 == 0 ? print("Fizzbuzz") : $0%3 == 0 ? print("Fizz") : $0%5 == 0 ? print("Buzz") : print($0)}

Outside of golf, .map() can help cut down on repetition. For example, suppose you have a view you need to position programmatically. You can put the anchors for the view into an anonymous array and run .map() over it to set each constraint's .isActive to true, like so:

_ = [
        view.topAnchor.constraint(equalTo: view.topAnchor, constant: 40),
        view.widthAnchor.constraint(equalTo: view.widthAnchor),
        view.centerXAnchor.constraint(equalTo: view.centerXAnchor),
        view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
    ].map { $0.isActive = true }

Martyav

Posted 2016-05-03T01:09:22.017

Reputation: 61

2Isn't it better to use forEach in your second example? map should really be used for transforming the contents of an array, and not as an iteration shortcut. In this case, you're discarding the result. – JAL – 2017-08-08T20:49:56.280

2

Substring

Sometimes, you can save bytes by falling back onto Foundation types instead of using pure Swift types. Compare accessing a substring of an NSString vs a Swift String type:

let x:NSString = "hello world"
x.substringToIndex(5) // "hello"

let y = "hello world"
y.substringToIndex(y.startIndex.advancedBy(5)) // "hello"

Even with the 9 characters lost by declaring x as an NSString, you save 25 more by using the Foundation type, since substringToIndex takes an Int as a parameter for NSString, vs an Index struct (String.CharacterView.Index) for Swift String types.

I should note that the availability of Foundation types may differ across multiple platforms (OS X, Linux, etc). Most Foundation classes are NSUnimplemented in the open-source version of Swift.

JAL

Posted 2016-05-03T01:09:22.017

Reputation: 304

2

Enumeration

You can chain forEach from enumerated() on a collection type to get a reference to the object (or value type) in a collection, as well as its index:

[1,2,3,4,5].enumerated().forEach{print($0,$1)}

or

for (c,i) in [1,2,3,4,5].enumerated(){print(c,i)}

or (even shorter CountableClosedRange syntax)

(1...5).enumerated().forEach{print($0,$1)}

Prints:

0 1
1 2
2 3
3 4
4 5

JAL

Posted 2016-05-03T01:09:22.017

Reputation: 304

1

Repeating Strings

Unfortunately, Swift does not support String multiplication with *, likewise Python. A good method you can use instead is String(repeating:count:), but unfortunately that's not really golfy. Compare these two approaches:

var a=String(repeating:"abc",count:3)

and

var a="";for _ in 0..<3{a+="abc"}

The second one is a couple of bytes shorter, but that cannot be used in a closure... Better yet, and it also works in closures:

(0..<3).map{_ in"abc"}.joined()

And what if I do it multiple times? Well, you can use String.init(). Now, this may save lots of bytes. For example (68 bytes):

let k=String.init(repeating:count:)
print(k("abcd",9)+k("XYZxyz",9))

instead of (74 bytes):

print(String(repeating:"abcd",count:9)+String(repeating:"XYZxyz",count:9))

or (70 bytes):

var f={String(repeating:$0,count:$1)}
print(f("abcd",9)+f("XYZxyz",9))

But make sure your String is long enough. If you are using String(repeating:"abc",3), it is much better to use "abcabcabc" instead.

Mr. Xcoder

Posted 2016-05-03T01:09:22.017

Reputation: 39 774

+1 because I had no idea variables could be initializers like that. – Daniel – 2017-08-09T19:49:37.123

1

Golfing variable assignments in control flow structures using tuples

Consider you want to use a while loop, and you want to use the same thing in both the condition and the block to follow. Then, an inline assignment in a tuple would most likely help. The longer your attribute, the better! Consider this (3 bytes shorter):

func f(s:[Int]){var k=s,i=0;while(i=k.count,i>0).1{print(i,i+k[i-1]);k.removeLast();}}

over this:

func g(s:[Int]){var k=s,i=0;while k.count>0{i=k.count;print(i,i+k[i-1]);k.removeLast();}}

Notice the (i=k.count,i>0).1 part, which is quite interesting.


Inspired by one of Herman Lauenstein's answers.

Mr. Xcoder

Posted 2016-05-03T01:09:22.017

Reputation: 39 774

1

Import

You can replace import Foundation with import UIKit for 5 bytes shorter, as UIKit does import Foundation already.

Cœur

Posted 2016-05-03T01:09:22.017

Reputation: 401

1Ah you're right. Didn't see that in the review – mbomb007 – 2019-07-18T16:29:34.707