Everybody what's going on this is Caleb with slopes slopestyle.
And in this video we're going to talk about one more kind of transformation.
It is the filter function and it is really really cool.
I love filter and I actually use it for a lot of things.
We're going to use it later in this course in the GET hub client that we'll build but for now let's
go ahead and pull open our X playground and let's figure out how it works.
If you think the name sounds like we're going to be basically filtering data.
Yeah that's that's exactly what it is.
So go ahead and.
Right click on flat map click new playground page and we're going to name this filter.
All right go ahead and select all and copy in those lines that we need are x swift playground support
foundation and needs indefinite execution of course like the last video create a Dispose bag by typing
let dispose bag equals a Dispose bag.
OK just like that.
There we go.
And now what we're going to do is we're going to create an observable and we're going to pass in five
different string values.
OK.
We're going to use a filter to basically filter out some of those values and store them in their own
array.
That's what filter does.
Then we're going to subscribe to that observable and print out the values.
You'll see how cool this is and how it works.
So this is going to be an observable that's going to hold some famous CEO names.
So go ahead and type observable dot of and we can pass in multiple elements to observable dot of k.
And what we're going to do is we're going to pass in.
Ilan for Elon Musk we'll pass in Steve for Steve Jobs.
Jeff for Jeff Bezos mark for Mark Price of course and Jack for Jack Ma of Ali Baba.
So we have an observable consisting of five string values that all happened to be CEO names.
Now we can do is we can call DOT filter K and the cool thing with filter is that inside the curly braces
we can set a condition and what it's going to do is it's basically going to look through every element
and it's only going to return values that meet that condition the values that meet that condition are
going to be passed into a new observable only containing values that met the condition you set.
Then of course after that you can subscribe to that and print it and do whatever you want with it.
So what we can do is we can use dollar signs 0 K to capture the value that passes through that's our
temporary variable and we can call anything that can be called upon a string.
So I'm going to use starts with sequence K and my sequence here is going to be J.
I want to pull out all the CEOs with the name J.
That's my condition.
So at the end what I can do is I can append another function.
OK let's actually do some cleanup here let's pull that down.
Let's indent this and indent this.
Now what I can do is I can call DOT subscribe.
OK because now I'm subscribing to the observable that's created by this condition.
I want to call on next because that is what happens.
Every value that is passed in is gonna call in on next call.
OK.
And now I'm going to go ahead and inside of subscribe I'm going to print dollar sign 0 to print out
that observable.
Watch what happens.
I get Jeff and Jack that's it.
It subscribes.
It goes through and prints out every observable of my condition that I set.
I filtered only names that start with J.
How about S.
If I were to do that.
I only get Steve.
How about let's do another condition here maybe contains.
Maybe.
I want it or not.
Sorry it contains.
Well how about contains character.
Let's do of type character.
And we're going to pass in a string value there for the character.
So let's say if it contains an A.
Let's do that with a string.
If the name contains an A and it'll conditionally cycle through here.
There we go.
Sorry.
Sometimes it takes a little while to think and do its thing.
There we go.
So I get Mark and Jack those are both CEO names with a.
How about Withey OK.
I get Steve and Jeff probably thinking what about Ilan.
Well I use the lowercase e.
And so that is where this can be problematic.
You have to set some conditions for capital and lower case.
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
can see this prints out the values for the condition I've set.
I filtered my observable You can also do this with arrays just by the way you can fill it.
You can set conditions and filter elements out of arrays or dictionaries and it's just a really cool
thing to do and are as Swift it's really helpful for parsing through certain bits of information.
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
just return down there.
This is how filter works guys.
This is this is it basically you call filter on an observable or an array or whatever you call filter
at the end and whatever happens whatever condition you set is returned back.
If an item meets that condition then that observable gets passed to subscribe and when the next is called
You can print out the value you can do something with it set it to be a label set it to fill a table
view whatever you want.
You can use then at the end we always add it to our dispose bag.
That's just something we do.
So this is how filter works it's very cool very robust lots of amazing uses for this and it's really
clean as well which is why I like using it.
So we're going to move on to the next video where we're going to learn about the last transformation
that you will learn about in this course called Zip Zip is really cool it's going to let us put two
observables together into one in a new way a little bit of a different way than you have seen already.
So let's head over to the next video and let's do that.