Re: are lambdas a way to bring functional programming to java?
On 7/1/2014 5:34 PM, markspace wrote:
On 6/28/2014 5:43 AM, Wanja Gayk wrote:
>
> static final Comparator<String> COMPARE_BY_LENGTH
> = (s1,s2) -> s1.length() - s2.length()
>
> Collections.sort(lst, COMPARE_BY_LENGTH);
In that video you just posted featuring Brian Goetz talking about
lambdas, Mr. Goetz says this is a bad idea. This costs a static field
and the time it takes to initialize the field during startup. And you
may never use that field, so you just spent time initializing
unnecessarily.
Not likely to be significant in my very humble opinion.
In article <53ae0af4$0$302$14726298@news.sunsite.dk>, Arne Vajh?j
>> Java 8 code:
>>
>> Collections.sort(lst, (s1,s2) -> s1.length() - s2.length());
>>
Whereas this code above seems to be "better" because the runtime
compiler can hoist the lambda out of the method call and cache it, just
like you did manually, but the compiler can do it lazily at runtime, and
won't do it at all if it's never needed.
I like it because it is simple - it only contains the core
logic - no extra "unnecessary" boilerplate code.
I think most people will consider that more readable - at least
after getting used to the syntax.
It happened in the C# world - I expect it to happen in the Java world
as well.
Arne