List comprehensions: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Python}}: experimental edit)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(7 intermediate revisions by 2 users not shown)
Line 21:
{{trans|Python}}
 
<syntaxhighlight lang="11l"> print(cart_product(1..20, 1..20, 1..20).filter((x, y, z) -> x ^ 2 + y ^ 2 == z ^ 2 & y C x .. z))</syntaxhighlight>
 
{{out}}
Line 275:
end mReturn</syntaxhighlight>
{{Out}}
<syntaxhighlight lang="applescript"pre>{{3, 4, 5}, {5, 12, 13}, {6, 8, 10}, {7, 24, 25}, {8, 15, 17}, {9, 12, 15}, {12, 16, 20}, {15, 20, 25}}</syntaxhighlightpre>
 
=={{header|Arturo}}==
Line 397:
C doesn't have a built-in syntax for this, but any problem can be solved if you throw enough macros at it:
{{works with|GCC}}
The program below is C11 compliant. For C99 compilers note the change on line 57 :(output remains unchanged).
<syntaxhighlight lang="c">
for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
</syntaxhighlight>
to
<syntaxhighlight lang="c">
int i;
for (i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); }
</syntaxhighlight>
Output remains unchanged.
<syntaxhighlight lang="c">
#include <stdlib.h>
Line 464 ⟶ 455:
List * intRangeList(int f, int t) {
List * l = listNew(sizeof f, &f), * e = l;
for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); } // C11 compliant
//int i;
//for (int i = f + 1; i <= t; i ++) { e = e->nx = listNew(sizeof i, &i); } // use this for C99
return l;
}
Line 611 ⟶ 604:
[x, y, z]
))
<code># pyth</code> can also be written more concisely as
<syntaxhighlight# lang="coffeescript">pyth = (n) -> flatten (flatten ([x, y, z] for z in [y..n] when x*x + y*y is z*z for y in [x..n]) for x in [1..n])</syntaxhighlight>
 
console.dir pyth 20</syntaxhighlight>
 
<code>pyth</code> can also be written more concisely as
 
<syntaxhighlight lang="coffeescript">pyth = (n) -> flatten (flatten ([x, y, z] for z in [y..n] when x*x + y*y is z*z for y in [x..n]) for x in [1..n])</syntaxhighlight>
 
=={{header|Common Lisp}}==
Line 1,045 ⟶ 1,036:
12 16 20
->
</pre>
 
=={{header|Insitux}}==
 
{{Trans|Clojure}}
 
<syntaxhighlight lang="cinsitux">
(function pythagorean-triples n
(let n+1 (inc n))
(for x (range 1 n+1)
y (range x n+1)
z (range y n+1)
(unless (= (+ (* x x) (* y y)) (* z z))
(continue))
[x y z]))
 
(pythagorean-triples 20)
</syntaxhighlight>
{{out}}
<pre>
[[3 4 5] [5 12 13] [6 8 10] [8 15 17] [9 12 15] [12 16 20]]
</pre>
 
Line 1,936 ⟶ 1,948:
=={{header|Python}}==
<syntaxhighlight lang="python">
import itertools
n = 20
 
# List comprehension:
[(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]
Line 2,692 ⟶ 2,707:
=={{header|Wren}}==
Using a generator.
<syntaxhighlight lang="ecmascriptwren">var pythTriples = Fiber.new { |n|
(1..n-2).each { |x|
(x+1..n-1).each { |y|
9,482

edits