Skip to content

Instantly share code, notes, and snippets.

@benkolera
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save benkolera/c26535c3705082aa4dfe to your computer and use it in GitHub Desktop.

Select an option

Save benkolera/c26535c3705082aa4dfe to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Data.List.NonEmpty as NEL
import Data.Monoid ((<>))
import qualified Data.Text as T
import Data.Text (Text)
chars :: NonEmpty Char
chars = NEL.fromList $ filter (not . (`elem` "OolI"))
$ ['0'..'9']
<> ['a'..'z']
<> ['A'..'Z']
convert :: NonEmpty Char -> Int -> Int -> Text
convert chars width = pad . convert'
where
convert' 0 = T.singleton $ zero
convert' i = T.unfoldr step i
zero = NEL.head chars
highest = NEL.length chars
pad = T.justifyRight width zero
step 0 = Nothing
step x = Just $
( (chars NEL.!! (x `mod` highest))
, x `div` highest
)
λ> traverse_ (print . convert chars 5) [1..60]
"00001"
"00002"
"00003"
"00003"
"00004"
"00005"
"00006"
"00007"
"00008"
"00009"
"0000a"
"0000b"
"0000c"
"0000d"
"0000e"
"0000f"
"0000g"
"0000h"
"0000i"
"0000j"
"0000k"
"0000m"
"0000n"
"0000p"
"0000q"
"0000r"
"0000s"
"0000t"
"0000u"
"0000v"
"0000w"
"0000x"
"0000y"
"0000z"
"0000A"
"0000B"
"0000C"
"0000D"
"0000E"
"0000F"
"0000G"
"0000H"
"0000J"
"0000K"
"0000L"
"0000M"
"0000N"
"0000P"
"0000Q"
"0000R"
"0000S"
"0000T"
"0000U"
"0000V"
"0000W"
"0000X"
"0000Y"
"0000Z"
"00001"
"00011"
"00021"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment