快速入门react_功能React式编程(FRP)快速入门

快速入门react

by Navdeep Singh

通过Navdeep Singh

功能React式编程(FRP)快速入门 (A quick introduction to Functional Reactive Programming (FRP))

FRP represents an intersection of two programming paradigms. But, before we dig deeper into the concepts, we need to know a bit more about some basic terms.

FRP代表两种编程范例的交集。 但是,在深入研究概念之前,我们需要了解一些基本术语。

命令式编程 (Imperative programming)

Traditionally, we write code that describes how it should solve a problem. Each line of code is sequentially executed to produce a desired outcome, which is known as imperative programming. The imperative paradigm forces programmers to write “how” a program will solve a certain task. Note that in the previous statement, the keyword is “how.”

传统上,我们编写描述其应如何解决问题的代码。 每行代码被顺序执行以产生期望的结果,这被称为命令式编程。 命令式范式迫使程序员编写“如何”完成某项任务的程序。 请注意,在前面的语句中,关键字是“ how”。

Here’s an example:

这是一个例子:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]var numbersLessThanFive = [Int]()for index in 0..<numbers.count     {    if numbers[index] < 5         {        numbersLessThanFive.append(numbers[index])        }    }

As you can see, we sequentially execute a series of instructions to produce a desired output.

如您所见,我们依次执行一系列指令以产生所需的输出。

功能编程 (Functional programming)

Functional programming is a programming paradigm where you model everything as a result of a function that avoids changing state and mutating data. We will discuss concepts such as state and data mutability and their importance in subsequent sections, but for reference:

函数式编程是一种编程范例,您可以在其中对所有事物进行建模,这些函数是避免避免更改状态和更改数据的函数的结果。 在接下来的部分中,我们将讨论状态和数据可变性等概念及其重要性,但仅供参考:

  • consider state as one of the different permutations and combinations that your program can have at any given time during its execution

    状态视为程序执行期间任何给定时间可以具有的不同排列和组合之一

  • data mutability is the concept where a given dataset might change over a given course of time during program execution.

    数据可变性是一个概念,其中给定的数据集在程序执行过程中可能会在给定的时间范围内发生变化。

The same example that was given using imperative programming can be used in the following way using the functional approach:

使用命令式编程给出的相同示例可以通过功能方法以以下方式使用:

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]let numbersLessThanFive = numbers.filter { $0 < 5 }

We feed the filter function with a closure containing a certain criterion. That criterion is then applied to each element in the numbers array, and the resulting array contains elements that satisfy our criteria.

我们为过滤器函数提供一个包含特定条件的闭包。 然后将该条件应用于numbers数组中的每个元素,并且结果数组包含满足我们条件的元素。

Notice the declaration of the two arrays in both the examples.

注意两个示例中两个数组的声明。

In the first example, the numbersLessThanFive array was declared as a var, whereas in the second example, the same array was declared as a let.

在第一个示例中, numbersLessThanFive数组被声明为var ,而在第二个示例中,同一数组被声明为let

Does it ring some bells?

它会响铃吗?

Which approach is better, which array is safer to work with?

哪种方法更好,哪种数组更安全?

What if more than one thread is trying to work with the same array and its elements?

如果有多个线程尝试使用同一数组及其元素怎么办?

Isn’t a constant array more reliable?

常数数组不是更可靠吗?

React式编程 (Reactive programming)

Reactive programming is the practice of programming with asynchronous data streams or event streams. An event stream can be anything like keyboard inputs, button taps, gestures, GPS location updates, accelerometer, and iBeacon. You can listen to a stream and react to it accordingly.

React式编程是使用异步数据流或事件流进行编程的实践。 事件流可以是诸如键盘输入,按钮轻击,手势,GPS位置更新,加速度计和iBeacon之类的任何内容。 您可以收听流,并对其做出相应的React。

You might have heard about reactive programming, but it might have sounded too intimidating, scary, or cryptic to even try out. You might have seen something like this:

您可能听说过React式编程,但听起来可能过于吓人,令人恐惧或难以理解,甚至无法尝试。 您可能已经看到了以下内容:

var twoDimensionalArray = [ [1, 2], [3, 4], [5, 6] ]let flatArray = twoDimensionalArray.flatMap { array in    return array.map { integer in        return integer * 2    }}print(flatArray)Output : [2, 4, 6, 8, 10, 12]

At first glance, the preceding code might feel a bit obscure, and this might be the reason you turned your back on this style of programming. Reactive programming, as we mentioned earlier, is programming with event streams.

乍一看,前面的代码可能会有点晦涩,这也许就是您拒绝这种编程风格的原因。 正如我们前面提到的,响应式编程是使用事件流进行编程。

However, the bigger question still remains unanswered. What is functional reactive programming (FRP)?

但是,更大的问题仍然没有答案。 什么是功能React式编程(FRP)?

FRP is the combination of functional and reactive paradigms. In other words, it is reacting to data streams using the functional paradigm. FRP is not a utility or a library — it changes the way you architect your applications and the way you think about your applications.

FRP是功能范式和React范式的组合 。 换句话说,它正在使用功能范式对数据流做出React。 FRP不是实用程序或库,它会改变您设计应用程序的方式以及您对应用程序的看法。

In the next blog I will talk about basic building blocks of reactive programming — till then stay tuned and enjoy reading:)

在下一个博客中,我将讨论React式编程的基本构建块-在此之前,请继续关注并喜欢阅读:)

To have a solid grasp over reactive concepts and write iOS applications in RxSwift you can read my book: Reactive programming in Swift 4.

要深入了解React式概念并在RxSwift中编写iOS应用程序,可以阅读我的书: Swift 4中的React式编程

More of my projects and downloadable code are in my public github repos

我的更多项目和可下载代码在我的公共github仓库中

You can read more about the topic here

您可以在此处阅读有关该主题的更多信息

Thanks for reading, please share it if you found it useful :)

感谢您的阅读,如果您觉得有用,请分享:)

翻译自: https://www.freecodecamp.org/news/functional-reactive-programming-frp-imperative-vs-declarative-vs-reactive-style-84878272c77f/

快速入门react

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值