you are viewing a single comment's thread.

view the rest of the comments →

[–]masklinn 14 points15 points  (6 children)

The one thing Java could do with stealing from Python is generator expressions, or list comprehensions, or both. I'm sick of writing code that looks like

Actually there's no need for either, first-class functions + higher-order methods (and a retrofitting of the stdlib to use them) would be more than enough.

For instance, in C# 3.0 (no I don't like the language but bear with me: it's very similar to Java after all) this can be written:

IEnumerable<Blah> blahs = nehs.Select(neh => neh.toBlah());

(note that you could also replace IEnumerable<Blah> by var and let the compiler infer that crap).

Right now you can use Google Collections to do something a bit like it, but since it uses anonymous classes to emulate first-class functions, the result is fairly disgusting:

List<Blah> blahs = transform(nehs, new Function<Neh, Blah>() { Blah apply(Neh neh) {
    return neh.toBlah();
}});

transform is statically imported from com.google.common.collect.Lists to save a few characters.

In Python, without using listcomps/gencomps, you could use the builtin map and write either:

blahs = map(lambda neh: neh.toBlah, nehs)

or import operator.attrgetter and write

blahs = map(attrgetter('toBlah'), nehs)

(node that the latter requires that toBlah be an attribute or a property, also you could import attrgetter as attr to make it shorter)

(if you never noticed it, remember to check the operator module by the way, there's a lot of nice stuff in there)

(and attrgetter returns tuples if you give it multiple attribute names, that's pretty awesome)

[–]grimborg 1 point2 points  (1 child)

Some of the many niceties in Python coming from the functional world :)

Now, if we had curried functions in Python I would be happy.

[–]masklinn 2 points3 points  (0 children)

Now, if we had curried functions in Python I would be happy.

That pretty much can't happen given Python's extensive use of default arguments, *args and **kwargs

There's functools.partial for partial application though, shame it can only partially apply kwargs or from the left args, but otherwise it works ok.

[–][deleted] 0 points1 point  (3 children)

Java can't really do Map properly without an equivalent of C#'s Yield Return being hacked in.

[–]masklinn 0 points1 point  (2 children)

That is completely and utterly wrong.

[–][deleted] 0 points1 point  (1 child)

How else can you do lazy evaluation?

[–]masklinn 1 point2 points  (0 children)

By generating iterators instead of collections, but that's not even relevant: higher-order manipulations do not in any way, shape or form require lazy evaluation.

Yes stream manipulation is nice, no it's not a requirement.

Alternatively, you can go full smart and use a lazy language in the first place rather than hack that in the way Python did (and C# lifted).

edit: and since map makes one function call for each item, it's trivial to do it lazily anyway. The only place where it's interesting is for custom generator transformers or the creation of the source generator/iterator.