Department numbers: Difference between revisions
m
→JS ES6
m (→JS ES6: Tidied.) |
m (→JS ES6) |
||
Line 2,334:
{{Trans|Haskell}}
<lang JavaScript>(() => {
//
// options :: Int -> Int -> Int -> [(Int, Int, Int)]
const options =
const
bind =
ds = enumFromTo(lo
return bind(ds.filter(even
x => bind(ds.filter(d => d !== x
y => bind([total - (x + y)]
z => (z !== y && lo <= z && z <= hi) ? [
[x, y, z]
Line 2,352:
)
)
);
};
//
const
n = solutions.length,
list = solutions
.map(JSON.stringify)
return (
);
};
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo =
n => Array.from({
length:
}, (_, i) => m + i);
Line 2,369 ⟶ 2,381:
const even = n => n % 2 === 0;
//
return main();
▲ const map = (f, xs) => xs.map(f);
▲ const unlines = xs => xs.join('\n');
▲ // TEST -------------------------------------------------------------------
▲ const xs = options(1, 7, 12);
▲ return '(Police, Sanitation, Fire)\n\n' +
▲ '\n\nNumber of options: ' + length(xs);
})();</lang>
{{Out}}
|