Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Created July 25, 2013 01:55
Show Gist options
  • Save lambda-fairy/6076273 to your computer and use it in GitHub Desktop.
Save lambda-fairy/6076273 to your computer and use it in GitHub Desktop.
Python identifier generator
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