
Ladies, look at your code. Now back to mine. - mambodog
http://www.google.com/buzz/kevinb9n/Q3YnNiEC9jr/Ladies-Look-at-your-code-return-Iterables-filter
======
gojomo
Perhaps Java8 will get multiline string literals -- an over-9-year-old
request:

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4472509>

Lots of the shortsighted decisions in the historical bug database can be
attributed to xxxxx@xxxxx, who dismissed this as "yet another request for a
syntactic sugar to save some user from typing... not worth it." I sure hope
Oracle has let xxxxx go by now!

~~~
csmeder
Wow, what an ad hominem answer:

 _shankar.unni

I agree with the Evaluation, completely. This is truly pointless syntactic
sugar, unless you're coming directly from the C# or Visual Basic world without
ever having worked in any other language, and escape sequences are just too
hard for you to comprehend.. (In which case, many other Java features would
be, too..)_

This is sad, some one offers a great language feature that will reduce bugs
and save money and people respond with insults.

~~~
Locke1689
I find it ironic that a Java programmer is deriding other programmers (C# and
VB) for not being "real" programmers.

~~~
scotth
That's narrow minded. There are some great Java programmers. It's just that
they don't have the same priorities that you do.

~~~
Locke1689
I think you misunderstood me: I was saying that the notion of who is a "real"
programmer is stupid. I really despise using PHP. Does that make everyone who
works at Facebook not a "real" programmer?

However, the notion of a _Java_ programmer deriding others for their language
choice is even more ridiculous because of the comparative ease of the syntax.
If the argument held any merit (which it doesn't), Java definitely wouldn't be
on top of the "real programmer" mountain.

------
InclinedPlane
Considering that Javascript, Ruby, Python, C#, Perl, and even PHP have
supported anonymous functions or lambda expressions, this advancement is a
long time coming.

~~~
Locke1689
Oh God it's so much worse than that. You remember who was a coauthor on the
original specification? _Guy Steele_. Yes, "Lambda Papers" Guy Steele. I have
no idea how this has taken so long.

Oh and I thought this was hilarious. :)

~~~
_delirium
While Steele's been involved with Lisp and Scheme, he also has a really long
history of involvement with imperative languages, which he seems to have kept
separate until recently. He's been on the C and Fortran standards committees,
for example, and that hasn't caused much noticeable crossover of features like
lambdas into those languages, either.

~~~
10ren
He also co-authored the Java Language Specification (with James Gosling and
Bill Joy.)

The wise are not dogmatic.

~~~
koenigdavidmj
He's also responsible for^W^Wthe author of <http://www.careferencemanual.com/>
.

------
jrockway
If Java were Haskell, the syntax would be "filter isPartTime".

It's silly to have to type stuff that's easily understood by both the
programmer and the compiler.

------
mhansen
It's sad that you can't easily chain these methods. e.g.

    
    
        employees.filter(predicate1).filter(predicate2).filter(predicate3)
    

instead of

    
    
        Iterables.filter(Iterables.filter(Iterables.filter(employees,predicate1), predicate2), predicate3)

~~~
jrockway
That's what's nice about functional programming, where functions are
functions, not thingies inside your object. You would write something like:

    
    
       filter predicate1 . filter predicate2 . filter predicate3 $ employees
    

Where . is function composition.

~~~
jlouis
You still need to hope the fusion optimization kicks in there. It can be done
faster with one filter over the list and then checking all three predicates at
each element in some cases. I think the following will do the right thing:

    
    
      filter (\x -> p1 x && p2 x && p3 x) employees
    

which can then be made points-free.

