Custom Enumerated Sequences in Just 1 Line of Code
In "Creating a Cheap Protocol-Oriented Copy of SequenceType (with a Twist!)" I created my own indexedEnumerate()
to return a special kind of enumeration: instead of a tuple of (Int, Element)
, I wanted to have a tuple of (Index, Element)
, where Index
was a UInt starting at 1. A custom CollectionType
can have a different type of index than Int
, so I was quite disappointed that the default enumerate()
simply returns a sequence with a counter for the elements at first.
Replacing Loops with Mapped Ranges
During the last weeks, I tried to use map()
even when the conversion wasn't straightforward to learn how it affects reading code. I found that some applications of map()
were dumb while I liked how others turned out to read. The functional programming paradigm is about writing code in a declarative way, which has its benefits.
Creating a Lens for an Object to Provide a New Public Interface
I'm back from my vacation and very deep into programmin already. Before I left, my subconscious mind brought up the notion of a Lens (functional programming) while I was modelling tabular data in Swift and I think this is a very cool approach to keep value types clean and easy to test. I use lenses in my current project to provide a specialized interface to operate with the table. The lenses provide access to column and row iterators, independent of the table's underlying data structure. This way the table doesn't have to worry about all this stuff.
Encapsulate a Process in a Single Line Using Bind and Good OO Design
Watch Saul Mora's AltConf presentation "Object-Oriented Functional Programming: The Best of Both Worlds!" to learn more about real world use of bind
and other functional concepts in object-oriented programming. His examples are really good.
Where Instead of Using Functional Bind, I Create an Expressive Model

The other day, I wrote a post about bind()
and the >>=
operator and how it can help chain function calls together. The example was a bit too contrived and making it fit the requirements left us with really bad code. I came up with an even better implementation: use plain Swift objects and express your intent carefully.
From East-Oriented Programming to Functional Bind

East-Oriented programming can, for example, be implemented through delegates or callback blocks. "East" is all about Tell, Don't Ask: don't query for values; instead, pass a handler around so the flow of information doesn't return to your current scope.
Library for Providing Static UITableView Contents in Swift
I discovered a Swift library called Static the other day. It helps set up and fill UITableView
s with static content – that is, it doesn't permit deletion or rearrangement of new rows into the table. I think the following example to configure sections with rows reads really nice:
Return Types can Capture Async Processes and Failures

I've written about using the Result enum to return values or errors already. Result
guards against errors in the process. There's also a way to guard against latency, often called "Futures". There exist well-crafted solutions in Swift already.
Functional Programming in Swift (YouTube Videos)

After the latest change in my diet, I eat a lot more often throughout the day and try to spend the time watching educational talks to make use of my time. Functional programming seems to not only be all the hype – its concepts seem to reach mainstream thinking, too. Here's a collection of talks you might find worth your while.
- Functional Swift – conference recordings with tons of great stuff
- Erik Meijer: What does it mean to be Reactive? – taught me a lot about being explicit and honest in my return types
- Neal Ford: Functional Thinking – how to use data transformations instead of a series of instructions
- Erik Meijer: Functional Programming – shows what being stateful entails
If you got some useful talks, share them in the comments!
Going Beyond Guard Clauses in Swift

Erica Sadun brought up a topic which is bugging me for some time already. It's how Swift encourages you to handle optionals. It's related to handling exceptions, which I covered earlier. Either you use if-let
to unwrap them implicitly and do all the stuff inside the nested code block, or you end up with code which is a lot wordier than I'd like it to be.
Functional Error Handling in Swift Without Exceptions
In Swift, there's no exception handling or throwing. If you can't use exceptions for control flow like you would in Java, what else are you going to do if you (a) write library code which executes a failing subroutine, and (b) you find unwrapping optionals too cumbersome? I played with the thought of keeping Swift code clean, avoiding the use of optionals, while maintaining their intent to communicate failing operations.
Thinking in Terms of Functional Programming Encourages Clean Factoring of Code
Chris Eidhof provided an excellent example for creating a mini Swift networking library. It executes simple requests and can be extended to work with JSON (or XML, if you must) without any change. The accompanying talk is worth watching, too. You should check it out to see well-factored Swift code in action. It's a great example.