Basic building blocks of Functional reactive programming(FRP): IOS

Navdeep Singh
10 min readMar 13, 2018
Control your code’s Side Effects

Every massive structure has basic units that come together and make sense to the whole structure. For example, bricks, cement, paint, concrete, and so on make up basic building blocks on a building. Similarly, before we go ahead and discuss the massive field of functional reactive programming, it would be great if we understood the basic building blocks that will come together so that the massive applications that we create make sense.Let’s have a look at some of the basic building blocks of FRP, starting with event streams.

Event streams

An event stream can be defined as a sequence of events happening over time; you can think of it as an asynchronous array. A simple description of an event stream is shown in the following diagram:

As you can see, we have expressed time on the arrow aligned from left to right, moving forward to the right, and events happen over time. The coloured bubbles (indicated by their names) drawn at intermittent intervals on the timeline represent the events. We can add an event listener to the whole sequence and whenever an event happens, WE CAN REACT TO IT by doing something; that’s the main idea!

We have numerous other types of sequence in Swift, for instance, arrays:

Suppose we have an eventStream array:

var eventStream = ["1", "2", "abc",  "3", "4", "cdf", "6"]

Let’s try to compare eventStream to an array; arrays are sequences in space, which means all the items in the eventStream array now exist in memory; on the other hand, eventStreams don’t have that property. Events might happen over time and you won’t even know all the items that might happen and when will they happen.

So, if we have to relate between an array and event stream, then we can assert that if the [“1”, “2”, “abc”, “3”, “4”, “cdf”, “6”] values happen over a period of time and don’t just exist in memory from scratch, the preceding array will act like an event stream where an event “1” might happen at 1st second, event “2” may happen at the 4th second, event “abc”

Navdeep Singh

Author of Reactive programming with Swift, Engineering Manager — Exploring possibilities with new Tech.