List comprehensions: Difference between revisions

add Ruby
(add Ruby)
Line 117:
 
<lang python>((x,y,z) for x in xrange(1,n+1) for y in xrange(x,n+1) for z in xrange(y,n+1) if x**2 + y**2 == z**2)</lang>
 
=={{header|Ruby}}==
A couple of ways, neither feel particularly elegant. Ruby's OO style really enforces writing left-to-right.
<lang ruby># using a storage array
a=[]; (1..n).each {|x| (1..n).each {|y| (1..n).each {|z| a << [x,y,z] if x**2 + y**2 == z**2}}}; a
 
# no temp array, but a lot of housework to flatten and remove nils
(1..n).collect {|x| (1..n).collect {|y| (1..n).collect {|z| [x,y,z] if x**2 + y**2 == z**2}}}.reduce(:+).reduce(:+).compact</lang>
 
=={{header|Tcl}}==
Anonymous user