6 ms·
Coming from Ruby to Python...I could not believe how damn hard it was to comprehend the beautifully explained and simple code (just 21 lines!) in Peter Norvig's
by danso 11y ago
Coming from Ruby to Python...I could not believe how damn hard it was to comprehend the beautifully explained and simple code (just 21 lines!) in Peter Norvig's writeup of how to create a spelling corrector [1], purely because of the list comprehensions (and a few other visual differences, such as partial list notation)
For example:
inserts = [a + c + b for a, b in splits for c in alphabet]
It's no different (conceptually, at least) than doing:
inserts = []
for a, b in splits:
for c in alphabet:
inserts.append(a + c + b)
Or in Ruby (OK, maybe this isn't perfect Ruby):
inserts = splits.reduce([]) do |x, (a, b)|
alphabet.each{|c| x << a + c + b }
x
end
But the list comprehension was so difficult for me to read that it might as well have been a brand new concept. But I found I got quickly over it after rewriting all of his comprehensions as for-loops, and then trying to convert them back into comprehensions. It was a good reminder that sometimes the barrier is just plain muscle/memory reflex (and probably much more so for beginning programmers...)
[1] http://norvig.com/spell-correct.html http://norvig.com/spell-correct.html
- mjevans 11y agoI most frequently use List Comprehensions in a nested manor, whenever I do that I /ignore/ the normal python syntax and do this for readability. inserts = [ a + c + b for a, b in splits for c in alphabet ] It has the effect of showing the nested loops stacked like they would be in code, but without intending.
- orf 11y ago> I /ignore/ the normal python syntax Is it not normal to do that? I always lay out the nested ones, and even single comprehensions if the conditional is complex.
- gary_bernhardt 11y agoYes, that's very common formatting (except for the whitespace immediately inside the square brackets, which violates PEP 8, but that's incidental to the question of line breaks).
- nomel 11y agoFor the projects I manage, I would have the dev rewrite that as a proper for loop. That's way too confusing and hard to parse. List comprehensions are great for simple operations, but once you start nesting, you're just being a jerk to the next dev that has to read your code. Code should be clear first, elegant second.
- troym 11y ago> Code should be clear first, elegant second. I disagree: code should be correct before it is clear. And because it's so easy to mess up a for loop (for me at least), I choose list comprehensions where reasonable. > you're just being a jerk to the next dev that has to read your code That's not very charitable to either party. You're assuming that the motive of the author is to be a jerk, and you're assuming that the reader won't understand. If I had a dev on my team who couldn't read a list comprehension, I'd (a) wonder how they were hired, and then (b) teach them.
- ebola1717 11y agoI've written a fair share of Haskell, and I still get tripped up by list comprehensions in python - even my own. It's not that it's impossible to understand, and when there's only one predicate, I think it's fine: for row in [[i*j for i in range(1, 8)] for j in some_list if j % 2 == 0]: some_op(row) vs for j in some_list: if j % 2 == 0: row = [ i * j for i in range(1,8) ] some_op(row) I would compare it to sentences and paragraphs. The former feels like a run-on sentence, while the latter is more obvious, cause it has one predicate per line. Also, list comprehensions are a bit like yoda-speak - it introduces the verb before the subject. You have to untangle the order of operations, rather than having the order read top-down and left-right. Of course, I'm a rubyist, so I'd prefer: some_list.select { |j| j % 2 == 0 } .map { |j| (1..8).map { |i| i * j } } .map { |row| some_op(row) } Though definitely need to do something about that second line. Haskell list comprehensions are a bit easier to parse because they have symbolic delimiters, the fact that Haskell is naturally more terse, and because you can always check the type of the list [ [i*j | i <-[1..8]] | j <- [1..4], j % 2 == 0 ]
- 11y ago
- akavi 11y agoI'd do the ruby version as: splits.flat_map do |(a, b)| alphabet.map{|c| a + b + c} end Which feels pretty close to the list comprehension to my eyes.
- viraptor 11y agoDepends on a programmer I guess. I'm pretty confused by the Ruby version and I can do Ruby in anger. I'd also use maps rather than each if I was writing it.