Binary digits: Difference between revisions

m
→‎JS ES6: Tidied
No edit summary
m (→‎JS ES6: Tidied)
Line 2,767:
 
===ES6===
The simplest showBinshowBinary (or showIntAtBase), using default digit characters, would use JavaScript's standard String.toString(base):
 
<lang JavaScript>(() => {
"use strict";
 
// TEST ------------------------------------------------- BINARY DIGITS ------------------
// showIntAtBase_ :: // Int -> Int -> String
const showIntAtBase_ = (base, n) => (n)
.toString(base);
 
// showBinshowBinary :: Int -> String
const showBinshowBinary = n => showIntAtBase_(2, n);
showIntAtBase_(2)(n);
 
// GENERIC FUNCTIONS FOR TEST ---------------------------------------------
 
// intercalateshowIntAtBase_ :: String// Int -> [a]Int -> String
const intercalateshowIntAtBase_ = (s, xs)base => xs.join(s);
n => n.toString(base);
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
 
// GENERIC FUNCTIONS FOR TEST ---------------------- TEST -----------------------
// show :: a -> String
const showmain = x() => JSON.stringify(x);[5, 50, 9000]
.map(n => `${n} -> ${showBinary(n)}`)
const unlines = xs => xs.join('"\n'");
 
// TEST -------------------------------------------------------------------
 
return// unlines(map(MAIN ---
return main();
n => intercalate(' -> ', [show(n), showBin(n)]),
[5, 50, 9000]
));
})();</lang>
{{Out}}
9,659

edits