Jump to ratings and reviews
Rate this book

Functional Swift: Updated for Swift 4

Rate this book
This book will teach you how to use Swift to apply functional programming techniques to your iOS or OS X projects. These techniques complement object-oriented programming that most Objective-C developers will already be familiar with, providing you with a valuable new tool in your developer's toolbox. We will start by taking a look at Swift's new language features, such as higher-order functions, generics, optionals, enumerations, and pattern matching. Mastering these new features will enable you to write functional code effectively. After that, we will provide several examples of how to use functional programming patterns to solve real-world problems. These examples include a compositional and type-safe API around Core Image, a library for diagrams built on Core Graphics, and a small spreadsheet application built from scratch.

202 pages, Kindle Edition

First published October 1, 2014

33 people are currently reading
214 people want to read

About the author

Chris Eidhof

7 books23 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
47 (36%)
4 stars
49 (37%)
3 stars
26 (20%)
2 stars
7 (5%)
1 star
1 (<1%)
Displaying 1 - 11 of 11 reviews
Profile Image for Alexey Smirnov.
1 review
December 8, 2014
As Swift conquers minds of iOS developers, more and more books will be written about this new programming language. The older books will be forgotten, while the newer ones will be getting attention. The importance of this book, however, will only increase as the time goes by.

What makes me think so is that this book is not just about a new programming language. It is about a new programming paradigm. To put it briefly, the authors' goal is to introduce you to the world of functional programming and to convince you that functional programming is more than just "map" and "filter" functions. It is a new way of looking at your code.

The first chapter gives a very nice example to illustrate the point. Given a convoluted piece of code with many "if" branches, how would one convert it into something more readable? Predicates (those small boolean functions) come to rescue. Since Swift treats functions as first-class citizens, it is possible to create functions dynamically, use them as return values, etc.

The next few chapters introduce you to the concepts of function composition, currying, generics, and optionals. Each concept is illustrated with a code snippet. After introducing basic concept of functional programming, the book moves on to more advanced examples. Be prepared to wrack your brain! Higher intelligence is a must! :-)

The first among the advanced examples is a binary tree data structure, the next one is trie. The style of code samples (written in Swift) starts to remind me of a Lisp class I took in the university...

Mind you, the authors are just warming up! The real crown jewel of the book are chapters 12 and 13 describing the implementation of lexer and parser which was ported from Haskell by one of the authors!

I have to admit, at some point I was ready to throw this book into trash can. The problem was that it took me more than an hour to get through just one page. But after taking a break things got settled in my head, and I was able to move on.

After all, the goal of the book was to CHALLENGE your way of thinking about programming, and the authors achieved it for sure! To summarize, the book really bridges the gap between day-to-day programming and functional paradigm, which was previously a refuge of egghead academics.
Profile Image for Stefan Kanev.
125 reviews235 followers
August 16, 2019
It's a pleasant book to read.

It does a couple of cool things, really:

* It walks the reader through the basic ideas found in most functional programming literature.
* It provides a nice example of functions-as-data that later gets augmented to actual-data-as-data
* It teaches you how to do an implementation of a simple QuickCheck-like library
* Must more importantly (and hence why I really liked this book!), it teaches you how to implement a small parser combinators library. Parser combinators are really exciting!

Of course, it also chooses to dedicate 10 pages on functors, applicative functors and monads, that will only make sense to you if you already know what those things are :/

That being said, it's a good book for any Swift developer that wants to start understanding FP.
Profile Image for pluton.
304 reviews10 followers
June 14, 2018
This is a good introduction book to functional programming in swift. It introduces a number of FP concepts in the swift form: higher-order functions, the benefits of the stronger type system, immutability, and of course functors, applicative functors, and monads at the end. You can read about those concepts in countless articles online and many-many books, so I think the most useful parts of the book are the real-world examples of their application, such as writing your own QuickCheck, wrapping CoreImage, creating an applicative parser combinator library — those are very good.
A small note: there are too many (awkward) swift constructs in the code where generic functions/operators could be implemented and used instead, especially I'd replace those ugly `guard` statements.
Profile Image for Mohannad Hassan.
193 reviews62 followers
March 20, 2016

This book explains how you could use functional programming to improve on what you do. The authors did not engage much in theory, however, they proceeded by examples to explain different aspects of functional programming.


I set my eyes on this book when it was announced, in hopes to find the answers for two questions:

1. What are the core principles of functional programming? What factors can be used to judge a piece of code to adhere to functional programming well?

2. Why should someone switch to / adopt more functional programming?

Despite being referenced many times in the book, both questions were not its main focus. The book focuses on teaching you how to leverage functional programming for the benefit of your programming skills, while using Swift. So the book is most perfect for people who are not waiting for a theoretical approach on the subject, but who are open-minded enough to mix different paradigms and ideas in their practices.



---

I can safely claim that Swift was designed from day one to fully embrace functional programming, and it's comfortably sitting in the that suite behind Haskell along with Scala, F#, OCaml and their sisters. You can go fully functional with it, or you can easily mix it with OOP. Or you could just ignore everything sweet Swift has to offer and just write Objective-C without semicolons.


I'm currently in the midst of a storm, trying to wrap my head around FP, OOP and the right ways to mix both. Some of the FP purists abhor OOP, and declare it the next worst thing after Nazis. As I myself am evolving in OOP, and have just started to understand its principles well, despite having spent almost 8 years in the industry divided between college and work. I can boldly claim that a lot of people don't understand a nickle about what OOP is about. While one of the perks of FP having little following compared to imperative and object-oriented languages, is that it hasn't been plagued with bad practices yet.

In conclusion, I think it's possible to mix FP and OOP in practice. Martin Odersky's Functional Programming Principles in Scala gives some good insight in that.

Profile Image for Rafal Szymanski.
53 reviews12 followers
May 21, 2015
Great into to FP in Swift. You don't need to have any FP knowledge as the book starts from the basics, although it will certainly help if you know what a functor and a monad are. There is a good chapter on parser combinators so if you are familiar with Parsec or Attoparsec you'll get through it much more easily (I would in fact recommend reading the original parser combinator paper by Wadler).
I hope that as the language evolves and improves (proper recursive enums, looking at you) I hope the book improves and adds more real-world examples for iOS (let's be honest, most people will probably use swift for developing iOS apps. More app examples, or maybe an FRP-style game would be cool!)
Profile Image for Yuri Karabatov.
Author 1 book24 followers
January 18, 2016
For someone not directly familiar with functional programming the book gets a bit dense toward the end. Not that the code isn't clear, it is, but some logical leaps are not immediately evident.

Still, liked it a lot. It was also recently updated for the latest Swift version.
Profile Image for Jp.
307 reviews5 followers
January 19, 2017
I'm a big fan of the objc.io guys. When I saw that they've updated for Swift 3, I had to give it another read. I can't stress enough what a great resource they are and how helpful their articles, books, and videos are.
15 reviews4 followers
May 14, 2018
The introductory chapter mentions that "you should be comfortable reading Swift programs…" and yet the first half of the book covers absolute Swift basics. The later chapters is where the meat is. Still I learned something from this book and the writing style, as with all objc.io books, is good.
1 review
May 20, 2016
Impeccable way to make one understand how swift is amazing and interesting.
Displaying 1 - 11 of 11 reviews

Can't find what you're looking for?

Get help and learn more about the design.