Roman numerals/Encode: Difference between revisions
m
→Functional JS: Tidied
(Add ed example) |
m (→Functional JS: Tidied) |
||
Line 4,412:
mapAccumL(residue =>
([k, v]) => second(
q => 0 < q
? k.repeat(q)
)(remQuot(residue)(v))
)(n)(
Line 4,432:
const main = () => (
[2016, 1990, 2008, 2000, 2020, 1666].map(roman)
)
.join("\n");
// ---------------- GENERIC FUNCTIONS ----------------
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
// A pair of values, possibly of
// different types.
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
yield this[k];
}
}
}
});
// mapAccumL :: (acc -> x -> (acc, y)) -> acc ->
// [x] -> (acc, [y])
const mapAccumL = f =>
acc => xs => [...xs].reduce(
([a, bs], x) =>
)(
▲ a[1].concat(tpl[1])
▲ ];
▲ },
[acc, []]
);
Line 4,463 ⟶ 4,480:
// second :: (a -> b) -> ((c, a) -> (c, b))
const second = f =>
xy =>
xy[0]
)(
f(xy[1])
);
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
ys => Array.from({
length: Math.min(xs.length, ys.length)
|