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 :: Int -> String |
// roman :: Int -> String |
||
const roman = n => |
const roman = n => |
||
mapAccumL(residue |
mapAccumL(residue => |
||
second( |
([k, v]) => second( |
||
q => 0 < q ? |
q => 0 < q ? ( |
||
k.repeat(q) |
|||
) : "" |
|||
) |
)(remQuot(residue)(v)) |
||
)(n)( |
)(n)( |
||
zip([ |
zip([ |
||
Line 3,631: | Line 3,631: | ||
.join(""); |
.join(""); |
||
⚫ | |||
⚫ | |||
// 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 => ({ |
|||
⚫ | |||
"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 |
return [ |
||
⚫ | |||
a[1].concat(tpl[1]) |
a[1].concat(tpl[1]) |
||
]; |
|||
}, |
}, |
||
[acc, []] |
|||
); |
); |
||
// |
// remQuot :: Int -> Int -> (Int, Int) |
||
const |
const remQuot = 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 => |
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 -- |