Roman numerals/Encode: Difference between revisions

m (→‎{{header|TypeScript}}: Comment: Weights and symbols in tuples.)
Line 3,610:
"use strict";
 
// --------------- ROMAN INTEGER STRINGS ---------------
 
// roman :: Int -> String
const roman = n =>
mapAccumL(residue => ([k, v]) =>
([k, v]) => second(
q => 0 < q ? k.repeat(q) : ""
) k.repeat(q)
swap(quotRem(residue)(v)) : ""
)(remQuot(residue)(v))
)(n)(
zip([
Line 3,631:
.join("");
 
// ---------------------- TEST -----------------------
 
// ---------------------- TEST -----------------------
// main :: IO ()
const main = () => (
Line 3,640:
 
// ---------------- GENERIC FUNCTIONS ----------------
 
// Tuple (,) :: a -> b -> (a, b)
const Tuple = a =>
b => ({
type: "Tuple",
"0": a,
"1": b,
length: 2
});
 
 
// mapAccumL :: (acc -> x -> (acc, y)) -> acc ->
Line 3,661 ⟶ 3,651:
const tpl = f(a[0])(x);
 
return Tuple(tpl[0])(
type: "Tuple" tpl[0],
a[1].concat(tpl[1])
)];
},
Tuple([acc)(, [])]
);
 
 
// quotRemremQuot :: Int -> Int -> (Int, Int)
const quotRemremQuot = m =>
n => Tuple([m % n, Math.trunc(m / n))(];
m % n
);
 
 
Line 3,681 ⟶ 3,670:
// to a function over a tuple.
// f (a, b) -> (a, f(b))
xy => Tuple([xy[0]), f(xy[1])];
f(xy[1])
);
 
 
// swap :: (a, b) -> (b, a)
const swap = ab =>
// The pair ab with its order reversed.
Tuple(ab[1])(
ab[0]
);
 
 
Line 3,701 ⟶ 3,680:
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
 
 
// MAIN --
9,655

edits