Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
32
Task/List-comprehensions/JavaScript/list-comprehensions-1.js
Normal file
32
Task/List-comprehensions/JavaScript/list-comprehensions-1.js
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
// USING A LIST MONAD DIRECTLY, WITHOUT SPECIAL SYNTAX FOR LIST COMPREHENSIONS
|
||||
|
||||
(function (n) {
|
||||
|
||||
return mb(r(1, n), function (x) { // x <- [1..n]
|
||||
return mb(r(1 + x, n), function (y) { // y <- [1+x..n]
|
||||
return mb(r(1 + y, n), function (z) { // z <- [1+y..n]
|
||||
|
||||
return x * x + y * y === z * z ? [[x, y, z]] : [];
|
||||
|
||||
})})});
|
||||
|
||||
|
||||
// LIBRARY FUNCTIONS
|
||||
|
||||
// Monadic bind for lists
|
||||
function mb(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
|
||||
// Monadic return for lists is simply lambda x -> [x]
|
||||
// as in [[x, y, z]] : [] above
|
||||
|
||||
// Integer range [m..n]
|
||||
function r(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1))
|
||||
.map(function (n, x) {
|
||||
return m + x;
|
||||
});
|
||||
}
|
||||
|
||||
})(100);
|
||||
Loading…
Add table
Add a link
Reference in a new issue