Base58Check encoding: Difference between revisions

→‎{{header|Haskell}}: Updated 2nd Haskell version (for parallel with Python)
(→‎{{header|Python}}: Added a functionally composed version (pylinted for Python 3.7))
(→‎{{header|Haskell}}: Updated 2nd Haskell version (for parallel with Python))
Line 350:
EJDM8drfXA6uyA
Rt5zm</pre>
 
 
and for bulk encoding, Array access would be one of various slightly faster alternatives to recursive subscripting of linked lists:
{{Trans|Python}}
<lang Haskell>import Numeric (showIntAtBase)
<lang Haskell>import Data.Array (Array, (!), listArray)
<lang Haskell>import Numeric (showHex, showIntAtBase)
 
(putStrLn . base58Encode)
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
base58Encode :: (Show a, Integral a) => a -> String
base58Encode n = showIntAtBase 58 (base58Codes !) n ""
=> Array Int Char -> a -> String
baseEncode cs n = showIntAtBase (fromIntegral $ length cs) (cs !) n []
 
-- TEST ---------------------------------------------------
main :: IO ()
main =
putStrLn $
mapM_
fTable
(putStrLn . base58Encode)
"Base 58 encoding:\n"
(\x -> '0' : 'x' : showHex x [])
base58Encode
id
[ 25420294593250030202636073700053352635053786165627414518
, 0x61
Line 376 ⟶ 390:
, 0xecac89cad93923c02321
, 0x10c8511e
]</lang>
 
-- 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}}
Rt5zm</pre>
<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
Rt5zm</pre>
0x10c8511e -> Rt5zm</pre>
 
=={{header|Java}}==
9,655

edits