Want to learn about functional reactive programming (FRP) on iOS? We created this guide to help everyone, from beginners to seasoned experts, learn how to leverage the power of FRP on iOS. In this guide, you’ll learn:
- Why you’d want to want to use FRP
- What FRP is
- How far can you can go with the power of FRP
- How to use FRP
- Who to follow to learn more about FRP
Why would I want to use FRP?
FRP lets you reason about your program more effectively. Instead of worrying about state changing all over the place, you’ve contained the ways in which it’s allowed to change. Instead of worrying about which parts of your application might be affected, you’ve also constrained the sites that care about any one change, and you’re guaranteed that they can handle changes well. Instead of having to think about synchronous and asynchronous changes as two totally separate data flows, you can just handle them the same way, so there’s less code you have to write. Overall, FRP lets you worry less about managing your data, and just concentrate on how your app should work.
Get more development news like this
These speakers did a great job of convincing us that we should be using FRP, so we know you’ll be convinced.
So what is FRP?
FRP stands for Functional Reactive Programming. As the name suggests, it’s a combination of two programming styles, functional programming & reactive programming.
Functional programming includes concepts like immutable data (data that can’t be changed) & using functions with no side effects. One of the major goals of functional programming is to get rid of side effects, which usually make code much harder to reason about, since things can happen without an obvious cause. However, it can be difficult to use functional programming in user-driven applications, since data does need to change at times.
Reactive programming focuses on data flows. Reactive programming thinks of data as flows of information over time, rather than standalone events. A helpful analogy is a pipe, where data can come out periodically as it’s ready, and someone might be there to handle that data. One of the major benefits of reactive programming is that synchronous (happens immediately) & asynchronous (happens at some point soon) code are treated the same way, which makes sporadic things like user input and networking much easier to reason about.
FRP takes the best parts of these two philosophies and smashes them together. From functional programming, FRP gets no side effects, & from reactive programming, FRP gets one single way to think about data. So FRP connects the parts of your code using streams (aka pipes) of immutable data. Once you connect everything, then data can flow smoothly around your app, with no danger of accidentally affecting or being affected by any of the rest of your code.
All that sounds great in theory, but it’s definitely hard to grasp just by reading about it. So let’s have some examples!
How far can I take FRP?
Once you get comfortable thinking in this way (which can take a while, so don’t feel bad if it’s hard to really get into!), you can start using the same techniques on a larger scale in your apps. FRP experts write FRP-style UI code so that they’re safe from weird edge cases, & connect whole sections of their applications through the FRP-style streams. The videos below give you some idea for where you can go, once you start to harness the power of FRP.
How do I use FRP?
Just want to try out FRP? If you’re interested in playing around, the best guide we’ve found is the Examples page from RxSwift. They show you line by line how to do a few tasks using that library.
Looking for a sample app? We wrote a tutorial on building a FRP-style app in Swift. Start by opening Xcode, end with a full app!
If you’re interested in choices, there are a few common libraries, each of which has their own styles & choices, all with documentation. We’ve checked them all out, and these are the ones that are alive, supported, and moving forward. Here are the links to their guides to get started:
If you’re interested in longer content, the folks behind objc.io wrote a whole book about Functional Swift. It doesn’t touch on the Reactive side very much, but it’s great for an in-depth look at the Functional side.
Who can teach me more about FRP?
If you’d like to keep learning about FRP (and we hope you do!), here are some people you could follow. They all are great at FRP & write(/tweet) about it often.
- Justin Spahr-Summers
- Josh Abernathy
- Rob Rix
- Andy Matuschak
- Robb Böhnke
- Ash Furrow
- Chris Eidhof
- Javier Soto
- Nacho Soto
- Colin Eberhardt
- Maximilian Alexander
We’ll keep this up to date with the best content available anywhere. Got suggestions for this guide? Let us know on Twitter!