Created
July 25, 2013 01:55
-
-
Save lambda-fairy/6076273 to your computer and use it in GitHub Desktop.
Python identifier generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Names (renderName) where | |
import Data.List (genericIndex) | |
-- | Map a non-negative integer to a valid Python identifier. | |
renderName :: Integer -> String | |
renderName | |
= ('_':) -- Prevent conflict with Python keywords | |
. map (genericIndex chars) | |
. digitify (fromIntegral $ length chars) | |
chars = '_' : ['a'..'z'] | |
digitify :: Integer -> Integer -> [Integer] | |
digitify limit = reverse . step . (+1) | |
where | |
step x | |
| x <= 0 = [] | |
| otherwise = i : step x' | |
where | |
(x', i) = (x-1) `divMod` limit | |
main = mapM_ (putStrLn . renderName) [0..755] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment