Source

ella / src / Ella / GenUtils.hs

-- | General utility functions that do not depend on other functions
-- in Web modules
module Ella.GenUtils

where

import Control.Monad (liftM)
import Data.ByteString.Lazy.Char8 (ByteString)
import GHC.Exts( IsString(..) )
import Random (randomRs, newStdGen)
import System.Posix.Time (epochTime)
import qualified Data.ByteString.Lazy.UTF8 as UTF8

-- | Takes a String and returns UTF8 ByteString
utf8 :: String -> ByteString
utf8 = UTF8.fromString

-- | Apply a list of transformation functions to a value
apply :: [a -> a] -- ^ List of functions
      -> a        -- ^ Initial value
      -> a
apply = flip with

-- | Same as apply with arguments flipped
with :: a -> [a -> a] -> a
with = foldl (flip ($))

-- | Parse a value, or fail if the entire string cannot be parsed
--
-- Note that this relies on 'reads', which appears to be buggy for Int
-- -- it works something like parsing as 'Integer', then wrapping to a 32 bit
-- Int.  The 'Param' instance for 'Int' has fixed this, so you can
-- use 'capture' instead.
exactParse :: (Read a, Monad m) => String -> m a
exactParse s =
  case reads s of
    [(val, "")] -> return val
    otherwise -> fail ("Cannot parse '" ++ s ++ "'")

-- | Converts empty string to Nothing, everything else wrapped in Just
nullToNothing :: String -> Maybe String
nullToNothing "" = Nothing
nullToNothing x  = Just x


-- | Return current time as a UNIX timestamp
getTimestamp :: IO Int
getTimestamp = liftM (floor . toRational) epochTime

-- | Returns a randomly generated string of length n
randomStr :: Int -> IO String
randomStr n = do
    g <- newStdGen
    return $ take n (randomRs chars g)
  where chars = ('a','z')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.