Binary digits: Difference between revisions
Content added Content deleted
(→{{header|AppleScript}}: updated primitives) |
(→{{header|JavaScript}}: Simple ES6 example (wrapping String.toString(base)) |
||
Line 1,451: | Line 1,451: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
===ES5=== |
|||
<lang javascript>function toBinary(number) { |
<lang javascript>function toBinary(number) { |
||
return new Number(number).toString(2); |
return new Number(number).toString(2); |
||
Line 1,459: | Line 1,460: | ||
}</lang> |
}</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> |
<lang JavaScript>(() => { |
||
// showIntAtBase_ :: // Int -> Int -> String |
|||
⚫ | |||
const showIntAtBase_ = (base, n) => (n) |
|||
.toString(base); |
|||
// showBin :: Int -> String |
|||
⚫ | |||
const showBin = n => showIntAtBase_(2, n); |
|||
// GENERIC FUNCTIONS FOR TEST --------------------------------------------- |
|||
Output: |
|||
⚫ | |||
// intercalate :: String -> [a] -> String |
|||
⚫ | |||
const intercalate = (s, xs) => xs.join(s); |
|||
⚫ | |||
// 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)]), |
|||
⚫ | |||
)); |
|||
⚫ | |||
{{Out}} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
=={{header|Joy}}== |
=={{header|Joy}}== |