Jump to content

Binary digits: Difference between revisions

→‎{{header|JavaScript}}: Simple ES6 example (wrapping String.toString(base)
(→‎{{header|AppleScript}}: updated primitives)
(→‎{{header|JavaScript}}: Simple ES6 example (wrapping String.toString(base))
Line 1,451:
 
=={{header|JavaScript}}==
===ES5===
<lang javascript>function toBinary(number) {
return new Number(number).toString(2);
Line 1,459 ⟶ 1,460:
}</lang>
 
===ES6===
Or, as a functional expression, rather than a statement:
The simplest showBin (or showIntAtBase), using default digit characters, would use JavaScript's standard String.toString(base):
 
<lang JavaScript>console.log(() => {
 
// showIntAtBase_ :: // Int -> Int -> String
[5, 50, 9000].map(function (n) {
const showIntAtBase_ return= (base, n).toString => (2n);
}) .jointoString('\n'base);
 
// showBin :: Int -> String
)</lang>
const showBin = n => showIntAtBase_(2, n);
 
// GENERIC FUNCTIONS FOR TEST ---------------------------------------------
Output:
 
<pre>101
// intercalate :: String -> [a] -> String
110010
const intercalate = (s, xs) => xs.join(s);
10001100101000</pre>
 
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
 
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
 
// show :: a -> String
const show = x => JSON.stringify(x);
 
// TEST -------------------------------------------------------------------
 
return unlines(map(
n => intercalate(' -> ', [show(n), showBin(n)]),
[5, 50, 9000].map(function (n) {
));
})();</lang>
{{Out}}
<pre>5 -> 101
50 -> 110010
9000 -> 10001100101000</pre>
 
=={{header|Joy}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.