More on Fold

This is a followup post to: Functional Swift: Fold it, baby!

The Accumulator Value

A small tip I forgot to mention about the initial accumulator value. Often times you can think of it as the neutral element of the operation you pass to fold. For example in the sum function the operation is addition. To not affect the final solution by the initial accumulator you use the neutral element to addition, which is zero.

More Variants of Fold

Speaking of the initial accumulator value, there is a version of fold that uses the first element of the list as its initial accumulator called foldl1. And these two versions of fold can be reversed so that the list is processed from right to left, foldr and foldr1.


Like foldl but with the first element as the starting accumulator.

func foldl1<A>(list: Array<A>, f: (A

Functional Swift: Fold it, baby!


Pure functional languages don't have the notion of mutable values and therefore don't have any variable assignment. You only have values and expressions, you can give them names, but you can't change the underlying value for a name.

// you get this behaviour in swift, when using let
let n = 42  
n = 7 // immutable value, reassignment is not allowed  

At first this can be cumbersome, because you can't use many familiar structures that you are used to in imperative languages. One of these structures are loops. Think about a for-loop, you need to increment or decrement a control variable to jump out of the loop at one time. Or take a while-loop, you have to change some state so that the boolean expression evaluates to false and you jump out of the while-loop.

But to implement every computable algorithm you need a form of iteration in the language. So how


Swift Optionals

Optionals! What is it good for...

Before we start diving in and look how to use optionals, I think it will be a good start to make clear what problem they solve. I'm sure a lot of people will get mad about the new daily ?-, !-, if let-business just because they don't realize how worse it was in the old Objective-C days.

The old way

Everytime we write a function, declare a property, variable or constant we promise other parts of our code, that it can retrieve a value under this name. But like in the real world we are not always capable of keeping our promises. In the Objective-C days when we promised to return an object-pointer, but weren't able to provide a meaningful value, we returned nil instead. Which was basically a void* pointing to special memory represting a nonexistent object.

nil was kinda great, because


Algebraic Data Types in Swift

What? and Why?

What's an algebraic data type you may ask. Sure you know some algebra like 5 + x = 7. But what has this to do with data types? An algebraic data type is based more on the idea of algebraic structures, which are a set of possible values and one ore more operators to combine a finite number of these values to a single one. A well known structure for example is (ℤ, +, -), the set of all integers with the plus and minus operations on them.

So an algebraic data type is a data type, that is created by algebraic operations. Specifically by sum and product as our operations. See the following example in Haskell for it.

-- product means a combination of values. In this case two Ints
data Point = Point Int Int

-- sum means an alternation between values. True or False, but not both