1 00:00:00,150 --> 00:00:06,570 In this lecture, we were going to learn how to accumulate values with the Reduce operator, the Reduce 2 00:00:06,570 --> 00:00:10,590 operator is the counterpart to the array reduce function. 3 00:00:10,950 --> 00:00:16,200 If you're familiar with the reduced function on arrays, understanding the Reduce operator will be a 4 00:00:16,200 --> 00:00:16,890 breeze. 5 00:00:17,340 --> 00:00:22,200 In some cases, we may want to accumulate the values emitted from an observable. 6 00:00:22,500 --> 00:00:25,350 The Reduce operator accomplishes this action. 7 00:00:25,890 --> 00:00:29,400 Let's say our observable emitted the values one through five. 8 00:00:29,670 --> 00:00:32,220 The observer may want the sum of these values. 9 00:00:32,580 --> 00:00:38,760 Instead of letting the observer handle this computation, we can add the reduced function to our pipeline 10 00:00:39,150 --> 00:00:41,050 every time a value is emitted. 11 00:00:41,280 --> 00:00:45,780 The Reduce operator will take in the value until the next value is emitted. 12 00:00:46,230 --> 00:00:53,280 Once the observable completes the reduce operator, it will push a value onto the observer throughout 13 00:00:53,280 --> 00:00:54,690 this entire process. 14 00:00:54,780 --> 00:00:57,270 The observer will receive a single value. 15 00:00:57,570 --> 00:01:01,770 We can modify how values are accumulated by passing in a function. 16 00:01:02,100 --> 00:01:05,190 In this example, we are adding the values together. 17 00:01:05,550 --> 00:01:08,190 Let's dive into the arguments through our code. 18 00:01:09,680 --> 00:01:12,920 For this example, let's create a stream of numbers. 19 00:01:13,160 --> 00:01:17,780 This stream will be reduced to a single value with the Reduce operator. 20 00:01:18,050 --> 00:01:22,670 First, we will update the import statement for the creation operators. 21 00:01:22,940 --> 00:01:25,280 We will import the of operator. 22 00:01:27,720 --> 00:01:34,680 As we learned, the Earth operator will allow us to create a synchronous stream of data next completely 23 00:01:34,680 --> 00:01:38,430 replace the from event operator with the operator. 24 00:01:40,960 --> 00:01:44,410 The other operator will limit numbers one through five. 25 00:01:46,920 --> 00:01:53,400 As it stands, the numbers will be emitted one by one, we can intercept each value with the reduce 26 00:01:53,400 --> 00:01:55,320 operator to accumulate them. 27 00:01:55,650 --> 00:02:02,490 Let's give it a try by importing the Reduce operator from the Oryx SJS slash operators package. 28 00:02:04,890 --> 00:02:09,720 In the pipeline, replace the current operators with the Reduce operator. 29 00:02:12,240 --> 00:02:17,550 The Reduce operator has two arguments, which are a function and seed value. 30 00:02:17,790 --> 00:02:22,140 Let's pass in an empty function and a seed value of zero. 31 00:02:24,630 --> 00:02:28,770 The seed value is optional, but typically you will want to add it. 32 00:02:29,070 --> 00:02:31,770 This value refers to the starting value. 33 00:02:32,040 --> 00:02:36,060 The goal is to get the sum of the values emitted from the observable. 34 00:02:36,390 --> 00:02:39,180 Our starting value should start at zero. 35 00:02:39,480 --> 00:02:42,060 New numbers are added to the seed value. 36 00:02:42,450 --> 00:02:46,650 Next, we can start accumulating the values through the arrow function. 37 00:02:46,980 --> 00:02:52,250 This function will be supplied with two arguments called accumulator and value. 38 00:02:54,690 --> 00:03:00,870 The accumulator argument stores the value previously returned by this function, if this function is 39 00:03:00,870 --> 00:03:06,180 being called for the first time, the accumulator argument will store the seed value. 40 00:03:06,450 --> 00:03:11,550 As for the value argument, it references the value emitted by the observable. 41 00:03:11,760 --> 00:03:15,420 We must return a value since we're trying to get the sum. 42 00:03:15,480 --> 00:03:17,850 We all have these two arguments together. 43 00:03:20,390 --> 00:03:27,170 In the end, we will receive the sum in the console, the behavior of the reduced operator is entirely 44 00:03:27,170 --> 00:03:29,040 different from other operators. 45 00:03:29,300 --> 00:03:34,610 Typically, each time a value is pushed, the value would get lost in the console. 46 00:03:34,910 --> 00:03:41,780 However, in this scenario, the reduced operator does not push each value emitted from the observable. 47 00:03:42,080 --> 00:03:45,010 It's waiting until the observable has been completed. 48 00:03:45,620 --> 00:03:52,190 After the observable completes, the reduce operator will push the value it has accumulated over time. 49 00:03:52,550 --> 00:03:55,340 This operator does introduce some problems. 50 00:03:55,640 --> 00:03:58,580 What if the observable it's reading from doesn't complete? 51 00:03:58,820 --> 00:04:03,050 Let's look at how we can tackle this scenario in the following lecture.