Jump to content

List comprehensions: Difference between revisions

m
(→‎ES6: Updated generic functions and function names)
Line 1,000:
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:
 
<code>[(x,y,z) | x <- [1..n], y <- [x..n], z <- [y..n], x^2 + y^2 == z^2]</code>
 
by using concatMap (the monadic bind function for lists), and x => [x] (monadic return for lists):
 
<lang JavaScript>(()n => {
'use strict';
 
Line 1,020:
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
 
 
// EXAMPLE ----------------------------------------------------------------
 
// xs :: [Int]
const xs = enumFromTo(1, 20n);
 
return concatMap(x =>
Line 1,034 ⟶ 1,035:
] : [],
 
dropenumFromTo(y, xsn)),
dropenumFromTo(x, xsn)), xs);
enumFromTo(1, n));
})(20);</lang>
{{Out}}
<lang JavaScript>[[3, 4, 5], [5, 12, 13], [6, 8, 10], [8, 15, 17], [9, 12, 15], [12, 16, 20]]</lang>
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.