Without actually escaping

6 minute read

When dealing with lazy sequences and generic functions, there are subtle pitfalls you need to be aware of. You could easily negate the laziness and end up drastically slowing down your algorithms.

Repeating sequence

less than 1 minute read

Today I had the need to generate an infinite repeating sequence from a fixed ordered collection of elements. That is, from the items a, b, c, I needed an infinite sequence like a, b, c, a, b, c, a, b, c, ....

Compact and flat maps

8 minute read

Although Swift 4.1 brought many improvements to Swift, it was intended to be source compatible with Swift 4.0, so most improvements where purely additive. But one notable exception was the rename from flatMap to compactMap. I always thought Swift’s old flatMap on arrays and other sequences was somewhat of an annoyance, as it never really was a proper flat map.

Keypaths as closures

3 minute read

Swift 4.0 added support for strongly typed key paths. A KeyPath<A, B> is a type that defines a path from a root type A, through zero or more hoops, to a resulting type B through a series of named accessors. It’s a way of accessing a type’s properties indirectly.

Nib-backed Collection Views

5 minute read

When working with collection views (and table views) in UIKit, you have to implement a data source which feeds the collection view with cells. Since a collection view often has hundreds (or even thousands) of cells, but only a few are visible on screen at a time, UIKit provides a mechanism for reusing cells as they scroll offscreen and new cells scroll into view. And although UIKit provides such a mechanism, the API is somewhat lacking. And it certainly isn’t very swifty.