Una introducción rápida a la programación reactiva funcional (FRP)

FRP representa una intersección de dos paradigmas de programación. Pero, antes de profundizar en los conceptos, necesitamos saber un poco más sobre algunos términos básicos.

Programación imperativa

Tradicionalmente, escribimos código que describe cómo debería resolver un problema. Cada línea de código se ejecuta secuencialmente para producir un resultado deseado, lo que se conoce como programación imperativa. El paradigma imperativo obliga a los programadores a escribir "cómo" un programa resolverá una determinada tarea. Tenga en cuenta que en la declaración anterior, la palabra clave es "cómo".

He aquí un ejemplo:

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

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.

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.

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?

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.

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:

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)?

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.

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

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

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

You can read more about the topic here

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