1 00:00:00,150 --> 00:00:06,990 In this lecture, we are going to learn how to simplify our code with operators throughout this section. 2 00:00:07,140 --> 00:00:12,600 We've created an observable pushed data and subscribed to the observable. 3 00:00:12,930 --> 00:00:16,800 In my opinion, the code we've written isn't pleasant to look at. 4 00:00:17,130 --> 00:00:21,150 We are writing a lot of code to accomplish creating an observable. 5 00:00:21,840 --> 00:00:26,160 The R SJS team recommends writing observables declaratively. 6 00:00:26,430 --> 00:00:33,090 In most cases, you will never have to create an observable manually and said there are functions for 7 00:00:33,090 --> 00:00:35,010 handling this process for us. 8 00:00:35,250 --> 00:00:41,700 Overall, we can write cleaner, declarative observables with operators before doing so. 9 00:00:41,820 --> 00:00:44,400 Let's talk about declarative programming. 10 00:00:45,510 --> 00:00:50,160 The words imperative and declarative programming is thrown around a lot. 11 00:00:50,550 --> 00:00:56,190 If you look at the differences between the two, most articles will give you the following definition 12 00:00:56,610 --> 00:01:03,510 Imperative programming focuses on the how, whereas declarative programming focuses on the watch. 13 00:01:04,200 --> 00:01:10,740 It's a great definition, but can be puzzling to understand for beginners to wrap our heads around these 14 00:01:10,740 --> 00:01:11,580 concepts. 15 00:01:11,730 --> 00:01:13,260 Let's look at a metaphor. 16 00:01:14,070 --> 00:01:15,840 A new restaurant is in town. 17 00:01:16,080 --> 00:01:18,150 We may want to try out their food. 18 00:01:18,450 --> 00:01:21,120 However, we may not know how to get there. 19 00:01:21,450 --> 00:01:26,250 A GPS will help us navigate traffic and guide us to our destination. 20 00:01:27,120 --> 00:01:31,920 If the GPS were imperative, we would manually enter each instruction. 21 00:01:32,220 --> 00:01:34,860 It's a ridiculous way to use a GPS. 22 00:01:35,130 --> 00:01:41,850 Imagine if we had to write down every direction in a GPS app with imperative programming. 23 00:01:42,000 --> 00:01:45,720 We are telling the program how it should perform a task. 24 00:01:46,350 --> 00:01:49,980 On the other hand, if the GPS was declarative. 25 00:01:50,190 --> 00:01:53,430 We can instruct the GPS to give us directions. 26 00:01:53,730 --> 00:01:56,820 At most, we would supply it with an address. 27 00:01:57,030 --> 00:01:59,940 We're not concerned with generating instructions. 28 00:02:00,270 --> 00:02:05,070 The GPS would figure out how to navigate to our desired destination. 29 00:02:05,700 --> 00:02:08,160 Let's switch to a programming example. 30 00:02:08,400 --> 00:02:12,270 In this example, we have a function for looping through an array. 31 00:02:12,900 --> 00:02:15,850 It'll calculate the sum of values in the array. 32 00:02:16,260 --> 00:02:19,350 We will be returned a single numeric value. 33 00:02:19,680 --> 00:02:21,660 This function is imperative. 34 00:02:21,930 --> 00:02:27,810 We are giving instructions on how to loop through a value and add these sum to a result. 35 00:02:28,470 --> 00:02:30,450 Here's a declarative approach. 36 00:02:30,690 --> 00:02:35,760 In this example, we are accomplishing the same task, but with one line of code. 37 00:02:36,210 --> 00:02:42,210 Instead of looping through the array, we are calling the reduced function, which will handle looping 38 00:02:42,210 --> 00:02:43,200 through the array. 39 00:02:43,680 --> 00:02:50,100 We are passing in a function to help the reduce function, understand what it should do with each value 40 00:02:50,100 --> 00:02:50,880 in the array. 41 00:02:51,420 --> 00:02:56,070 The reduced function will return the accumulated values in the array. 42 00:02:56,730 --> 00:03:00,420 In my opinion, the declarative approach is much simpler. 43 00:03:00,660 --> 00:03:02,850 It's easier to understand and write. 44 00:03:03,210 --> 00:03:06,780 We should attempt to write declarative code whenever possible. 45 00:03:07,170 --> 00:03:14,010 Instead of telling our program how to perform a task, we can tell it what we want, which is what declarative 46 00:03:14,010 --> 00:03:15,690 programming is all about. 47 00:03:16,260 --> 00:03:22,170 One thing to keep in mind is that declarative programming is not 100 percent achievable. 48 00:03:22,560 --> 00:03:26,760 Underneath declarative programming is an imperative abstraction. 49 00:03:27,120 --> 00:03:30,840 After all, underneath functions are imperative code. 50 00:03:31,200 --> 00:03:37,470 Whenever we call a function for performing an action, the function is likely written with imperative 51 00:03:37,470 --> 00:03:38,220 programming. 52 00:03:40,080 --> 00:03:43,920 The observable we've written is 100 percent imperative. 53 00:03:44,430 --> 00:03:50,070 Channeling the observable how to create an interval push a value and clearly interval. 54 00:03:50,520 --> 00:03:53,220 It's a lot of code to achieve something simple. 55 00:03:53,520 --> 00:03:57,540 Luckily, we can simplify this solution with operators. 56 00:04:00,010 --> 00:04:01,750 An operator is a function. 57 00:04:01,990 --> 00:04:03,340 It's as simple as that. 58 00:04:03,670 --> 00:04:05,980 It isn't necessary to overthink it. 59 00:04:06,400 --> 00:04:10,210 Our SJS exports functions called operators. 60 00:04:10,510 --> 00:04:16,000 The purpose of these operators is to help us control an observable stream of data. 61 00:04:16,390 --> 00:04:20,560 We can mostly avoid imperative programming by using operators. 62 00:04:21,130 --> 00:04:26,590 There are two types of operators creation operators and paperboard operators. 63 00:04:27,190 --> 00:04:31,270 Creation operators are functions that can create an observable. 64 00:04:31,720 --> 00:04:40,120 We can use these operators to create observable from events, requests, intervals, etc. Next, there 65 00:04:40,120 --> 00:04:41,950 are pipe able operators. 66 00:04:42,220 --> 00:04:47,800 There are functions for handling streams of data they accept and observable as input. 67 00:04:48,130 --> 00:04:51,460 New observables are output from the operator. 68 00:04:51,790 --> 00:04:54,610 The output can vary based on the operator. 69 00:04:54,940 --> 00:05:00,790 Pipe cable operators allow us to polish the output before pushing data to the observer. 70 00:05:01,120 --> 00:05:07,540 In the resource section of this lecture, I provide a link to the reference page for our SJS. 71 00:05:08,930 --> 00:05:16,400 On this page, we will find a list of operators, as you can see, there are over 100 operators, they 72 00:05:16,400 --> 00:05:19,700 can help you perform almost any task imaginable. 73 00:05:20,240 --> 00:05:26,330 It can be overwhelming, but don't worry, it's uncommon to use every operator at most. 74 00:05:26,540 --> 00:05:30,230 You will likely use the same five to seven operators. 75 00:05:30,500 --> 00:05:33,470 The rest of the operators are for edge cases. 76 00:05:34,030 --> 00:05:36,530 We've learned the basics of our recharges. 77 00:05:36,770 --> 00:05:41,420 The rest of the lectures for this section will be dedicated to learning operators. 78 00:05:41,690 --> 00:05:47,060 Once we've become comfortable with a few operators, we will go back to our project.