Seven Basics of Functional Programming in Swift

I finally purchased a copy of Functional Programming in Swift by Chris Eidhof, Florian Kugler, and Wouter Swierstra. And this post is the first scribble in my learning journal (which can also be found as a Gist).
// one: use Swift's closure syntax to write a function as a value, either specifying type or being explicit about the values to be passed within the function
let squared:Int -> Int =  {$0 * $0}
let squared = {(v:Int) in return v * v}

// two: you use the name of the variable or constant as the method name
squared(10) // 100

// three: functions are copied when assigned to new variables
var square = squared
square(10) // 100

// four: a variable function can have it's value changed as long as it is consistent with its original type
square = { $0 * $0 * $0}

// five: a function can return a function
let cubing: () -> (Int -> Int) = {{$0 * $0 * $0}}
let cube = cubing()
cube(2) // 8

// six: the function that returns a function can have an ongoing effect on the function that is returned
let cubingMultiplier: Int -> (Int -> Int) = {v in {$0 * $0 * $0 * v}}
let cubedMultipliedByThree = cubingMultiplier(3)
cubedMultipliedByThree(2) // 24

// seven: to tidy things up we can give function types meaningful names using typealias
typealias sodaPop = Int -> Float
let bubbles:sodaPop = {Float($0)}

// eight: this one's not so basic, but shows how you can take a function not written as a value and still utilise it
typealias HaveFun = Int -> (Int -> Int)
func preExistingFunc(num:Int) -> Int { return num * num }
let myFunction:HaveFun = {v in { preExistingFunc($0*v) }}
let newFunc = myFunction(10)
newFunc(10) // 10000
I have no Haskell or Scala knowledge and up until now everything I know about functional programming has arrived to me filtered through the Swift community in blogs, on twitter and in talks. So this is the first proper attempt I've made to understand the direction that functional programming is coming from by starting to read a whole book about the subject.

The commented code featured here was written after reading 'Chapter 2: Thinking Functionally'. It takes the ideas written there but moves a bit slower and for me provides a lead up to the code included in the book covering a few areas that I wasn't entirely clear on and which I had to clarify by writing my own code.

Concluding thoughts

The starting point that really helps me in thinking about defining functions as values is to see how similar the function syntax is to the closure syntax when written like this,
func regularFunction(num:Int) -> Int {return num * num}
let closureFuction = {(num:Int) in return num * num}
regularFunction(2)
closureFuction(2)
before entering into the more abbreviated ways of writing closures:
let closureFuctionWithType:Int -> Int = {$0 * $0}
closureFuctionWithType(2)
And while I agree with the authors that the use of typealias helps us to think of functions as regular constants and variables, at the same time this reasoning doesn't happen immediately and we do need to keep some grip on what is happening by writing out in full as well (at least I do).


Endorse on Coderwall

Comments