Created
September 1, 2012 14:10
-
-
Save dtchepak/3574228 to your computer and use it in GitHub Desktop.
replacing field contents with random strings
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
import Control.Monad.State | |
import System.Random | |
type Field = String | |
replaceField :: String -> Field -> Field | |
replaceField newContent field = | |
let fieldName = takeWhile (/=':') field | |
in fieldName ++ ": " ++ newContent | |
fieldUpdate :: RandomGen g => Field -> State g Field | |
fieldUpdate f = do | |
newContent <- randomString | |
return $ replaceField newContent f | |
updateAll :: RandomGen g => [Field] -> g -> [Field] | |
updateAll = evalState . mapM fieldUpdate | |
randomChar :: (RandomGen g) => State g Char | |
randomChar = state $ randomR ('a','z') | |
randomStringWithLength :: (RandomGen g) => Int -> State g String | |
randomStringWithLength i = replicateM i randomChar | |
randomString :: (RandomGen g) => State g String | |
randomString = state (randomR (1,10)) >>= randomStringWithLength | |
input :: [Field] | |
input = ["first: hello", "second: world", "third: !!!"] | |
-- ghci> updateAll input (mkStdGen 321) | |
-- ["first: ddoxhpps","second: th","third: lryt"] | |
-- ghci> updateAll input (mkStdGen 3212) | |
-- ["first: ct","second: pohjx","third: klryey"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment