monad-ox 0.2.0 → 0.3.0
raw patch · 2 files changed
+40/−34 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Control.Monad.Ox: atWith :: Vector t -> (t -> a) -> Int -> Maybe a
+ Control.Monad.Ox: atWith :: Vector a -> (a -> b) -> Int -> Maybe b
- Control.Monad.Ox: atsWith :: Vector t -> (t -> [a]) -> Int -> [a]
+ Control.Monad.Ox: atsWith :: Vector a -> (a -> [b]) -> Int -> [b]
- Control.Monad.Ox: execOx :: Ox t w a -> [(Id, w)]
+ Control.Monad.Ox: execOx :: Ox o a -> [(Id, o)]
- Control.Monad.Ox: group :: Ox t w a -> Ox t w a
+ Control.Monad.Ox: group :: Ox o a -> Ox o a
- Control.Monad.Ox: save :: Maybe w -> Ox t w ()
+ Control.Monad.Ox: save :: Maybe o -> Ox o ()
- Control.Monad.Ox: saves :: [w] -> Ox t w ()
+ Control.Monad.Ox: saves :: [o] -> Ox o ()
- Control.Monad.Ox: type Ox t w a = WriterT [(Id, w)] (State Id) a
+ Control.Monad.Ox: type Ox o a = WriterT [(Id, o)] (State Id) a
- Control.Monad.Ox: when :: Bool -> Ox t w a -> Ox t w (Maybe a)
+ Control.Monad.Ox: when :: Bool -> Ox o a -> Ox o (Maybe a)
- Control.Monad.Ox: whenJT :: Maybe Bool -> Ox t w a -> Ox t w (Maybe a)
+ Control.Monad.Ox: whenJT :: Maybe Bool -> Ox o a -> Ox o (Maybe a)
Files
- Control/Monad/Ox.hs +39/−33
- monad-ox.cabal +1/−1
Control/Monad/Ox.hs view
@@ -8,8 +8,6 @@ , Id -- * Functions-, atWith-, atsWith , save , saves , when@@ -18,6 +16,10 @@ -- * Ox monad execution , execOx++-- * Utilities+, atWith+, atsWith ) where import Control.Applicative ((<$>), (<*), (*>))@@ -59,69 +61,51 @@ -- | The Ox is a monad stack with observation type identifier handled by -- the state monad and the resulting observation values paired with identifiers -- printed using the writer monad.-type Ox t w a = WriterT [(Id, w)] (State Id) a+type Ox o a = WriterT [(Id, o)] (State Id) a -- | Retrieve the current identifier value.-getId :: Ox t w Id+getId :: Ox o Id getId = lift get {-# INLINE getId #-} -- | Set the new identifier value.-setId :: Id -> Ox t w ()+setId :: Id -> Ox o () setId = lift . put {-# INLINE setId #-} -- | Update the current identifier of the Ox monad.-updateId :: (Id -> Id) -> Ox t w ()+updateId :: (Id -> Id) -> Ox o () updateId f = do i <- getId setId (f i) -- | Increase the current identifier of the Ox monad.-incId :: Ox t w ()+incId :: Ox o () incId = updateId inc -- | Perform the identifier-dependent action and increase the identifier.-withId :: (Id -> Ox t w a) -> Ox t w a+withId :: (Id -> Ox o a) -> Ox o a withId act = do x <- act =<< getId incId return x -- | Perform the Ox action on the lower level.-below :: Ox t w a -> Ox t w a+below :: Ox o a -> Ox o a below act = updateId grow *> act <* updateId shrink --- | Value of the 't -> a' function with respect to the given sentence--- and sentence position. Return Nothing if the position is out of--- bounds.-atWith :: V.Vector t -> (t -> a) -> Int -> Maybe a-atWith xs f k =- if k < 0 || k >= V.length xs- then Nothing- else Just $ f (xs V.! k)---- | Value of the 't -> [a]' function with respect to the given sentence--- and sentence position. Return empty list if the position is out of--- bounds.-atsWith :: V.Vector t -> (t -> [a]) -> Int -> [a]-atsWith xs f k =- if k < 0 || k >= V.length xs- then []- else f (xs V.! k)- -- | Save observation values in the writer monad of the Ox stack.-saves :: [w] -> Ox t w ()+saves :: [o] -> Ox o () saves xs = withId $ \i -> tell [(i, x) | x <- xs] -- | Save the observation value.-save :: Maybe w -> Ox t w ()+save :: Maybe o -> Ox o () save = saves . maybeToList -- | Perform the Ox action only when the 'cond' is True. It works like -- the standard 'Control.Monad.when' function but also changes the current -- identifier value.-when :: Bool -> Ox t w a -> Ox t w (Maybe a)+when :: Bool -> Ox o a -> Ox o (Maybe a) when cond act = do x <- case cond of False -> return Nothing@@ -130,7 +114,7 @@ return x -- | Perform the action only when the given condition is equal to Just True.-whenJT :: Maybe Bool -> Ox t w a -> Ox t w (Maybe a)+whenJT :: Maybe Bool -> Ox o a -> Ox o (Maybe a) whenJT cond = when (justTrue cond) where@@ -140,7 +124,7 @@ -- | Make all embedded observations to be indistinguishable with respect -- to their top-most identifier components. -- TODO: Perhaps should set only the current level, not the deeper ones.-group :: Ox t w a -> Ox t w a+group :: Ox o a -> Ox o a group act = do i <- getId let top = getTop i@@ -150,7 +134,29 @@ -- | Execute the Ox monad and retrieve the saved (with the 'save' and -- 'saves' functions) results.-execOx :: Ox t w a -> [(Id, w)]+execOx :: Ox o a -> [(Id, o)] execOx ox = (map (first reverse) . fst) (runState (execWriterT ox) [1])++------------------------------+-- Utilities+------------------------------++-- | Value of the 't -> a' function with respect to the given sentence+-- and sentence position. Return Nothing if the position is out of+-- bounds.+atWith :: V.Vector a -> (a -> b) -> Int -> Maybe b+atWith xs f k =+ if k < 0 || k >= V.length xs+ then Nothing+ else Just $ f (xs V.! k)++-- | Value of the 't -> [a]' function with respect to the given sentence+-- and sentence position. Return empty list if the position is out of+-- bounds.+atsWith :: V.Vector a -> (a -> [b]) -> Int -> [b]+atsWith xs f k =+ if k < 0 || k >= V.length xs+ then []+ else f (xs V.! k)
monad-ox.cabal view
@@ -1,5 +1,5 @@ name: monad-ox-version: 0.2.0+version: 0.3.0 synopsis: Monad for observation extraction description: The library provides an Ox monad and accompanying functions which