~~~
lsb
Don't just hope, write it yourself!

    
    
      {-# RULES
        "filter/filter"  forall f g xs. filter f (filter g xs) = filter (f.g) xs
      #-}
    

It's a programmable programming language.

~~~
uros643
Don't you mean

    
    
      forall f g xs. map f (map g xs) = map (f.g) xs
    

?

As for filter, I presume something like

    
    
      forall f g xs. filter f (filter g xs) = filter (\x. g x && f x) xs
    

would work.

~~~
lsb
Yup,

    
    
      forall f g xs . filter f (filter g xs) = filter (\x -> g x && f x) xs
    

was exactly what I meant, thanks for the catch!

------
muyyatin
In Scala you can just do:

employees.filter( _.isPartTime )

And it runs in the same virtual machine.

~~~
erikpukinskis
In Ruby:

employees.find_all &:isPartTime

~~~
patio11
And I liked employees.select {|employee| employee.part_time?}

Crikey, should buy a book...

~~~
Confusion
Personally, I prefer your version; the &: version starts reeking of Perl line
noise. Although I believe Ruby would benefit from an _ for not explicitly
named block arguments.

~~~
steveklabnik
As a random note for anyone who's wondering how this syntax came about, this
is the Symbol#to_proc [1] method, which was actually a monkeypatch from Rails
that ended up making its way back into Ruby proper.

1: <http://ruby-doc.org/core-1.9/classes/Symbol.html#M000031>

------
statictype
Correct me if I'm wrong but I believe the C# compiler goes a step further and
allows you to write those lambda arguments without the type annotation. Is
there a technical reason why the Java compiler can't do the same?

~~~
StrawberryFrog
C# will infer lambda argument types from context, yes. So the code given:

    
    
      return Iterables.filter(employees, {Employee e -> e.isPartTime()});
    

Would be in C# (Since you'd also omit the { } and use "=>" not "->") something
like this:

    
    
      return Iterables.filter(employees, e => e.isPartTime());
    

And it is a better C# idiom to make "filter" an extension method on
IEumerable<T>.

    
    
      return employees.filter(e => e.isPartTime());
    

In fact, the existing extension method "Where()" is what you want.

    
    
      return employees.Where(e => e.isPartTime());
    

C# has had this for about 2 years now. And it is good. So, I'm looking at your
code, and back to mine. Mine is shorter but still very readable (to me
anyway). And it stays readable when you chain methods, e.g.

    
    
      return employees.Where(e => e.isPartTime()).ToArray();
    

I'll stay with mine right now.

~~~
henrikschroder
There was also a comment to the above saying that instead of writing:

    
    
      List<Ticket<ThingYouLove>> tickets = new ArrayList<Ticket<ThingYouLove>>();
    

You could write:

    
    
      List<Ticket<ThingYouLove>> tickets = new ArrayList<>();"
    

Nice, but I prefer C# there as well where you use the var keyword, like this:

    
    
      var tickets = new ArrayList<Ticket<ThingYouLove>>();

~~~
instcode

       Nice, but I prefer C# there as well where you use the var keyword, like this:
    
         var tickets = new ArrayList<Ticket<ThingYouLove>>();
    

Actually, JDK7 is better in this case because it helps developer to "program
to an interface".

~~~
StrawberryFrog
Could you expand on what "program to an interface" means to you in this
context, I'm not getting it. Assume that I know what it means in the usual
context, of depending on the services of an interface type not a concrete
type.

~~~
henrikschroder
With the var keyword the variable will be the exact same type as what you
initialize it to, not an interface.

    
    
      List<Foo> list = new ArrayList<Foo>();
    

...is programming towards an interface, whereas

    
    
      var list = new ArrayList<Foo>();
    

...isn't, because the type of list will be ArrayList. I think his point was
that with the new Java stuff you could instead do

    
    
      List<Foo> list = new ArrayList<>();
    

...which would then be explicitly programming towards an interface, while
still not having to type so bleeding much.

~~~
StrawberryFrog
Programming to an interface just doesn't seem so useful within the scope of a
method.

------
fourneau
I'm really glad that JDK 7 finally has diamonds. (In more ways than one:
[http://weblogs.java.net/blog/forax/archive/2009/08/27/diamon...](http://weblogs.java.net/blog/forax/archive/2009/08/27/diamond-
gate))

------
aaronbrethorst
> I'm on a horse.

And quite possibly very drunk.

edit: oh I get it, he's referring to [http://www.blogher.com/im-horse-old-
spices-man-your-man-coul...](http://www.blogher.com/im-horse-old-spices-man-
your-man-could-smell). This is what I get for not watching tv anymore, I
guess.

~~~
Qz
Or even HN? There was a fair amount of coverage of the Old Spice man on here
when they were doing their nonstop live commercial shoots.

~~~
aaronbrethorst
I spend a fair amount of time reading HN, but this is one of the topics I
skipped over.

------
Jach
He should have thrown in the new feature for meaningless underscores in
numeric literals. I've always wanted this feature in every language, since
1_000_000 is so much more readable than 1000000 or 10 __6.

------
baguasquirrel
Did this strike anyone else as being mildly chauvinistic?

~~~
lotharbot
It's a parody of an Old Spice commercial (official version at
<http://www.youtube.com/watch?v=owGykVbfgUE> ).

~~~
baguasquirrel
Wow, nevermind then. That was hilarious.

~~~
whatusername
And since you missed it - about a month ago Old Spice did a great internet
level promotion with this ad - answering questions from facebook / reddit /
youtube / twitter. And cleverly targeted videos at people like Kevin Rose. It
was really impressive. HN had the post about how they did it here:
<http://news.ycombinator.com/item?id=1516195> And the key discussion about the
campaign was here: <http://news.ycombinator.com/item?id=1512225>

You can see the full list (that does get rather repetitive -- although keep in
mind they were coming out about every 20 minutes for 2 days straight) here:
<http://www.youtube.com/user/OldSpice#p/u>

------
tibbon
Now I want to see the Old Spice guy make a response video to this... or maybe
just read this aloud.

------
EliRivers
Which of those two styles is being presented as superior?

------
kinnth
So funny and i'm not even a geek!

------
ebtalley
Im on a horse. Freaking awesome.

------
admorphit
Awesome - love it.

------
mkramlich
go Java! catching up a little more with Python. Meanwhile, back to my Python
hacking session...

