1
00:00:08,030 --> 00:00:10,980
Everybody what's going on this is Caleb with slopes slopestyle.

2
00:00:11,050 --> 00:00:14,920
And in this video we're going to talk about one more kind of transformation.

3
00:00:14,950 --> 00:00:18,670
It is the filter function and it is really really cool.

4
00:00:18,670 --> 00:00:21,770
I love filter and I actually use it for a lot of things.

5
00:00:21,940 --> 00:00:26,950
We're going to use it later in this course in the GET hub client that we'll build but for now let's

6
00:00:26,950 --> 00:00:30,520
go ahead and pull open our X playground and let's figure out how it works.

7
00:00:30,550 --> 00:00:34,180
If you think the name sounds like we're going to be basically filtering data.

8
00:00:34,210 --> 00:00:35,860
Yeah that's that's exactly what it is.

9
00:00:35,860 --> 00:00:37,530
So go ahead and.

10
00:00:37,570 --> 00:00:43,570
Right click on flat map click new playground page and we're going to name this filter.

11
00:00:43,600 --> 00:00:50,410
All right go ahead and select all and copy in those lines that we need are x swift playground support

12
00:00:50,410 --> 00:00:56,860
foundation and needs indefinite execution of course like the last video create a Dispose bag by typing

13
00:00:56,860 --> 00:01:00,900
let dispose bag equals a Dispose bag.

14
00:01:01,560 --> 00:01:02,710
OK just like that.

15
00:01:02,710 --> 00:01:03,780
There we go.

16
00:01:04,180 --> 00:01:09,880
And now what we're going to do is we're going to create an observable and we're going to pass in five

17
00:01:09,880 --> 00:01:11,490
different string values.

18
00:01:11,650 --> 00:01:12,210
OK.

19
00:01:12,460 --> 00:01:18,250
We're going to use a filter to basically filter out some of those values and store them in their own

20
00:01:18,250 --> 00:01:19,010
array.

21
00:01:19,060 --> 00:01:20,580
That's what filter does.

22
00:01:20,710 --> 00:01:24,830
Then we're going to subscribe to that observable and print out the values.

23
00:01:24,910 --> 00:01:27,010
You'll see how cool this is and how it works.

24
00:01:27,010 --> 00:01:31,140
So this is going to be an observable that's going to hold some famous CEO names.

25
00:01:31,180 --> 00:01:41,430
So go ahead and type observable dot of and we can pass in multiple elements to observable dot of k.

26
00:01:41,590 --> 00:01:43,550
And what we're going to do is we're going to pass in.

27
00:01:43,570 --> 00:01:48,650
Ilan for Elon Musk we'll pass in Steve for Steve Jobs.

28
00:01:48,670 --> 00:01:58,010
Jeff for Jeff Bezos mark for Mark Price of course and Jack for Jack Ma of Ali Baba.

29
00:01:58,390 --> 00:02:05,320
So we have an observable consisting of five string values that all happened to be CEO names.

30
00:02:05,320 --> 00:02:13,270
Now we can do is we can call DOT filter K and the cool thing with filter is that inside the curly braces

31
00:02:13,510 --> 00:02:19,960
we can set a condition and what it's going to do is it's basically going to look through every element

32
00:02:20,320 --> 00:02:27,940
and it's only going to return values that meet that condition the values that meet that condition are

33
00:02:27,940 --> 00:02:34,630
going to be passed into a new observable only containing values that met the condition you set.

34
00:02:34,720 --> 00:02:39,390
Then of course after that you can subscribe to that and print it and do whatever you want with it.

35
00:02:39,580 --> 00:02:45,760
So what we can do is we can use dollar signs 0 K to capture the value that passes through that's our

36
00:02:45,760 --> 00:02:50,160
temporary variable and we can call anything that can be called upon a string.

37
00:02:50,160 --> 00:02:56,900
So I'm going to use starts with sequence K and my sequence here is going to be J.

38
00:02:56,920 --> 00:03:00,250
I want to pull out all the CEOs with the name J.

39
00:03:00,890 --> 00:03:02,300
That's my condition.

40
00:03:02,360 --> 00:03:06,350
So at the end what I can do is I can append another function.

41
00:03:06,350 --> 00:03:09,270
OK let's actually do some cleanup here let's pull that down.

42
00:03:09,290 --> 00:03:11,620
Let's indent this and indent this.

43
00:03:11,630 --> 00:03:14,930
Now what I can do is I can call DOT subscribe.

44
00:03:15,330 --> 00:03:20,590
OK because now I'm subscribing to the observable that's created by this condition.

45
00:03:20,930 --> 00:03:23,790
I want to call on next because that is what happens.

46
00:03:23,810 --> 00:03:29,120
Every value that is passed in is gonna call in on next call.

47
00:03:29,390 --> 00:03:29,760
OK.

