When you hear the word “curry”, the first thing that probably comes to your mind is a plethora of Indian cuisine. Indian people use a very complex combination of spices to prepare the dish. He added all the ingredients one by one to make a perfect curry.
So the main trick is to add all the ingredients one by one. Similarly, in programming, queuing is the technique of converting a function that takes multiple arguments into a function that takes one argument at a time and then returns a function.
But in any programming language, we can easily declare a function that takes multiple arguments at a time, and most programmers are used to it. So why use curry?
In addition to making your meal stand out, it is used to allow a series of operations on a particular dataset. So instead of writing a complex algorithm with some nested loops, you can accomplish this with a few simple commands.
It uses code reusability, and the less code you have to write, the less errors you’ll have! You can introduce currying/un-currying for constructive reasoning and as a way to state the elimination and introduction of rules. It provides a connection to a more elegant inspiration as to why it exists.
The programmer has the option to declare every function in two identical ways. In currying, a function takes only one argument and returns a function. Then the returned function takes one argument and returns the final result.
So there may be a question on the mind of every programmer: why would we take a more complex direction, that is, first writing a function that returns one function, and then calling another function?
In the first function, we pass both the arguments at the same time. In the second function, we pass the first argument (which itself returns a function), and then we pass the second argument.
Actually, here both the functions are doing the same thing. These two examples show how we can always convert a function that takes more than one argument at a time to one that takes one argument at a time. This is the process of making curry. So the MULTIPLE 2 function is the currying version of MULTIPLY 1.
These are some of the benefits of the curry ceremony. You can always chain operations with a few simple steps. Great, isn’t it?
Let’s look at another example:
Now let’s make morningwalk for sunday and add 100 steps to it.
So basically here we are calling the addSteps() instance method on the instance itself.
But we can also do the same thing in a curry way:
It’s doing the same thing we did above. First, we are assigning the addSteps and minusSteps methods to two separate variables. Here at this stage, we are not calling any function. We have created references to functions in the same way as function pointers. In the next step, we are actually calling the functions that are stored inside stepIncreaser and stepDecreaser .
Now, stepIncreaser takes a single argument which is the Morningwalk instance, and returns a function whose type is (Int) -> (). So the returned function takes an argument of type Int and returns nothing. Here, the returned function and addSteps() function have the same type of method signature. The same concept applies to StepDecreaser.
So, finally, we can say that an instance method is also a type of method in Swift. It takes an instance of the class as argument and returns a function, which will then take the other arguments and return/update the final result.
In this article, we had a function with more than one argument. We turned this into a function that always took a single argument at a time, resulting in another function, until there were no arguments left. This gave us the final result. So we can say that functions are nothing more than ordinary values that can be generated and returned by other functions.