List comprehensions: Difference between revisions

m
tested in spidermonkey 1.8
(added javascript)
m (tested in spidermonkey 1.8)
Line 38:
))</lang>
Output:
<div style="width:full;overflow:scroll"><pre>
<pre>
+3 +4 +5 +5 +12 +13 +6 +8 +10 +8 +15 +17 +9 +12 +15 +12 +16 +20
</pre></div>
 
=={{header|Clojure}}==
Line 112:
 
=={{header|JavaScript}}==
{{works with|JavaScript|1.7+ (Firefox 2+)}} {{works with|SpiderMonkey|1.7}}
See [https://developer.mozilla.org/en/New_in_JavaScript_1.7#Array_comprehensions here] for more details
 
<div style="width:full;overflow:scroll"><lang javascript><script type="application/javascript;version=1.7"/>function range(begin, end) {
(not tested yet)
<lang javascript><script type="application/javascript;version=1.7"/>
function range(begin, end) {
for (let i = begin; i < end; ++i) {
yield i;
Line 124 ⟶ 122:
 
function triples(n) {
return [[x,y,z] for each (x in range(01,n+1)) for each (y in range(x,n+1)) for each (z in range(y,n+1)) if (x*x + y*y == z*z)];
}
 
</script></lang>
for each (var triple in triples(20))
print(triple);</script></lang></div>
outputs:
<pre>3,4,5
5,12,13
6,8,10
8,15,17
9,12,15
12,16,20</pre>
 
=={{header|Mathematica}}==
Line 176 ⟶ 183:
=={{header|Ruby}}==
A couple of ways, neither feel particularly elegant. Ruby's OO style really enforces writing left-to-right.
<div style="width:full;overflow:scroll"><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></div>
 
=={{header|Tcl}}==
Anonymous user