Binary digits: Difference between revisions
m
→JS ES6: Tidied general version
m (→JS ES6: Tidied) |
m (→JS ES6: Tidied general version) |
||
Line 2,801:
<lang JavaScript>(() => {
"use strict";
// showBin :: Int -> String▼
const showBin = n => {▼
};▼
const showIntAtBase = base =>
// A string representation of n, in the given base,
// using a supplied (Int -> Char) function for digits,
// and a supplied suffix string.
▲ const showIntAtBase = (base, toChr, n, rs) => {
▲ return n !== 0 ? (
▲ showIt(quotRem(n, base), r_)
▲ ) : r_;
};▼
return base <= 1 ? (▼
'error: showIntAtBase applied to unsupported base'▼
▲ ) : n < 0 ? (
'error: showIntAtBase applied to negative number'▼
) : showIt(quotRem(n, base), rs);▼
// quotRem :: Integral a => a -> a -> (a, a)▼
const quotRem = (m, n) => [Math.floor(m / n), m % n];▼
// GENERIC FUNCTIONS FOR TEST ---------------------------------------------▼
`${e} unsupported base`
) : 0 > n ? (
▲ };
const
const showHanBinary = n =>
showIntAtBase(2)(
x => 0 !== x ? (
"一"
) : "〇"
)(n)("");
n => `${n} -> ${showHanBinary(n)}`
)
▲ };
▲ // unlines :: [String] -> String
▲ const unlines = xs => xs.join('\n');
// --------------------- GENERIC ---------------------
▲ // quotRem :: Integral a => a -> a -> (a, a)
// The quotient, tupled with the remainder.
▲ // TEST -------------------------------------------------------------------
return main();
})();</lang>
{{Out}}
|