Currying is a concept in functional programming where a function that takes multiple arguments is transformed into a series of functions, each taking a single argument. This can be useful for creating more generalized code in Angular, as it allows for greater flexibility and reusability in your functions.
To demonstrate the concept of currying in Angular, let’s create a simple example involving a math operation function.
First, let’s create a basic function that takes two numbers as arguments and returns their sum:
function add(a: number, b: number): number {
return a + b;
}
Now, let’s curry this function to make it more generalized. We can create a curried version of the add
function using the following code:
function curriedAdd(a: number): (b: number) => number {
return function (b: number) {
return a + b;
};
}
Now, we can use the curried version of the add
function like this:
const addFive = curriedAdd(5);
console.log(addFive(3)); // Output: 8
In this example, curriedAdd(5)
returns a new function that takes a single argument and adds it to the original value of 5. This allows us to create a more flexible and reusable function that can be easily customized for different use cases.
Currying can also be useful when working with Angular services. For example, you can curry a service method to make it more modular and easier to work with:
export class MathService {
add(a: number): (b: number) => number {
return function (b: number) {
return a + b;
};
}
}
Now, you can use the add
method from the MathService
in a more flexible way:
const mathService = new MathService();
const addTwo = mathService.add(2);
console.log(addTwo(5)); // Output: 7
By currying functions in Angular, you can create more modular and flexible code that is easier to maintain and reuse. This can be particularly useful when working with complex operations or when you need to customize a function for different use cases.
Overall, currying is a powerful concept in functional programming that can help you create more generalized and flexible code in Angular. By currying functions, you can build functions that are easier to work with, more reusable, and better suited for creating modular code in your Angular applications.
Can i get your email id
Great job! Although i believe it is not readable, it is still good to know!
I've generally made a mess of teaching this topic… This is the best way I've ever seen it explained and I will likely be referring folks this video from now on.
Deborah, you have such a talent for explaining things in a digestible way. Right after I landed a dev job in a .NET shop in 2016, I found your article "Practical Uses of Lambdas" in an old CODE magazine from 2010 they had at the office and I've been following your content ever since. Your C# content really helped me thrive there. Such superb content for a long time. It's really cool to see your content do the same for the Angular community.
I guess this is the only video I understood the real purpose of currying… But still wouldn't write it myself 😅
Great stuff, Deborah, thank you very much!
Great video once again Deborah! One stupid question though, why do you create a new array each time in the first example instead of using push(). Arrays are passed by reference right, so that would also take away the reassignment issue?
So in this case, obviously because we're using Javascript, I would just use call() or bind() for creating a function with a hard first argument. Are these just native currying functions in JS, so you don't have to complicate your code syntax writing your own nested functions everywhere you need to use this?
In C#, it's pretty easy to define a custom Curry delegate like:
delegate Func<T2, TResult> Curry<T1, T2, TResult>(T1 arg1);
It's not common but can definitely be done.
Great. Awesome presenting
Good thing about currying – you never gonna need it, if you are not working with lambda calculations
Excellent!! One more tool added to the box. Thanks
I really enjoy your content, thanks for sharing, love your easy to understand explanations 😁
I wish I had this video back in 2002 to explain what curry is. I’m familiar with it from having worked in c and c++ but never quite understood it from a very dry academic perspective. I’ll have to keep an eye out in my code for when I’m passing in the same argument over and over again and try and use this technique.
another awesome tutorial! I tried to understand that watiching some other videos but here I finally got that! Thank you 🙂
great as always!
Thanks 🙏. Whenever I write code the only thing in my mind is to follow DRY as it leads to implement different patterns. I was thinking of learning currying practically and here your video came. Last 2 videos are for coders who want to write optimized code…Very well articulated video…thanks
Again a good one! Thanks for doing these vids.
I always admire that you avoid any shortcuts while explaining something.
Somehow related to the topic of the video (functions as objects): I started to add pre-configured functions (or curried functions) to my Observables. Then the template just needs to call a method that is part of the data. If it is bound to a button, if the funcition is null, the button is disabled. So – for example – a list of snacks has not only a property for the name but also for a function (without parameters) that will delete exactly that snack.
I would be interested what you think of that approach.
Great video Deborah!
Like it a lot.
In every video of yours, I learn something new. Same thing can be said about your Pluralsight courses.
I really appreciate it.
Thank you very much!
Great video, very well explained!
Not gonna lie, I'm not a fan of currying personally. In Typescript it does feel like an unfavorable trade off: You WIN a small reduction of trivial boilerplate but you LOOSE a lot of clarity and readability.
Maybe I am just too untrained on the pattern, but right now I feel like I need AT LEAST 10 time as much time to comprehend a method with currying in comparison to a "normal" method.
Maybe this dwindles a bit with more experience in writing/reading curry'd methods?
Great explanations as usual thank you a lot