Machine Learning Newsletter

Learning Swift Part 1

I have recently become interested in the very new programming language as it provides a sweetspot for iOS developers who are tired writing Objective-C.

I spent about 5 hours looking at its syntax and functionality; I must say it feels mostly Scala to me. There are places where syntax is much simpler and sometimes feels like a dynamic programming language; yet it is statically typed language where functions are first class citizens and you could write object oriented programming if you want to; very much like Scala.

I really liked the Playground, it feeld like a REPL and allows you to see the changes and expressions evaluated in real time. For iterative process, it is equivalent of IPython for Python which I use all the time for development.

The things that I did not like; they still somehow need to maintain legacy Objective-c interoperability which makes some of the interfaces quite complicated to deal with, also some of the types are carried to the Swift. The language has been around 2011 and it is new which means it is quite immature in terms of development ecosystem as well as libraries. However, the last one seems likely to disappear as more and more developers opt-in using Swift over Objective-c.

Basic Language Structs

Type Inference

From Javaland, most people complain how verbose it is. There are many reasons for verbosity in java, but one of the most important lacking thing in Java is type inference. You not only need to express the type in the declaration of a variable but also you need to give the type arguments when you are assigning it. When I saw Scala, I thought this is a clear improvement for statically typed languages and Swift follows the same path. When you declare variables or assigning them, you do not need to explicitly write the information, compiler will interfere the type for you.

Explicit Types vs Implicit Types

However, you could always use explicit type:

var euroDollarParity: Double = 0.88
// instead of 
var euroDollarParity = 0.88

var to make variable and let to make it constant

Constants cannot be assigned twice. When you declare the variable as constant, you kind of removed possibility of using variable again.

No Implicit Type Conversion

Strong statically typed languages do not allow you to combine oranges with apples for good reason. Swift(even in string concatenation) follows this convention. You cannot concatenate doubles with strings, you need to explicitly convert the integer or double to String before you concatenate the strings.

let euroDollarSentence = You could buy + String(euroDollarParity) + euro with one dollar

But this is ugly apart from the fact that you need to do explicit conversion, you need to also handle string concatenation. Swift provides a nice formatting for this type of formatting the string:

let euroDollar = You could buy \(euroDollarParity) euro with one dollar

which is much nicer to read and not string conversion.

Arrays and Dictionaries

Apart from Integer, String, Double and all of the primitive data structures, there are dictionary-like structures and array-like structures, which are called dictionary and array. They both use [] but : makes the difference between elements. : signifies key value pair relationship whereas commas would only signify the order in the array.

As in the string, integer and double; you do not need to give the types of key, value pairs of dictionary, or the elements in the array.

var currencyList = [dinar, dollar, franc, lira, peso, pound]
var currencyDict = [dinar: 0.96,
                    franc: 0.21,
                    dollar: 1.00,
                    lira: 0.62,
                    peso: 0.84,
                    pound: 2.31
]

You could initialize the arrays with type, but you do not have to.

var validArray = []
var validDict = [:]

var validArray = [String]()
var validDict = [String: Double]()

Syntax is somehow similar to Python except the {}. If you access a key that does not exist, it does not prompt an error unlike Python. It returns simply nil.

Conditionals

for-in loop behaves exactly in Python except the {}. This time other way around.

for elem in array {

}

where you traverse the array.

Optionality

You could define optional strings as well, based on check, you could check if there is a value or not. Since you cannot pass a string to if block and expect it to be evaluated false(like “” in Python), nil value in Optional provides a nice way to provide such functionality.

Switch Block

let kind=HEARTS

switch kind.lowercaseString {
    case hearts:
        println(kind.uppercaseString) // this will get printed out
    case clubs:
        println(kind.uppercaseString)
    case spades:
        println(kind.uppercaseString)
    case diamonds:
        println(kind.uppercaseString)
default:
    println(WHAT)
}

Functions

First thing is that, functions are first-class citizens. You could pass them around, define one inside of another, write your function which returns a function and so on.

Function signature is almost same with Scala except func and -> to show the return type, other than that type assignment, order are same. Since developers write “Hello World” and fibonacci computation all the time(this is how we make our living after all), let’s see how one could write a fibonacci number computation in Swift.

func fib(n: Int) -> Int {
    if n == 0 || n == 1{
        return 1
    } else {
        return fib(n-1) + fib(n-2)
    }
}

We are defining a function which takes an integer and returns an integer. That was easy.

What if we want to get all the parameters and do something in the function? As long as the types are same, we could use ... in parameter declaration in the function to collect all of the parameters in a list.

func computeStatistics(numbers: Int...) -> (max: Int, min: Int, mean: Double, median: Int) {
    var max = numbers[0]
    var min = numbers[0]
    var sum = 0
    var median: Int
    if numbers.count % 2 == 0{
        median = numbers[numbers.count / 2]
    }else{
        median = numbers[(numbers.count+1) / 2]
    }

    for number in numbers {
        sum += number
        if number >= max{
            max = number
        }else if number <= min {
            min = number
        }
    }
    var mean = Double(sum) / Double(numbers.count)
    return (max, min, mean, median)
}
println(computeStatistics(3, 4, 5, 6, 7))

Assume that the parameters is given in order(for median).

One of the cool things about functions is, you could return tuples(pretty much same with Python and Scala), but you could access both by position AND by name. Isn’t that convenient?

func prime(n: Int) -> (primeNumber:Int, primeList: [Int]) {
    var ii = 0
    var primeNumber = 2
    var primeList = [Int]()
    while ii < n {
        primeNumber++
        if isPrime(primeNumber) {
            ii++
            primeList += [primeNumber]
        }
    }
    return (primeNumber, primeList)
}

var primeN = prime(5)
primeN.primeNumber
primeN.primeList

primeN.0 // same with primeN.primeNumber
primeN.1 // same with primeN.primeList

That is pretty cool.

comments powered by Disqus