| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
chenzhen05
9年前发布

学习关于iOS中的functional reactive programming (FRP)

来自: https://realm.io/news/frp-ios-guide/

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
  • How far can you can go with the power of FRP
  • Who to follow to learn more about FRP

Get more guides, videos, & tutorials — we won’t email you for any other reason, ever.

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.

These speakers did a great job of convincing us that we should be using FRP, so we know you’ll be convinced.

Ash Furrow does a nice (& enthusiastic!) job covering why FRP can make your programming experience much more fun.

Agnes Vasarhelyi shows a real-world example that makes it easy to imagine using FRP in a smaller app. Cheers to FRP! :beer:

So what is FRP?

FRP stands for F unctional R eactive P rogramming. 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!

Here's a great video for beginners: Max Alexander gives a good overview of FRP. If you're not familiar with the concepts, this will teach you everything you need to know. He also covers some of the basics of a common library called RxSwift.

For the Swiftier among you, here's a talk by Colin Eberhardt that covers how Swift has improved APIs for ReactiveCocoa (another common library).

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.

If this live coding session by Nacho Soto can't convince you how easy-to-use FRP can be, nothing will!

Two of the most talked-about FRP-inspired frameworks have come out of 非死book. Hannes Verlinde covers why 非死book made Component Kit & React Native.

Benji Encz kicks off a whole architectural project, which aims to bring Redux (a popular JavaScript FRP framework) to Swift. This has the potential to make your view code way easier to work with (plus enable time travel, which you can't say about every library).

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.

We’ll keep this up to date with the best content available anywhere. Got suggestions for this guide? Let us know on 推ter !

</div>

 本文由用户 chenzhen05 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1456886788093.html
函数式编程 iOS开发 Objective-C开发