Skip to content

Commit

Permalink
rationalise generation of error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
aristidb committed Sep 21, 2010
1 parent c7b5b9f commit 689d5d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 7 additions & 4 deletions Data/Random/Extras.hs
Expand Up @@ -47,6 +47,9 @@ import qualified Data.Array.IArray as Arr
import qualified Data.Array
import Data.Array.IArray ((!))

moduleError :: String -> String -> a
moduleError n s = error $ "Data.Random.Extras." ++ n ++ ": " ++ s

(.:) :: (c -> c') -> (a -> b -> c) -> (a -> b -> c')
(.:) = (.).(.)

Expand Down Expand Up @@ -128,7 +131,7 @@ choiceExtractSeq s | Seq.null s = Nothing
--
-- /Complexity:/ O(n), where /n/ is the length of the input list.
choice :: [a] -> RVar a
choice [] = error "Control.Monad.Random.Extras.choice: empty list"
choice [] = moduleError "choice" "empty list"
choice xs = (xs !!) `liftM` uniform 0 (length xs - 1)

-- | Safely select a random element from a list.
Expand All @@ -153,15 +156,15 @@ iterativeChoice xs = fst `liftM` foldl' stepM (return start) xs
| otherwise = old
return $! new `seq` (new, n + 1)
start = (err, 0 :: Int)
err = error "Control.Monad.Random.Extras.iterativeChoice: empty list"
err = moduleError "iterativeChoice" "empty list"

-- | Select a random element from a sequence.
--
-- /Partial function:/ This function is only defined on non-empty sequences.
--
-- /Complexity:/ O(log n), where /n/ is the length of the input sequence.
choiceSeq :: Seq.Seq a -> RVar a
choiceSeq s | Seq.null s = error "Control.Monad.Random.Extras.choiceSeq: empty sequence"
choiceSeq s | Seq.null s = moduleError "choiceSeq" "empty sequence"
| otherwise = Seq.index s `liftM` uniform 0 (Seq.length s - 1)

-- | Safely select a random element from a sequence.
Expand All @@ -185,7 +188,7 @@ choiceArray v = (v !) `liftM` uncurry uniform (Arr.bounds v)
--
-- /Complexity:/ O(n) base and O(1) per element.
choices :: Int -> [a] -> RVar [a]
choices _ [] = error "Control.Monad.Random.Extras.choices: empty list"
choices _ [] = moduleError "choices" "empty list"
choices n xs = choicesArray n $ Data.Array.listArray (1, length xs) xs

-- | Safely get a stream of random elements from a list.
Expand Down
5 changes: 4 additions & 1 deletion Data/Random/Shuffle/Weighted.hs
Expand Up @@ -18,9 +18,12 @@ import Data.Random.RVar
import Data.Random.Distribution
import Data.Random.Distribution.Uniform
import qualified Data.Map as M

moduleError :: String -> String -> a
moduleError n s = error $ "Data.Random.Shuffle.Weighted." ++ n ++ ": " ++ s

weightedChoiceExtractCDF :: (Num w, Ord w, Distribution Uniform w) => M.Map w a -> RVar (M.Map w a, a)
weightedChoiceExtractCDF m | M.null m = error "Data.Random.Shuffle.Weighted.weightedChoiceExtract: empty map"
weightedChoiceExtractCDF m | M.null m = moduleError "weightedChoiceExtractCDF" "empty map"
| otherwise = extract <$> uniform 0 wmax
where Just ((wmax, _), _) = M.maxViewWithKey m
extract w = (a `M.union` c, b)
Expand Down

0 comments on commit 689d5d0

Please sign in to comment.