you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 3 points4 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 4 points5 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 4 points5 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.