Skip to content

Commit a0cf462

Browse files
committed
clean up comments and some functions
1 parent 843110c commit a0cf462

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

src/Tablebot/Plugins/Roll/Dice/DiceStats.hs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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

1515
import Control.Monad.Exception
1616
import 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

4242
rangeExpr :: (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
190175
getDiceExperiment 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.
196180
rangeDiceExperiment :: (MonadException m) => Distribution -> [DieOpOption] -> m (DistributionSortedList -> DistributionSortedList)
197181
rangeDiceExperiment 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.
202186
rangeDieOpExperiment :: (MonadException m) => Distribution -> DieOpOption -> m (Endo DistributionSortedList)
203187
rangeDieOpExperiment 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.
223207
rangeDieOpExperimentKD :: (MonadException m) => KeepDrop -> LowHighWhere -> m (Endo DistributionSortedList)
224208
rangeDieOpExperimentKD 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

Comments
 (0)