Jump to content

Base58Check encoding: Difference between revisions

m
m (→‎version 3: aligned comment with assignment value.)
Line 417:
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 Data.Array (Array, listArray, (!), listArray)
import Numeric (showHex, showIntAtBase)
 
------------------- BASE58CHECK ENCODING -----------------
base58Encode
 
:: (Integral a, Show a)
base58Encode ::
=> a -> String
:: (Integral a, Show a) =>
a ->
String
base58Encode =
baseEncode $
listArray (0, 57) $
[('1', '9'), ('A', 'H'), ('J', 'N'),[ ('P1', 'Z9'), ('a', 'k'), ('m', 'z')] >>=
('A', 'H'),
uncurry enumFromTo
('J', 'N'),
('P', 'Z'),
('a', 'k'),
('m', 'z')
, 0x61 ]
>>= uncurry enumFromTo
 
baseEncode ::
:: (Show a, Integral a) =>
=> Array Int Char -> a -> String
a ->
baseEncode cs n = showIntAtBase (fromIntegral $ length cs) (cs !) n []
String
baseEncode cs n =
showIntAtBase
baseEncode cs n = showIntAtBase (fromIntegral $ length cs) (cs !) n []
(cs !)
n
[]
 
-- TEST -------------------------- TEST -------------------------
main :: IO ()
main =
putStrLn $
fTable
"Base 58 encoding:\n"
--(\x -> '0' : 'x' : showHex x [])
(("0x" <>) . flip showHex [])
base58Encode
id base58Encode
id
[ 25420294593250030202636073700053352635053786165627414518,
, 0x61
, 0x626262 0x61,
, 0x636363 0x626262,
0x636363,
, 0x73696d706c792061206c6f6e6720737472696e67,
, 0x516b6fcd0f,
, 0xbf4f89001e670274dd,
, 0x572e4794,
, 0xecac89cad93923c02321,
, 0x10c8511e
] 0x10c8511e
]
 
-- OUTPUT FORMATTING ------------------- OUTPUT FORMATTING -------------------
fTable ::
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
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 :
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang>
fmap
( ((<>) . rjust w ' ' . xShow)
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++<>) . fxShow . f)) xs</lang>
)
xs</lang>
{{Out}}
<pre>Base 58 encoding:
9,659

edits

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