Discussion:
Lambdas/Callback Functions/Functional Programming. What is the point?
(too old to reply)
Steve
2015-01-22 17:35:07 UTC
Permalink
Hi all,

I had my first introduction to Java 8 last night via a talk on lambdas, streams, and functional programming.

I understand that this is they way college students are learning to program in Java, so it is here to stay, so I plan on learning it too.

I want to understand why Oracle implemented lambdas, streams, and functional programming.

It looks very similar to the "callback funcitons" I have seen in javascript and the jQuery library. I never liked that as it seemed to me to be a poor way of organizing things ( feeding blocks of code/functions into other functions ).

Basically, what I would like to understand is what are the benefits of these things?

Thanks

Steve
Pascal J. Bourguignon
2015-01-22 17:33:47 UTC
Permalink
Post by Steve
Hi all,
I had my first introduction to Java 8 last night via a talk on
lambdas, streams, and functional programming.
I understand that this is they way college students are learning to
program in Java, so it is here to stay, so I plan on learning it too.
I want to understand why Oracle implemented lambdas, streams, and functional programming.
It looks very similar to the "callback funcitons" I have seen in
javascript and the jQuery library. I never liked that as it seemed to
me to be a poor way of organizing things ( feeding blocks of
code/functions into other functions ).
Basically, what I would like to understand is what are the benefits of these things?
http://web.stanford.edu/class/cs242/readings/backus.pdf
http://worrydream.com/refs/Hughes-WhyFunctionalProgrammingMatters.pdf
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
Lothar Kimmeringer
2015-01-23 21:17:18 UTC
Permalink
Post by Steve
I want to understand why Oracle implemented lambdas,
streams, and functional programming.
One reason was, that everybody asked for this ;-)
Post by Steve
Basically, what I would like to understand is what are the
benefits of these things?
One reason, why I waited so long for this is Delayed Initialization.
Resources are only created when they are necessary so instead
of all the repeated code like this:

public class HeavyResource {
private ResourceData resData = null;

public String getResourceResult() {
if (resData == null){
getResourceData();
}
return resData.getResult();
}
}

you can now write

public class HeavyResouerce {
private ResourceData resData = () -> getResourceData();

public String getResourceResult() {
return resData.getResult();
}
}

The automatic creation if the resource hasn't been created
is done the moment the first access takes place to it. And
it is done in a thread-safe way (that many self made solutions
often fail to implement correctly)

Another feature is Lazy Evaluation. If you call a method
evaluate(heavyCalc1(), heavyCalc(2))
both heavyCalc-methods are called before the call of evaluate.
If the result of heavyCalc1 is enough for the evaluation the
call of heavyCalc2 was useless. Simple real-world-example:
logDebug(callStatusWebService());

With Lazy Evaluation the methods in the parameters are only
called if they are actually needed in the method.

A third feature is the ability to parallelize streams.
Just call .parallelStream instead of .stream and your
whole operation will be performed on all available CPUs
instead of only one.


Best regards, Lothar
--
Lothar Kimmeringer E-Mail: ***@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
questions!

--- news://freenews.netfront.net/ - complaints: ***@netfront.net ---
Stefan Ram
2015-01-26 17:51:36 UTC
Permalink
Post by Steve
Basically, what I would like to understand is what are the benefits of these things?
This is not about »supporting functional programming«
for functional programming's sake. It's about exploiting
multi-cores by make multi-threading more easy. It turned
out that functional programming can help in this regard.

Loading...