Roman numerals/Encode: Difference between revisions

Content added Content deleted
m (→‎{{header|TypeScript}}: Comment: Weights and symbols in tuples.)
Line 3,610: Line 3,610:
"use strict";
"use strict";


// --------------- ROMAN INTEGER STRINGS ---------------
// -------------- ROMAN INTEGER STRINGS --------------


// roman :: Int -> String
// roman :: Int -> String
const roman = n =>
const roman = n =>
mapAccumL(residue => ([k, v]) =>
mapAccumL(residue =>
second(
([k, v]) => second(
q => 0 < q ? k.repeat(q) : ""
q => 0 < q ? (
)(
k.repeat(q)
swap(quotRem(residue)(v))
) : ""
)
)(remQuot(residue)(v))
)(n)(
)(n)(
zip([
zip([
Line 3,631: Line 3,631:
.join("");
.join("");


// ---------------------- TEST -----------------------


// ---------------------- TEST -----------------------
// main :: IO ()
// main :: IO ()
const main = () => (
const main = () => (
Line 3,640: Line 3,640:


// ---------------- GENERIC FUNCTIONS ----------------
// ---------------- 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 ->
// mapAccumL :: (acc -> x -> (acc, y)) -> acc ->
Line 3,661: Line 3,651:
const tpl = f(a[0])(x);
const tpl = f(a[0])(x);


return Tuple(tpl[0])(
return [
tpl[0],
a[1].concat(tpl[1])
a[1].concat(tpl[1])
);
];
},
},
Tuple(acc)([])
[acc, []]
);
);




// quotRem :: Int -> Int -> (Int, Int)
// remQuot :: Int -> Int -> (Int, Int)
const quotRem = m =>
const remQuot = m =>
n => Tuple(Math.trunc(m / n))(
n => [m % n, Math.trunc(m / n)];
m % n
);




Line 3,681: Line 3,670:
// to a function over a tuple.
// to a function over a tuple.
// f (a, b) -> (a, f(b))
// f (a, b) -> (a, f(b))
xy => Tuple(xy[0])(
xy => [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: Line 3,680:
length: Math.min(xs.length, ys.length)
length: Math.min(xs.length, ys.length)
}, (_, i) => [xs[i], ys[i]]);
}, (_, i) => [xs[i], ys[i]]);



// MAIN --
// MAIN --