Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 Function Style Programming what is the difference between currying and Functions Composition [closed]

I am pretty new to the world of functional programming.Trying the new function style programming that ships with Java 8. recently I have come to know about currying and method composition. Understanding the true essence of functional style programming using java is pretty hard and now I have a couple of questions, However, before asking all these questions I have tried the same on python and now a bit familiar with few core concepts.

1.In java how Currying and method composition are different In fact I don't see any difference at all, especially after reading this article https://dzone.com/articles/higher-order-functions

2.As a programmar (from my java programming perspective) why would I prefer currying. for example why would i Do this f(x){ return g(y) } instead of f(x,y){ return x(y)} what difference does it make?

like image 715
Arko Avatar asked Jul 30 '17 09:07

Arko


1 Answers

While both operations output a function, the example makes the difference fairly clear:

  1. Currying takes a single function f() and produces an "intermediate" function f'() that is the same as f() but with some parameters already fixed. When you eventually fill in the rest of the parameters, you will evaluate the original f().
  2. Whereas composition will take two functions f() and g() and creates a completely different function g(f()).

Take a simple example: f(x,y) = x+y, where x and y are integers. No amount and combination of currying of this function can result in a function that will ever return a non-integer result. But compose it with g(x) = x/2, and you get g(f(x,y)) = (x+y)/2, which will of course happily return non-integers.

Why would you then use currying?

Java instance methods for example are a result of a fairly similar process. Instance methods differ from static methods in that they've got an extra hidden parameter called this. When you say new Foo(), essentially you bind this hidden parameter to the newly created Foo object. So instead of having to call function void bar(Foo this, int x), you can just refer to it as void bar(int x), with the first parameter already fixed in place. (By the way, void bar(Foo this, int x) is in fact perfectly valid Java syntax, we just almost never use it.)

This isn't entirely a coincidence, as pure functional languages can only have functions whose outputs depend on its inputs alone (as opposed to OO languages, where the output of a method can also depend on the internal state of the object the method belongs to.)

As a general advice, if you want to learn the essence of functional programming, it's best not to do it from Java. Not even from Scala either. Try to learn it from a pure functional language like Haskell and then you can come back to Java and understand much better what subset of FP was implemented in it and how.

like image 130
biziclop Avatar answered Oct 17 '22 08:10

biziclop