---------------------------------------------------------------------------------------------------
-- |
-- Module      :  Text.Bravo.Util
-- Copyright   :  Matthias Reisner
-- License     :  BSD3
--
-- Maintainer  :  Matthias Reisner <matthias.reisner@googlemail.com>
-- Stability   :  experimental
-- Portability :  unknown
--
-- Bravo utility functions.
--
---------------------------------------------------------------------------------------------------

module Text.Bravo.Util (
        (<<),
        skip,
        liftM',
        partition',
        safeReadFile
    )
where


import Control.Monad

import Data.List


infixl 1 <<

(<<) :: Monad m => m a -> m b -> m a
(<<) = liftM2 const

skip :: Monad m => m a -> m ()
skip = liftM $ const ()

liftM' :: Monad m => (a -> m b) -> m a -> m b
liftM' = (=<<)

partition' :: Eq a => (a -> a -> Bool) -> [a] -> [[a]]
partition' _ []     = []
partition' p (c:cs) = (c : cs') : partition' p ncs
    where (cs', ncs) = partition (p c) cs

safeReadFile :: FilePath -> IO (Maybe String)
safeReadFile path = catch (liftM Just $ readFile path) (\_ -> return Nothing)