1

Is Currying, Partial-Application and Functional-Decomposition are same notions? Is there any difference between them?

I am reading about these, I found these same. I don't get if there is any difference between them. Why write code like this in java when you can pass all parameters to a single method in one go?

 Function<Integer,Function<Integer,Function<Integer, Integer> > > 
        triadder = u -> w -> v -> u + w + v; 

    // Calling the curried functions 

    // Calling Curried Function for Adding u, v & w 
    System.out.println("Add 2, 3, 4 :"
                       + triadder 
                             .apply(2) 
                             .apply(3) 
                             .apply(4));
Meenamma
  • 47
  • 8
  • [What is the difference between **currying** and **partial application**?](https://stackoverflow.com/q/218025/5221149) – Andreas Nov 04 '19 at 06:14
  • Wikipedia: [Contrast **currying** with **partial function application**](https://en.wikipedia.org/wiki/Currying#Contrast_with_partial_function_application) – Andreas Nov 04 '19 at 06:16
  • 1
    Possible duplicate of [What is the difference between currying and partial application?](https://stackoverflow.com/questions/218025/what-is-the-difference-between-currying-and-partial-application) – njzk2 Nov 04 '19 at 06:26
  • @njzk2 suggested question explains only a part of this question. – Meenamma Nov 05 '19 at 10:52
  • @Meenamma I recommend reading the accepted answer on that question – njzk2 Nov 06 '19 at 05:01

2 Answers2

3

Java is not well-suited to this approach. Other languages make it easier. In Java, Object Oriented is the way to go, with Functional Programming ideas spicing it up only in specific cases where it is really needed.

"Partial application" is more general concept than currying. It means giving a function that holds some data in its closure--the caller provides another parameter.

This can be compared to a Java object that "captures" a field, then offers a method that uses that field as well as another param.

"Currying" more specifically re-structures a multi-param function into functions of one param. This is done by capturing all but one parameter, leaving the other parameters for later calls.

"Functional decomposition" is the analysis that breaks down a complex process into simple functions, the Functional Programming equivalent of Object Oriented design.

Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
0

In addition to @JoshuaFox's answer, one answer to your other question about why not pass all parameters in a single go is that it often makes code repetitive and can add a lot of boilerplate where it isn't really needed.

I have a small web app written in F# that works on several different platforms. I have a config file that provides values for each specific platform. At runtime I get those values from the config file and have functions like this:

let private supptFunc (p1: string) (p2: string) (p3: string) : string = ...
...
// Partially apply parameters from config file to create a simpler
// function in the rest of the code
let externaFn1 = supptFunc (<get v1 from config file>) (<get v2 from config file>)

This makes externalFn1 a function that takes a string and returns a string. So, in the rest of my code I only call externaFn1 with a single parameter (p3) which I get from the requests.

In this way I implement supptFunc in terms of the parameters it needs to do the job and never worry about where they come from. I don't have to read a config file inside the code or store the values in a list or map somewhere. They are always passed in.

But I don't have to make those values available where ever the function may be needed. I have simply created a useful function that I expose to the rest of my code that has those values 'baked in' for the current platform. All of the rest of the codebase simply uses that exposed function with only the parameters they have and need.

So, now I can test supportFunc as a function that always takes all the parameters it needs and can provide any parameters I want at test time to check out its behavior under any circumstances I choose. But I don't require all callers to provide everything supportFunc needs since it isn't necessarily their responsibility to hold on to all of this information.

This isn't the only use case for partial application, but the fact that F# automatically curries functions makes it a simple way to keep your code cleaner and simpler.

melston
  • 2,198
  • 22
  • 39