1010--
1111-- This plugin generates statistics based on the values of dice in given
1212-- expressions.
13- module Tablebot.Plugins.Roll.Dice.DiceStats (rangeExpr , rangeListValues , getStats ) where
13+ module Tablebot.Plugins.Roll.Dice.DiceStats (rangeExpr , getStats ) where
1414
1515import Control.Monad.Exception
1616import Data.Bifunctor
@@ -40,22 +40,7 @@ getStats d = (modalOrder, D.expectation d, D.standardDeviation d)
4040 modalOrder = fst <$> sortBy (\ (_, r) (_, r') -> compare r' r) vals
4141
4242rangeExpr :: (MonadException m ) => Expr -> m Distribution
43- rangeExpr e = do
44- ex <- range e
45- return $ ex
46-
47- rangeListValues :: (MonadException m ) => ListValues -> m [Distribution ]
48- rangeListValues lv = do
49- lve <- rangeList lv
50- let lvd = lve
51- lvd' = D. toList lvd
52- return $ D. fromList <$> zip' lvd'
53- where
54- head' [] = []
55- head' (x : _) = [x]
56- getHeads xs = (\ (xs', p) -> (,p) <$> head' xs') =<< xs
57- getTails xs = first (drop 1 ) <$> xs
58- zip' xs = getHeads xs : zip' (getTails xs)
43+ rangeExpr = range
5944
6045-- | Try and get the `Distribution` of the given value, throwing a
6146-- `MonadException` on failure.
@@ -190,15 +175,14 @@ getDiceExperiment :: Integer -> Distribution -> DistributionSortedList
190175getDiceExperiment i d =
191176 DM. sequenceSL (replicate (fromInteger i) d)
192177
193- -- | Go through each operator on dice and modify the `Experiment` representing
194- -- all possible collections of rolls, returning the `Experiment` produced on
195- -- finding `Nothing`.
178+ -- | Go through each operator on dice and modify the distribution of values
179+ -- based on those operations.
196180rangeDiceExperiment :: (MonadException m ) => Distribution -> [DieOpOption ] -> m (DistributionSortedList -> DistributionSortedList )
197181rangeDiceExperiment die =
198182 fmap (appEndo . fold) . traverse (rangeDieOpExperiment die)
199183
200- -- | Perform one dice operation on the given `Experiment`, possibly returning
201- -- a modified experiment representing the distribution of dice rolls.
184+ -- | Perform one dice operation on a set of values, returning
185+ -- a modified distribution of dice rolls.
202186rangeDieOpExperiment :: (MonadException m ) => Distribution -> DieOpOption -> m (Endo DistributionSortedList )
203187rangeDieOpExperiment die (MkDieOpOption doo) = case doo of
204188 (DieOpOptionLazy o) -> rangeDieOpExperiment die (MkDieOpOption o)
@@ -219,7 +203,7 @@ rangeDieOpExperiment die (MkDieOpOption doo) = case doo of
219203 | otherwise = D. assuming (\ i -> not $ applyCompare cond i limitValue) die
220204 countTriggers limitValue = foldr (\ i ~ (c, xs') -> if applyCompare cond i limitValue then (c + 1 , xs') else (c, i `SL.insert` xs')) (0 , mempty )
221205
222- -- | Perform a keep/drop operation on the `Experiment` of dice rolls.
206+ -- | Perform a keep/drop operation on the dice rolls.
223207rangeDieOpExperimentKD :: (MonadException m ) => KeepDrop -> LowHighWhere -> m (Endo DistributionSortedList )
224208rangeDieOpExperimentKD kd (Where cond nb) = do
225209 nbDis <- range nb
@@ -284,4 +268,10 @@ rangeFunction fi exprs = do
284268 let params = first (funcInfoFunc fi) <$> D. toList (DM. sequence exprs')
285269 D. fromList <$> foldAndIgnoreErrors params
286270 where
287- foldAndIgnoreErrors = foldr (\ (mv, p) mb -> catchBot ((: [] ) . (,p) <$> mv) (const (return [] )) >>= \ v -> mb >>= \ b -> return (v ++ b)) (return [] )
271+ foldAndIgnoreErrors = foldr foldrFunc (return [] )
272+ foldrFunc :: (MonadException m ) => (m a , t ) -> m [(a , t )] -> m [(a , t )]
273+ foldrFunc (mv, p) mb = do
274+ -- try to execute the result of each function, and if it throws an
275+ -- exception we cancel out the exception and say that it never happened
276+ v <- catchBot (pure . (,p) <$> mv) (\ _ -> pure [] )
277+ (v <> ) <$> mb
0 commit comments