How Function Composition Can Make Your Code Better | Functional Programming

preview_player
Показать описание

Function composition is a simple concept: you combine a few smaller functions to produce a more complicated function. An example is passing the results of a small function as an argument to a complex function. This is a common concept in functional programming, and I'll show you how it can make your code better.

Join my weekly .NET newsletter:

Read my Blog here:

Subscribe for more:

Chapters
0:00 What are we trying to do?
1:29 Defining the first function
2:48 Defining the second function
5:27 Implementing function composition
7:56 Another approach to function composition
Рекомендации по теме
Комментарии
Автор

The second approach is super clean, removes all the boilerplate that comes from static methods. I very much appreciate your videos about functional programming in C#, keep up the good work.

soverain
Автор

Great Milan, you explanation was very clear.

adiazwise
Автор

Very nice and clean. That would be usefull in many cases, thanks.

krzysztofhandzlik
Автор

Thank you Milan. I prefer the second one because it's more readable and we can passed many validations at once.

birukayalew
Автор

Thanks Milan, you awesome
I hope you talk about Vertical Slice Architecture and compare it with Clean Architecture.

Tamer_Ali
Автор

Hi Milan. Another great video.
I have one additional suggestion.
If the email is null or empty, we need to check on all predicates against it, otherwise we get null reference error.
Instead of repeating the check, I would advise to add the check in the Ensure method like this, and the call stays the same as you showed.
public static Result<T> Ensure<T>(
T value,
(Func<T, bool> Predicate, Error Error) hasValue,
params (Func<T, bool> Predicate, Error Error)[] functions)
{
if (!hasValue.Predicate(value))
{
return Failure<T>(hasValue.Error);
}
...
}
What do you think?

andreibicu
Автор

I like both options, but would like more examples using class Rusults. I want more practice. Thanks

angelldark
Автор

Hello Milan, where to get resources to write a FP code such you.

mahmmoudkinawy
Автор

i like the idea of having an array of tuples one question, would it not be more functional had you use Aggregate instead of foreach? since you are creating a new type, then folding over the array of tuples you could then pass in the value t, to each function?

gardnerjens
Автор

hello thank you its was useful, can i ask you how i can learn how i create my own extenision result ?
and Function Composition?

darwish
Автор

Hey there! On your thumbnail ... composition is spelled wrong ... to me it shows as "comopsition"

EdsonViniciusSchmitt
Автор

I like the 2nd approach more. Looks much cleaner and has less code

antonmartyniuk
Автор

how could i get the ResultExtensions class?

AmirHashemZadeh
Автор

Thoughts on making videos showing how to build good WPF applications?

alberthalbert
Автор

You are getting closer and closer to F# :D

able
Автор

About the second approach, it is rather unoptimized, firstly because the parameter is a params array object, in the function you can check the length of it and create an array of results to match the length, and use a for loop instead of the foreach, then pass the array to the combine method. This will entirely eliminate the List allocation, because you are already allocating a new array with the ToArray method which you could then remove. Or even more simple, without using the loop, run over the params object with a .Where method, and pass the Enumerable to the combine. For even more efficiency, you could modify the check to add an object only if an error occurred, and combine that with a .Where method.

dusrdev
Автор

Nice video! But stop abusing the phrase "I'am going to" every 3 seconds :D

eloherbapol
visit shbcf.ru