List comprehensions: Difference between revisions

m
→‎{{header|Python}}: experimental edit
(→‎{{header|Raku}}: add output)
m (→‎{{header|Python}}: experimental edit)
Line 1,935:
 
=={{header|Python}}==
<syntaxhighlight lang="python">def triplets(n):
# List comprehension:
<syntaxhighlight 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]</syntaxhighlight>
 
# A Python generator expression (note the outer round brackets), returns an iterator over the same result rather than an explicit list:
<syntaxhighlight 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]</syntaxhighlight>
# returns an iterator over the same result rather than an explicit list:
<syntaxhighlight 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)</syntaxhighlight>
 
# A slower but more readable version:
A Python generator expression (note the outer round brackets), returns an iterator over the same result rather than an explicit list:
<syntaxhighlight lang="python">[(x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z]</syntaxhighlight>
 
# Or as an iterator:
<syntaxhighlight 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)</syntaxhighlight>
<syntaxhighlight lang="python">((x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z)</syntaxhighlight>
 
# Alternatively we shorten the initial list comprehension but this time without compromising on speed. First we introduce a generator which generates all triplets:
A slower but more readable version:
# First we introduce a generator which generates all triplets:
 
def triplets(n):
<syntaxhighlight lang="python">[(x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z]</syntaxhighlight>
 
Or as an iterator:
 
<syntaxhighlight lang="python">((x, y, z) for (x, y, z) in itertools.product(xrange(1,n+1),repeat=3) if x**2 + y**2 == z**2 and x <= y <= z)</syntaxhighlight>
 
Alternatively we shorten the initial list comprehension but this time without compromising on speed. First we introduce a generator which generates all triplets:
 
<syntaxhighlight lang="python">def triplets(n):
for x in xrange(1, n + 1):
for y in xrange(x, n + 1):
for z in xrange(y, n + 1):
yield x, y, z</syntaxhighlight>
 
# Apply this to our list comprehension gives:
<syntaxhighlight lang="python">[(x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2]</syntaxhighlight>
 
# Or as an iterator:
<syntaxhighlight lang="python">[(x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2]</syntaxhighlight>
<syntaxhighlight lang="python">((x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2)</syntaxhighlight>
 
# More generally, the list comprehension syntax can be understood as a concise syntactic sugaring
Or as an iterator:
# of a use of the list monad, in which non-matches are returned as empty lists, matches are wrapped
# as single-item lists, and concatenation flattens the output, eliminating the empty lists.
 
# The monadic 'bind' operator for lists is concatMap, traditionally used with its first two arguments flipped. The following three formulations of a '''pts''' (pythagorean triangles) function are equivalent:
<syntaxhighlight lang="python">((x, y, z) for (x, y, z) in triplets(n) if x**2 + y**2 == z**2)</syntaxhighlight>
# The following three formulations of a '''pts''' (pythagorean triangles) function are equivalent:
 
<syntaxhighlight lang="python">from functools import (reduce)
More generally, the list comprehension syntax can be understood as a concise syntactic sugaring of a use of the list monad, in which non-matches are returned as empty lists, matches are wrapped as single-item lists, and concatenation flattens the output, eliminating the empty lists.
 
The monadic 'bind' operator for lists is concatMap, traditionally used with its first two arguments flipped. The following three formulations of a '''pts''' (pythagorean triangles) function are equivalent:
 
<syntaxhighlight lang="python">from functools import (reduce)
from operator import (add)
 
 
# pts :: Int -> [(Int, Int, Int)]
Line 2,029 ⟶ 2,027:
 
 
main()</syntaxhighlight>
</syntaxhighlight>
{{Out}}
<pre>[(3, 4, 5), (5, 12, 13), (6, 8, 10), (8, 15, 17), (9, 12, 15), (12, 16, 20)]
7,796

edits