48
00:03:29,780 --> 00:03:35,720
And now I'm going to go ahead and inside of subscribe I'm going to print dollar sign 0 to print out

49
00:03:35,720 --> 00:03:36,550
that observable.

50
00:03:36,560 --> 00:03:37,720
Watch what happens.

51
00:03:37,940 --> 00:03:40,290
I get Jeff and Jack that's it.

52
00:03:40,340 --> 00:03:41,150
It subscribes.

53
00:03:41,150 --> 00:03:46,510
It goes through and prints out every observable of my condition that I set.

54
00:03:46,520 --> 00:03:49,010
I filtered only names that start with J.

55
00:03:49,010 --> 00:03:49,760
How about S.

56
00:03:49,790 --> 00:03:50,690
If I were to do that.

57
00:03:52,440 --> 00:03:53,880
I only get Steve.

58
00:03:53,880 --> 00:03:59,080
How about let's do another condition here maybe contains.

59
00:03:59,240 --> 00:03:59,640
Maybe.

60
00:03:59,730 --> 00:04:00,870
I want it or not.

61
00:04:00,900 --> 00:04:03,080
Sorry it contains.

62
00:04:03,960 --> 00:04:05,640
Well how about contains character.

63
00:04:05,700 --> 00:04:07,950
Let's do of type character.

64
00:04:07,950 --> 00:04:12,400
And we're going to pass in a string value there for the character.

65
00:04:13,380 --> 00:04:18,830
So let's say if it contains an A.

66
00:04:19,040 --> 00:04:20,280
Let's do that with a string.

67
00:04:20,570 --> 00:04:26,510
If the name contains an A and it'll conditionally cycle through here.

68
00:04:26,930 --> 00:04:30,120
There we go.

69
00:04:30,240 --> 00:04:30,570
Sorry.

70
00:04:30,570 --> 00:04:33,960
Sometimes it takes a little while to think and do its thing.

71
00:04:33,960 --> 00:04:34,410
There we go.

72
00:04:34,410 --> 00:04:38,120
So I get Mark and Jack those are both CEO names with a.

73
00:04:38,130 --> 00:04:42,050
How about Withey OK.

74
00:04:42,070 --> 00:04:44,860
I get Steve and Jeff probably thinking what about Ilan.

75
00:04:44,980 --> 00:04:47,220
Well I use the lowercase e.

76
00:04:47,410 --> 00:04:50,650
And so that is where this can be problematic.

77
00:04:50,650 --> 00:04:53,830
You have to set some conditions for capital and lower case.

78
00:04:54,010 --> 00:05:02,410
But as you can see when we go back in time here and undo what I just did do starts with J again as you

79
00:05:02,410 --> 00:05:07,010
can see this prints out the values for the condition I've set.

80
00:05:07,030 --> 00:05:12,990
I filtered my observable You can also do this with arrays just by the way you can fill it.

81
00:05:13,000 --> 00:05:19,120
You can set conditions and filter elements out of arrays or dictionaries and it's just a really cool

82
00:05:19,120 --> 00:05:23,440
thing to do and are as Swift it's really helpful for parsing through certain bits of information.

83
00:05:23,620 --> 00:05:29,920
So of course at the end we need to add it to the Dispose bag do that every time and we'll go ahead and

84
00:05:29,920 --> 00:05:31,520
just return down there.

85
00:05:31,570 --> 00:05:33,340
This is how filter works guys.

86
00:05:33,420 --> 00:05:39,730
This is this is it basically you call filter on an observable or an array or whatever you call filter

87
00:05:39,730 --> 00:05:46,050
at the end and whatever happens whatever condition you set is returned back.

88
00:05:46,090 --> 00:05:52,390
If an item meets that condition then that observable gets passed to subscribe and when the next is called

89
00:05:52,420 --> 00:05:57,880
You can print out the value you can do something with it set it to be a label set it to fill a table

90
00:05:57,880 --> 00:05:58,830
view whatever you want.

91
00:05:58,830 --> 00:06:03,690
You can use then at the end we always add it to our dispose bag.

92
00:06:03,910 --> 00:06:05,070
That's just something we do.

93
00:06:05,080 --> 00:06:10,810
So this is how filter works it's very cool very robust lots of amazing uses for this and it's really

94
00:06:10,810 --> 00:06:13,300
clean as well which is why I like using it.

95
00:06:13,300 --> 00:06:18,040
So we're going to move on to the next video where we're going to learn about the last transformation

96
00:06:18,520 --> 00:06:23,260
that you will learn about in this course called Zip Zip is really cool it's going to let us put two

97
00:06:23,260 --> 00:06:28,810
observables together into one in a new way a little bit of a different way than you have seen already.

98
00:06:28,810 --> 00:06:31,730
So let's head over to the next video and let's do that.
