Binary digits: Difference between revisions
Content added Content deleted
m (→JS ES6: Tidied) |
m (→JS ES6: Tidied general version) |
||
Line 2,801: | Line 2,801: | ||
<lang JavaScript>(() => { |
<lang JavaScript>(() => { |
||
"use strict"; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
const binaryChar = n => n !== 0 ? '一' : '〇'; |
|||
⚫ | |||
return showIntAtBase(2, binaryChar, 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. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return 0 !== x ? ( |
|||
⚫ | |||
⚫ | |||
) : r_; |
|||
}; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
}; |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
`${e} unsupported base` |
|||
) : 0 > n ? ( |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// intercalate :: String -> [a] -> String |
|||
const |
const main = () => { |
||
⚫ | |||
const showHanBinary = n => |
|||
showIntAtBase(2)( |
|||
x => 0 !== x ? ( |
|||
"一" |
|||
) : "〇" |
|||
)(n)(""); |
|||
return [5, 50, 9000] |
|||
.map( |
|||
n => `${n} -> ${showHanBinary(n)}` |
|||
) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
// --------------------- GENERIC --------------------- |
|||
// show :: a -> String |
|||
const show = x => JSON.stringify(x); |
|||
⚫ | |||
⚫ | |||
// The quotient, tupled with the remainder. |
|||
⚫ | |||
⚫ | |||
// MAIN --- |
|||
return main(); |
|||
n => intercalate(' -> ', [show(n), showBin(n)]), [5, 50, 9000] |
|||
)); |
|||
})();</lang> |
})();</lang> |
||
{{Out}} |
{{Out}} |