Exploring a new language - Swift(Part 1)

Author

Kurian Benoy

Published

March 14, 2020

I usually pick up a new language, every 2 months and explore new things - Kushal Das

Inspired by this I decided to pickup a new language after sometime and blog my experiences with that. The inspiration for me to learn Swift was not IOS development, but for machine learning/DL application. Last year fast.ai guru Jeremy Howards conducted a Deep learning from Foundations course copresented with Chris Lattner-creator of Swift, guy who made LLVM compiler. Inspired by them, I start journey with Swift with a hope to contribute to Tensorflow pretty and learn Tensorflow4 Swift.

Promise of Swift

  1. Infinetely Hackable language - we can go and change even the lowest level in the language, and use these above the hood to get best performance
  2. Similar to Python (print(), no semi colons)
  3. Can reach C level speed for programming
  4. Swift for TensorFlow’s advantages include seamless integration with a modern general-purpose language, allowing for more dynamic and sophisticated models. Fast abstractions can be developed in “user-space” (as opposed to in C/C++, aka “framework-space”), resulting in modular APIs that can be easily customized.
  5. Can change anything even at lowest level, for TPU’s new represntation of float called bfloat16 is needed. You can change the base level implementation of float with Swift using LLVM primitives.

Installation

Before hacking a new language, it’s always necessary to setup your working environment for the language. The installation instructions are covered in Swift docs. Swift can be installed in MacOS and Linux.

Since my operating system is Debian10, as I found the local installation with linux a bit tricky, I installed with Docker.

Docker installation is pretty straightword:

docker pull swift
docker run --security-opt seccomp=unconfined -it swift

Semantics

We use let to make a constant and var to make a variable. The value of a constant doesn’t need to be known at compile time, but you must assign it a value exactly once. This means you can use constants to name a value that you determine once but use in many places.

var myVar = 42
let myconstant = 42

let var:Double = 70 (specifying type to double)

Strings can be added with values in Swift in it’s own special syntax sugar. The value is written in parentheses, and write a backslash (\) before the parentheses. For example:

let apples = 3
let oranges = 5
let appleSummary = "I have \(apples) apples."
let fruitSummary = "I have \(apples + oranges) pieces of fruit."

Arrays

We can easily create arrays and dictionaries using brackets([]), and access their elements by writing the index or key in brackets.

var shoppingList = ["catfish", "water", "tulips"]
shoppingList[1] = "bottle of water"

var occupations = [
    "Malcolm": "Captain",
     "Kaylee": "Mechanic",
]
occupations["Jayne"] = "Public Relations"

You can add new elements to arrays using append:

shoppingList.append("blue paint")

Control flows

We use for-in to iterate over items in a dictionary by providing a pair of names to use for each key-value pair. Dictionaries are an unordered collection, so their keys and values are iterated over in an arbitrary order.

let interestingNumbers = [
    "Prime": [2, 3, 5, 7, 11, 13],
    "Fibonacci": [1, 1, 2, 3, 5, 8],
    "Square": [1, 4, 9, 16, 25],
]
var largest = 0
for (kind, numbers) in interestingNumbers {
    for number in numbers {
        if number > largest {
            largest = number
        }
    }
}
print(largest)

Use while to repeat a block of code until a condition changes. The condition of a loop can be at the end instead, ensuring that the loop is run at least once.

var n = 2
while n < 100 {
    n *= 2
}
print(n)

You can keep an index in a loop by using ..< to make a range of indexes. Very interesting syntax

var total=0
for i in 0..<4 {
    total += i
    }
print(total)

Currently I am reading a book called Swift from Depth to learn more about the language.

Thanks for reading!

Also do check out my first thoughts about Swift4Tensorflow and how to train a NN with Swift article. Till next time C-x s!