Roman numerals/Encode: Difference between revisions

m
(Add ed example)
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");
.join("\n");
 
 
// ---------------- GENERIC FUNCTIONS ----------------
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
// A pair of values, possibly of
// different types.
b => },({
type: ];"Tuple",
"0": a,
"1": b,
length: 2,
*[Symbol.iterator]() {
for (const k in this) {
a[1].concatif (tpl[1]!isNaN(k)) {
yield this[k];
}
}
}
});
 
 
// mapAccumL :: (acc -> x -> (acc, y)) -> acc ->
// [x] -> (acc, [y])
const mapAccumL = f =>
// A tuple of an accumulation and a list
// obtained by a combined map and fold,
// with accumulation from left to right.
acc => xs => [...xs].reduce(
([a, bs], x) => {second(
const tplv => f(a[0...bs, v])(x);
)(
 
return [f(a)(x)
tpl[0]),
a[1].concat(tpl[1])
];
},
[acc, []]
);
Line 4,463 ⟶ 4,480:
// second :: (a -> b) -> ((c, a) -> (c, b))
const second = f =>
// A function over a simple value lifted
// to a function over a tuple.
// f (a, b) -> (a, f(b))
xy => [xy[0], fTuple(xy[1])];
xy[0]
)(
f(xy[1])
);
 
 
// zip :: [a] -> [b] -> [(a, b)]
const zip = xs =>
// The paired members of xs and ys, up to
// the length of the shorter of the two lists.
ys => Array.from({
length: Math.min(xs.length, ys.length)
9,659

edits