Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #28899

Re: are lambdas a way to bring functional programming to java?

Date 2014-07-01 22:32 -0400
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: are lambdas a way to bring functional programming to java?
References <ead6a498-272d-4ddd-b509-031bb858e882@googlegroups.com> <53ae08d5$0$304$14726298@news.sunsite.dk> <53ae0af4$0$302$14726298@news.sunsite.dk> <MPG.2e18d6e192455b131e@202.177.16.121> <53b36e09$0$293$14726298@news.sunsite.dk>
Message-ID <53b36f49$0$293$14726298@news.sunsite.dk> (permalink)
Organization SunSITE.dk - Supporting Open source

Show all headers | View raw


On 7/1/2014 10:27 PM, Arne Vajhøj wrote:
> On 6/28/2014 8:43 AM, Wanja Gayk wrote:
>> In article <53ae0af4$0$302$14726298@news.sunsite.dk>, Arne Vajhøj
>> (arne@vajhoej.dk) says...
>>
>>> Here is a simple example.
>>>
>>> Java 7 code:
>>>
>>> Collections.sort(lst, new Comparator<String>() {
>>>       public int compare(String s1, String s2) {
>>>           return s1.length() - s2.length();
>>>       }
>>> });
>>>
>>> Java 8 code:
>>>
>>> Collections.sort(lst, (s1,s2) -> s1.length() - s2.length());
>>>
>>> I do not see any value in the extra code in the 7 version.
>>
>> I fear Java8's Lambdas may not encourage reuse so much as the Java 7
>> way. This does seem to be a pretty standard comparator, so I would
>> probably code it this way:
>>
>> static final Comparator<String> COMPARE_BY_LENGTH = new Comparator<>{
>>        public int compare(String s1, String s2) {
>>            return s1.length() - s2.length();
>>        }
>>   });
>>
>> Collections.sort(lst, COMPARE_BY_LENGTH);
>>
>> Just because using it looks better.
>> Of course in Java 8 it could look like this (sorry for the bad line
>> breaks:
>>
>> static final Comparator<String> COMPARE_BY_LENGTH
>>                = (s1,s2) -> s1.length() - s2.length()
>>
>> Collections.sort(lst, COMPARE_BY_LENGTH);
>>
>> ..or even implementing a more general interface.
>> But would people do that very often, if the original expression is so
>> concise? Hmm..
>
> I look at it a bit differently: the introduction of lambda
> makes the benefits of reusing close to zero and we only like
> reuse when it actually has benefits, so I do not see it as
> a problem.

BTW somewhat off-topic, I do not like the COMPARE_BY_LENGTH thingy.

It is almost like:

public static final int TWO = 2;

* The name does not really explain more than the value
* The value is given by the name so the value can never be changed
   without changing the name

The paradigm actually creates more work when something has to be
changed without improving readability.

Obviously we could have COMPARE_BY_XYZ where XYZ is something
business logic related - that we may want to change the semantics
for.

Arne



Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

are lambdas a way to bring functional programming to java? gdotone@gmail.com - 2014-06-27 16:50 -0700
  Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-06-27 20:14 -0400
    Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-06-27 20:23 -0400
      Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-06-28 14:43 +0200
        Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-01 14:34 -0700
          Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-07-01 22:01 -0400
          Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-02 20:52 +0200
            Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-02 13:08 -0700
              Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-03 03:13 +0200
              Re: are lambdas a way to bring functional programming to java? Joerg Meier <joergmmeier@arcor.de> - 2014-07-06 14:42 +0200
        Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-07-01 22:27 -0400
          Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-07-01 22:32 -0400
            Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-01 19:58 -0700
            Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-02 22:55 +0200
              Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-02 15:32 -0700
                Re: are lambdas a way to bring functional programming to java? Gene Wirchenko <genew@telus.net> - 2014-07-03 09:45 -0700
          Re: are lambdas a way to bring functional programming to java? leif.roar@invalid.com (Leif Roar Moldskred) - 2014-07-01 23:48 -0500
            Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-02 22:55 +0200
              Re: are lambdas a way to bring functional programming to java? Gene Wirchenko <genew@telus.net> - 2014-07-03 09:49 -0700
                Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-03 10:25 -0700
                Re: are lambdas a way to bring functional programming to java? Gene Wirchenko <genew@telus.net> - 2014-07-04 11:03 -0700
          Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-02 21:23 +0200
      Re: are lambdas a way to bring functional programming to java? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-30 09:08 -0700
        Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-07-01 21:56 -0400
  Re: are lambdas a way to bring functional programming to java? Joerg Meier <joergmmeier@arcor.de> - 2014-06-28 12:11 +0200
    Re: are lambdas a way to bring functional programming to java? Roedy Green <see_website@mindprod.com.invalid> - 2014-06-30 08:50 -0700
      Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-06-30 09:09 -0700
      Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-01 19:27 +0200
        Re: are lambdas a way to bring functional programming to java? Wanja Gayk <brixomatic@yahoo.com> - 2014-07-01 20:46 +0200
          Re: are lambdas a way to bring functional programming to java? Sebastian <news@seyweiler.dyndns.org> - 2014-07-01 21:52 +0200
            Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-01 14:11 -0700
              Re: are lambdas a way to bring functional programming to java? Jan Burse <janburse@fastmail.fm> - 2014-07-22 14:37 +0200
                Re: are lambdas a way to bring functional programming to java? markspace <markspace@nospam.nospam> - 2014-07-22 10:39 -0700
                Sidetrack on generics (WAS: Re: are lambdas a way to bring functional programming to java?) Daniele Futtorovic <da.futt.news@laposte.net> - 2014-07-22 20:48 +0200
                Re: Sidetrack on generics (WAS: Re: are lambdas a way to bring functional   programming to java?) Chris Riesbeck <Chris.Riesbeck@gmail.com> - 2014-07-23 12:01 -0500
      Re: are lambdas a way to bring functional programming to java? Arne Vajhøj <arne@vajhoej.dk> - 2014-07-01 21:50 -0400
  Re: are lambdas a way to bring functional programming to java? Roedy Green <see_website@mindprod.com.invalid> - 2014-07-22 04:40 -0700
  Re: are lambdas a way to bring functional programming to java? Roedy Green <see_website@mindprod.com.invalid> - 2014-07-28 09:45 -0700

csiph-web