File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed
Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change 1+ {-# LANGUAGE FlexibleContexts #-}
2+ module StateList where
3+
4+ import Control.Monad.Except
5+ import Control.Monad.State
6+ import Data.Foldable
7+
8+
9+ limited :: (MonadError Int m , MonadState s m ) => (s -> Bool ) -> [State s a ] -> m [a ]
10+ limited p fs = traverse limit1 (zip [0 .. ] fs) where
11+ limit1 (i, f) = do
12+ a <- state (runState f)
13+ stateIsBad <- gets (not . p)
14+ when stateIsBad $ throwError i
15+ return a
16+
17+
18+ runLimited1 :: (s -> Bool ) -> [State s a ] -> s -> (Either Int [a ], s )
19+ runLimited1 p fs s = run1 (limited p fs) s
20+
21+
22+ runLimited2 :: (s -> Bool ) -> [State s a ] -> s -> Either Int ([a ], s )
23+ runLimited2 p fs s = run2 (limited p fs) s
24+
25+
26+ run1 :: ExceptT Int (State s ) [a ] -> s -> (Either Int [a ], s )
27+ run1 m s = runState (runExceptT m) s
28+
29+
30+ run2 :: StateT s (Except Int ) [a ] -> s -> Either Int ([a ], s )
31+ run2 m s = runExcept $ runStateT m s
You can’t perform that action at this time.
0 commit comments