12- Composing Functions
Jul 01, 2026 03:29
· 4:19
· English
· Whisper Turbo
· 2 वक्ता
यो प्रतिलिपि आज म्याद समाप्त हुन्छ ।
स्थायी भण्डारणका लागि स्तरबृद्धि गर्नुहोस् →
देखाउँदै मात्र
0:03
S…
Speaker 1 (12- Composing Functions)
Let me show you how we can compose small functions to build more complex and
0:07
S…
Speaker 1 (12- Composing Functions)
interesting functions.
0:08
S…
Speaker 1 (12- Composing Functions)
So let's say we have a string in this format,
0:12
S…
Speaker 1 (12- Composing Functions)
key, colon,
0:13
S…
Speaker 1 (12- Composing Functions)
value.
0:13
S…
Speaker 1 (12- Composing Functions)
Now I want to pass the string through two transformations.
0:17
S…
Speaker 1 (12- Composing Functions)
First we want to replace the colon with an equal sign.
0:21
S…
Speaker 1 (12- Composing Functions)
So key equals value.
0:24
S…
Speaker 1 (12- Composing Functions)
That is our first transformation.
0:26
S…
Speaker 1 (12- Composing Functions)
Now in the second transformation,
0:29
S…
Speaker 1 (12- Composing Functions)
we want to add braces around the string.
0:32
S…
Speaker 1 (12- Composing Functions)
So second
0:33
S…
Speaker 1 (12- Composing Functions)
going to add curly braces and get something
0:38
S…
Speaker 1 (12- Composing Functions)
like this.
0:38
S…
Speaker 1 (12- Composing Functions)
So we're going to declare two small functions and then we're going to compose
0:42
S…
Speaker 1 (12- Composing Functions)
them to build a larger,
0:44
S…
Speaker 1 (12- Composing Functions)
more complex function.
0:45
S…
Speaker 1 (12- Composing Functions)
So first we declare a function of
0:49
S…
Speaker 1 (12- Composing Functions)
string and string.
0:51
S…
Speaker 1 (12- Composing Functions)
We call it replace colon
0:55
S…
Speaker 1 (12- Composing Functions)
and set it to a lambda expression like this.
0:59
S…
Speaker 1 (12- Composing Functions)
str goes to str .replace.
1:02
S…
Speaker 1 (12- Composing Functions)
We want to replace the colon with
1:07
S…
Speaker 1 (12- Composing Functions)
an equal sign,
1:08
S…
Speaker 1 (12- Composing Functions)
like this.
1:09
S…
Speaker 1 (12- Composing Functions)
Now the second function,
1:12
S…
Speaker 1 (12- Composing Functions)
so function of string and string,
1:15
S…
Speaker 1 (12- Composing Functions)
let's call this add braces,
1:19
S…
Speaker 1 (12- Composing Functions)
and set it to a lambda like this.
1:21
S…
Speaker 1 (12- Composing Functions)
str goes to,
1:24
S…
Speaker 1 (12- Composing Functions)
first we add the left brace,
1:26
S…
Speaker 1 (12- Composing Functions)
then we add our string,
1:27
S…
Speaker 1 (12- Composing Functions)
and then we add the right brace.
1:29
S…
Speaker 1 (12- Composing Functions)
Pretty straightforward,
1:31
S…
Speaker 1 (12- Composing Functions)
right?
1:31
S…
Speaker 2 (12- Composing Functions)
Now,
1:32
S…
Speaker 1 (12- Composing Functions)
there are two ways to compose functions.
1:35
S…
Speaker 1 (12- Composing Functions)
Here's one way.
1:36
S…
Speaker 1 (12- Composing Functions)
We start the replace colon function,
1:39
S…
Speaker 1 (12- Composing Functions)
then we call and then,
1:42
S…
Speaker 1 (12- Composing Functions)
exactly like how we chained multiple consumer objects.
1:45
S…
Speaker 1 (12- Composing Functions)
Here we should pass another function,
1:48
S…
Speaker 1 (12- Composing Functions)
so we pass add braces,
1:50
S…
Speaker 1 (12- Composing Functions)
now this returns a new function,
1:53
S…
Speaker 1 (12- Composing Functions)
we can store it somewhere,
1:54
S…
Speaker 1 (12- Composing Functions)
or we can directly call the apply method,
1:58
S…
Speaker 1 (12- Composing Functions)
pass our string,
1:59
S…
Speaker 1 (12- Composing Functions)
key colon value,
2:01
S…
Speaker 1 (12- Composing Functions)
and then get the result.
2:03
S…
Speaker 1 (12- Composing Functions)
As simple as that.
2:05
S…
Speaker 1 (12- Composing Functions)
So let's print it,
2:07
S…
Speaker 2 (12- Composing Functions)
and
2:11
S…
Speaker 1 (12- Composing Functions)
There you go.
2:12
S…
Speaker 1 (12- Composing Functions)
That's what we get.
2:12
S…
Speaker 1 (12- Composing Functions)
Beautiful.
2:13
S…
Speaker 2 (12- Composing Functions)
Now,
2:16
S…
Speaker 1 (12- Composing Functions)
once again, this is an example of declarative programming.
2:20
S…
Speaker 1 (12- Composing Functions)
So instead of writing instructions to specify how we should do
2:24
S…
Speaker 1 (12- Composing Functions)
something, we express what needs to be done.
2:27
S…
Speaker 1 (12- Composing Functions)
The details are somewhere else.
2:29
S…
Speaker 1 (12- Composing Functions)
So we have a bunch of small reusable functions and we're composing
2:33
S…
Speaker 1 (12- Composing Functions)
them to build something more complex.
2:34
S…
Speaker 1 (12- Composing Functions)
Now,
2:35
S…
Speaker 1 (12- Composing Functions)
when writing code like this,
2:37
S…
Speaker 1 (12- Composing Functions)
it's better to reformat our code like this.
2:40
S…
Speaker 1 (12- Composing Functions)
So we put
2:43
S…
Speaker 1 (12- Composing Functions)
each statement on a new line.
2:45
S…
Speaker 1 (12- Composing Functions)
That reads better.
2:46
S…
Speaker 1 (12- Composing Functions)
So replace colon,
2:48
S…
Speaker 1 (12- Composing Functions)
and then add braces,
2:49
S…
Speaker 1 (12- Composing Functions)
finally apply.
2:51
S…
Speaker 1 (12- Composing Functions)
Now we also have another method called compose.
2:55
S…
Speaker 1 (12- Composing Functions)
Compose is similar to and then,
2:58
S…
Speaker 1 (12- Composing Functions)
but it changes these functions in the reverse order.
3:01
S…
Speaker 1 (12- Composing Functions)
So if you want to achieve the same result using the compose method,
3:04
S…
Speaker 1 (12- Composing Functions)
we should write code like this.
3:06
S…
Speaker 1 (12- Composing Functions)
We start with the second function,
3:08
S…
Speaker 1 (12- Composing Functions)
add braces,
3:10
S…
Speaker 1 (12- Composing Functions)
we compose this with the first function,
3:14
S…
Speaker 1 (12- Composing Functions)
replace colon and then we apply key value
3:20
S…
Speaker 1 (12- Composing Functions)
to get the result and then print it.
3:22
S…
Speaker 2 (12- Composing Functions)
Take a look.
3:24
S…
Speaker 1 (12- Composing Functions)
We get the same result as before.
3:27
S…
Speaker 1 (12- Composing Functions)
So there are two ways to compose functions.
3:31
S…
Speaker 1 (12- Composing Functions)
Now if you're curious how these work,
3:33
S…
Speaker 1 (12- Composing Functions)
put the carrot over here on the top from the navigate menu go
3:37
S…
Speaker 1 (12- Composing Functions)
to declaration or use the shortcut.
3:40
S…
Speaker 1 (12- Composing Functions)
So here's the implementation
3:44
S…
Speaker 1 (12- Composing Functions)
of the and then method.
3:46
S…
Speaker 1 (12- Composing Functions)
So this method takes a function
3:48
S…
Speaker 1 (12- Composing Functions)
and it returns another function.
3:50
S…
Speaker 2 (12- Composing Functions)
Now,
3:52
S…
Speaker 1 (12- Composing Functions)
in the body of this function,
3:53
S…
Speaker 1 (12- Composing Functions)
first we make sure that the argument is not null,
3:56
S…
Speaker 1 (12- Composing Functions)
and here we return another function that is represented using a
4:00
S…
Speaker 1 (12- Composing Functions)
lambda expression,
4:00
S…
Speaker 1 (12- Composing Functions)
so we have this parameter of type t,
4:03
S…
Speaker 1 (12- Composing Functions)
first we call the apply method on this argument,
4:06
S…
Speaker 1 (12- Composing Functions)
we get the result,
4:07
S…
Speaker 1 (12- Composing Functions)
and then pass it to the apply method of the second function.
4:11
S…
Speaker 1 (12- Composing Functions)
Now the compose method is very similar,
4:14
S…
Speaker 1 (12- Composing Functions)
but it composes these functions in the reverse order.
यो प्रतिलिपि AI (स्वचालित वक्तव्य पहिचान) द्वारा उत्पन्न गरिएको थियो । त्रुटि समावेश गर्न सक्छ - महत्वपूर्ण प्रयोगका लागि मौलिक अडियोसँग रुजु गर्नुहोस् । AI नीति
सारांश
यो लिखितको AI सारांश उत्पन्न गर्न सारांश गर्नुहोस् क्लिक गर्नुहोस् ।
सारांश गर्दैछ...
यो प्रतिलिपि बारे AI सोध्नुहोस्
यो transcript बारेमा केही सोध्नुहोस् - एआई सम्बन्धित खण्डहरू र जवाफ पाउनुहुनेछ.