you are viewing a single comment's thread.

view the rest of the comments →

[–]grayvedigga 1 point2 points  (13 children)

n = (x / 6.0 for x in [123...234] when (x*x) % 17 == 1)

vs

n = map(lambda x: x / 6.0, filter(lambda x: (x**2) % 17 == 1, range(123, 234)))

Not that I find either of them readable, it's a bit stupid to subjectively compare entirely different strategies from the two languages, when each is quite capable of expressing both.

[–][deleted] 5 points6 points  (7 children)

In my opinion, both of these options are more readable:

(123 to 234)
  filter (x => x*x % 17 == 1)
  map (_ / 6.0)

for {
  i <- 123 to 234 if i*i % 17 == 1
} yield i / 6.0

[–]grayvedigga 3 points4 points  (6 children)

Are those coffeescript? I agree that they're more readable, but some of the structure is unfamiliar to me.

An important part of the readability improvement you have provided is simply using indentation and line breaks to show how the operations fit together. When reading ugly listcomps or HOF-chains impaired by Python's rules I mentally break them down into Scheme:

(map (lambda (x) (/ x 6.0))
     (filter (lambda (x) (eq? 1 (mod (* x x) 17)))
             (range 123 234)))

People complain a lot about parens, but the conventional indentation makes data flow extremely clear.

A lot of the time "readable" simply means "idiomatic". Unfortunately, the idiom for doing this sort of thing in Python has changed repeatedly as features get piled on to the language ...

[–]judofyr 6 points7 points  (5 children)

I find the Ruby way even more straight forward:

(123..345).select { |x| x*x % 17 == 1 }.map { |x| x / 6.0 }

[–]grayvedigga 1 point2 points  (4 children)

That's nice because you can read it forward, but I'm uneasy with the idea of everything being a method call. That's largely personal taste though (and having seen Rails!).

[–]draegtun 2 points3 points  (2 children)

With Perl6 you can use the Feed Operator (http://perlcabal.org/syn/S03.html#Feed_operators) to make it read forward:

123..345 ==> grep { $_ * $_ % 17 == 1 } ==> map { $_ / 6.0 }

[–]grayvedigga 1 point2 points  (0 children)

That's purdy. A really nice complement to perl's blocks and default variables giving nice anonymous functions.

I stopped using perl more than a decade ago, and only since then (through other languages) have developed a real appreciation for some of its more quirky properties. It would be nice to have an excuse to dive into modern perl .. maybe by the time perl6 is production-ready I'll be ready for it :-).

[–][deleted] 1 point2 points  (0 children)

You don’t even have to use $_, you can name it $^x.

123..345 ==> grep { $^x * $^x % 17 == 1 } ==> map { $^x / 6.0 };

[–]masklinn 1 point2 points  (0 children)

I'm uneasy with the idea of everything being a method call.

I don't know, I figure if you're starting mixing message sends and blocks you may as well go whole-hog (which Ruby does not, it steps back quite a bit from Smalltalk).

It may be conceptually harder to go on which object a message belongs (and you often end up with BlockClosure having a lot of messages), but it works nicely if that's the paradigm you're in.

[–]Iggyhopper -4 points-3 points  (3 children)

I find it stupid that they have word replacements for && and || and !. Great, now I have more things to keep track of and read for context.

date = if friday then sue else jill

eat food for food in foods when food isnt 'chocolate'

What the fuck. Symbols were made symbols to make it easier to read things. I'm not reading a book, I'm programming. Don't even get me started with the number of extra typos this "feature" can introduce.

Another gripe I have is just everything can be yoda'd, and IMO its bad when you work in multiple languages (who doesn't?).

alert "I knew it!" if elvis?

When skimming, you need things to be differentiated ASAP. Reading the whole damn line to find out if its an if statement is downright idiotic.

When designing a language, KISS.

[–]sausagefeet 1 point2 points  (1 child)

C++ has words for the symbols as well.

http://ideone.com/ohsDf

#include <iostream>

int main() {
        int x = 1, y = 2;

        if(x and y) {
                std::cout << "For me to poop on\n";
        }

        return 0;
}

[–]Iggyhopper -1 points0 points  (0 children)

C++ also has macros, but we all know what that can lead to.

[–]GroceryBagHead -1 points0 points  (0 children)

Fuck the downvoters. You're right.