In this lecture, we are going to learn how to simplify our code with operators throughout this section.
We've created an observable pushed data and subscribed to the observable.
In my opinion, the code we've written isn't pleasant to look at.
We are writing a lot of code to accomplish creating an observable.
The R SJS team recommends writing observables declaratively.
In most cases, you will never have to create an observable manually and said there are functions for
handling this process for us.
Overall, we can write cleaner, declarative observables with operators before doing so.
Let's talk about declarative programming.
The words imperative and declarative programming is thrown around a lot.
If you look at the differences between the two, most articles will give you the following definition
Imperative programming focuses on the how, whereas declarative programming focuses on the watch.
It's a great definition, but can be puzzling to understand for beginners to wrap our heads around these
concepts.
Let's look at a metaphor.
A new restaurant is in town.
We may want to try out their food.
However, we may not know how to get there.
A GPS will help us navigate traffic and guide us to our destination.
If the GPS were imperative, we would manually enter each instruction.
It's a ridiculous way to use a GPS.
Imagine if we had to write down every direction in a GPS app with imperative programming.
We are telling the program how it should perform a task.
On the other hand, if the GPS was declarative.
We can instruct the GPS to give us directions.
At most, we would supply it with an address.
We're not concerned with generating instructions.
The GPS would figure out how to navigate to our desired destination.
Let's switch to a programming example.
In this example, we have a function for looping through an array.
It'll calculate the sum of values in the array.
We will be returned a single numeric value.
This function is imperative.
We are giving instructions on how to loop through a value and add these sum to a result.
Here's a declarative approach.
In this example, we are accomplishing the same task, but with one line of code.
Instead of looping through the array, we are calling the reduced function, which will handle looping
through the array.
We are passing in a function to help the reduce function, understand what it should do with each value
in the array.
The reduced function will return the accumulated values in the array.
In my opinion, the declarative approach is much simpler.
It's easier to understand and write.
We should attempt to write declarative code whenever possible.
Instead of telling our program how to perform a task, we can tell it what we want, which is what declarative
programming is all about.
One thing to keep in mind is that declarative programming is not 100 percent achievable.
Underneath declarative programming is an imperative abstraction.
After all, underneath functions are imperative code.
Whenever we call a function for performing an action, the function is likely written with imperative
programming.
The observable we've written is 100 percent imperative.
Channeling the observable how to create an interval push a value and clearly interval.
It's a lot of code to achieve something simple.
Luckily, we can simplify this solution with operators.
An operator is a function.
It's as simple as that.
It isn't necessary to overthink it.
Our SJS exports functions called operators.
The purpose of these operators is to help us control an observable stream of data.
We can mostly avoid imperative programming by using operators.
There are two types of operators creation operators and paperboard operators.
Creation operators are functions that can create an observable.
We can use these operators to create observable from events, requests, intervals, etc. Next, there
are pipe able operators.
There are functions for handling streams of data they accept and observable as input.
New observables are output from the operator.
The output can vary based on the operator.
Pipe cable operators allow us to polish the output before pushing data to the observer.
In the resource section of this lecture, I provide a link to the reference page for our SJS.
On this page, we will find a list of operators, as you can see, there are over 100 operators, they
can help you perform almost any task imaginable.
It can be overwhelming, but don't worry, it's uncommon to use every operator at most.
You will likely use the same five to seven operators.
The rest of the operators are for edge cases.
We've learned the basics of our recharges.
The rest of the lectures for this section will be dedicated to learning operators.
Once we've become comfortable with a few operators, we will go back to our project.