module Ella.GenUtils
where
import Control.Monad (liftM)
import Data.ByteString.Lazy (ByteString)
import GHC.Exts( IsString(..) )
import Random (randomRs, newStdGen)
import System.Posix.Time (epochTime)
import qualified Data.ByteString.Lazy.UTF8 as UTF8
utf8 :: String -> ByteString
utf8 = UTF8.fromString
apply :: [a -> a]
-> a
-> a
apply = flip with
with :: a -> [a -> a] -> a
with = foldl (flip ($))
exactParse :: (Read a, Monad m) => String -> m a
exactParse s =
case reads s of
[(val, "")] -> return val
otherwise -> fail ("Cannot parse '" ++ s ++ "'")
nullToNothing :: String -> Maybe String
nullToNothing "" = Nothing
nullToNothing x = Just x
getTimestamp :: IO Int
getTimestamp = liftM (floor . toRational) epochTime
randomStr :: Int -> IO String
randomStr n = do
g <- newStdGen
return $ take n (randomRs chars g)
where chars = ('a','z')