Base58Check encoding: Difference between revisions
Content added Content deleted
(→{{header|Python}}: Added a functionally composed version (pylinted for Python 3.7)) |
(→{{header|Haskell}}: Updated 2nd Haskell version (for parallel with Python)) |
||
Line 350: | Line 350: | ||
EJDM8drfXA6uyA |
EJDM8drfXA6uyA |
||
Rt5zm</pre> |
Rt5zm</pre> |
||
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists: |
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists: |
||
{{Trans|Python}} |
|||
⚫ | |||
import Data.Array |
<lang Haskell>import Data.Array (Array, (!), listArray) |
||
⚫ | |||
⚫ | |||
base58Codes :: (Num i, Ix i) => Array i Char |
|||
:: (Integral a, Show a) |
|||
base58Codes = |
|||
=> a -> String |
|||
listArray (0, 57) "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" |
|||
base58Encode = |
|||
baseEncode $ |
|||
listArray (0, 57) $ |
|||
['1' .. '9'] ++ |
|||
['A' .. 'H'] ++ ['J' .. 'N'] ++ ['P' .. 'Z'] ++ ['a' .. 'k'] ++ ['m' .. 'z'] |
|||
baseEncode |
|||
:: (Show a, Integral a) |
|||
base58Encode n = showIntAtBase 58 (base58Codes !) n "" |
|||
=> Array Int Char -> a -> String |
|||
baseEncode cs n = showIntAtBase (fromIntegral $ length cs) (cs !) n [] |
|||
-- TEST --------------------------------------------------- |
|||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
putStrLn $ |
|||
mapM_ |
|||
fTable |
|||
⚫ | |||
"Base 58 encoding:\n" |
|||
(\x -> '0' : 'x' : showHex x []) |
|||
base58Encode |
|||
id |
|||
[ 25420294593250030202636073700053352635053786165627414518 |
[ 25420294593250030202636073700053352635053786165627414518 |
||
, 0x61 |
, 0x61 |
||
Line 376: | Line 390: | ||
, 0xecac89cad93923c02321 |
, 0xecac89cad93923c02321 |
||
, 0x10c8511e |
, 0x10c8511e |
||
] |
] |
||
-- OUTPUT FORMATTING -------------------------------------- |
|||
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String |
|||
fTable s xShow fxShow f xs = |
|||
let w = maximum $ fmap length (xShow <$> xs) |
|||
rjust n c s = drop (length s) (replicate n c ++ s) |
|||
in unlines $ |
|||
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang> |
|||
{{Out}} |
{{Out}} |
||
⚫ | |||
<pre>6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM |
|||
0x10966776006953d5567439e5e39f86a0d273beed61967f6 -> 6UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM |
|||
2g |
|||
0x61 -> 2g |
|||
a3gV |
|||
0x626262 -> a3gV |
|||
aPEr |
|||
0x636363 -> aPEr |
|||
2cFupjhnEsSn59qHXstmK2ffpLv2 |
|||
0x73696d706c792061206c6f6e6720737472696e67 -> 2cFupjhnEsSn59qHXstmK2ffpLv2 |
|||
ABnLTmg |
|||
0x516b6fcd0f -> ABnLTmg |
|||
3SEo3LWLoPntC |
|||
0xbf4f89001e670274dd -> 3SEo3LWLoPntC |
|||
3EFU7m |
|||
0x572e4794 -> 3EFU7m |
|||
EJDM8drfXA6uyA |
|||
0xecac89cad93923c02321 -> EJDM8drfXA6uyA |
|||
⚫ | |||
0x10c8511e -> Rt5zm</pre> |
|||
=={{header|Java}}== |
=={{header|Java}}== |