List comprehensions: Difference between revisions

m
(→‎ES5: (Syntactic sugar for list comprehensions was not, in the end, adopted by ES6))
Line 986:
 
 
List comprehension notation was not, in the end, included in the final ES6 standard, and the code above will not run in fully ES6-compliant browsers or interpreters, but we can still go straight to the underlying monadic logic of list comprehensions and obtain:
 
[(x,y,z) | x <- [1..n], y <- [x..n], z <- [y..n], x^2 + y^2 == z^2]
Line 993:
 
<lang JavaScript> (n => {
const
let flatMap = (xs, f) => [].concat.apply([], xs.map(f)),
let flatMap = (xs, f) => [].concat.apply([], xs.map(f)),
range = (m, n) => Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
 
 
return flatMap(range(1, n), (x) =>
flatMap(range(1 + x, n), (y) =>
flatMap(range(1 + y, n), (z) =>
x * x + y * y === z * z ? [
[x, y, z]
] : []
)));
 
})(20);</lang>
 
9,659

edits