Functional Reactive Programming on iOS with ReactiveCocoa

Whiteboarding some ReactiveCocoa

Note: This is going to be a slightly more technical post geared toward our friends in the iOS developer community.

Objective-C is a programming language which often finds itself mired in the antiquated ways of C, the programming language upon which modern Objective-C is built. While improvements in computing power have advanced the state of art in programming language design, Objective-C sometimes seems stuck in the past.

Objective-C and C are imperative programming languages where software developers provide statements that the computer executes in sequence. Behaviour emerges from executing these instructions. If a developer has written the correct instructions in the correct order, then the behaviour that emerges should satisfy the requirements of the program.

However, the instructions often have flaws. We use manual and automated testing to mitigate the risk of these flaws, but it would be better if we could abstract away the individual instructions and focus solely on the desired behaviour. That’s where declarative programming comes in.

The imperative paradigm forces developers to write how a program will solve some task. The declarative paradigm frees developers to describe whatthe task is.

ReactiveCocoa is an approach to making Objective-C less imperative and more declarative. It abstracts much of the how and focuses on the what.

Let’s take a look at a few examples to see what a declarative program in Objective-C might look like.

At the core of ReactiveCocoa is a signal. A signal represents a stream of events that occur over time. Subscribing to a signal allows developers to access those events. Let’s take a look at a basic example.

A text field in an iOS app can provide a signal which produces events when its text changes. ReactiveCocoa provides a category on UITextField with the function rac_textSignal, which we can subscribe to.

Here, we’ve declared that when the text field’s text changes, its new value is logged. Whenever the text field’s signal emits an event, the block is executed and passed the updated value.

The nice thing about signals is that they can be composed. Let’s filter the signal returned by rac_textSignal to make sure that the length of the string is at least three characters before we log it.

The filter: returns a new signal. When the first signal emits an event, the value of that event is passed to the filter block. If the block returns YES, then the new signal emits an event. The subscription is to the signal returned byfilter:.

Let’s do something more complex. Let’s combine two signals from two different text fields and reduce their values into a boolean which we thenbind to the enabled property of a button.

The enabled property of the button is always derived from the latest signal values from the two text fields. This represents one of the core components of FRP: deriving state.

In each of these cases, we make declarations once in viewDidLoad and the statements stay true for the duration of the application runtime. There are no delegate methods to implement or state to store. Behaviour is explicitly declared instead of implicitly inferred.

FRP can get much more complex, and learning the nuts-and-bolts of ReactiveCocoa takes time. However, this time is an investment that will yield more stable programs with predictable, well-defined behaviour.

We’ve seen throughout computing history that software development tends toward higher levels of abstraction. We no longer, for instance, deal with punchcards or assemblers. I believe that FRP represents another level of abstraction which programmers should leverage to build better applications, faster.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
函数响应式编程 Functional Reactive Programming by Blackheath, Jones 2016 | ISBN: 1633430103 | English | 360 pages | PDF | 10 MB Summary Functional Reactive Programming teaches the concepts and applications of FRP. It offers a careful walk-through of core FRP operations and introduces the concepts and techniques you'll need to use FRP in any language. Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications. About the Technology Today's software is shifting to more asynchronous, event-based solutions. For decades, the Observer pattern has been the go-to event infrastructure, but it is known to be bug-prone. Functional reactive programming (FRP) replaces Observer, radically improving the quality of event-based code. About the Book Functional Reactive Programming teaches you how FRP works and how to use it. You'll begin by gaining an understanding of what FRP is and why it's so powerful. Then, you'll work through greenfield and legacy code as you learn to apply FRP to practical use cases. You'll find examples in this book from many application domains using both Java and JavaScript. When you're finished, you'll be able to use the FRP approach in the systems you build and spend less time fixing problems. What's Inside Think differently about data and events FRP techniques for Java and JavaScript Eliminate Observer one listener at a time Explore Sodium, RxJS, and Kefir.js FRP systems About the Reader Readers need intermediate Java or JavaScript skills. No experience with functional programming or FRP required. About the Authors Stephen Blackheath and Anthony Jones are experienced software developers and the creators of the Sodium FRP library for multiple languages. Foreword by Heinrich Apfelmus. Illustrated by Duncan Hill. Table of Contents Stop listening! Core FRP Some everyday widget stuff Writing a real application New concepts FRP on the web Switch Operational primitives Continuous time Battle of the paradigms Programming in the real world Helpers and patterns Refactoring Adding FRP to existing projects Future directions

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值