Skip to content

Commit 6ed74fa

Browse files
committed
Export helper functions isInRangeOrd and isInRangeEnum
1 parent a249073 commit 6ed74fa

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* Add `splitGen` and `splitMutableGen`
2323
* Switch `randomM` and `randomRM` to use `FrozenGen` instead of `RandomGenM`
2424
* Deprecate `RandomGenM` in favor of a more powerful `FrozenGen`
25+
* Add `isInRangeOrd` and `isInRangeEnum` that can be used for implementing `isInRange`:
26+
[#148](https://github.com/haskell/random/pull/148)
2527
* Add `isInRange` to `UniformRange`: [#78](https://github.com/haskell/random/pull/78)
2628
* Add default implementation for `uniformRM` using `Generics`:
2729
[#92](https://github.com/haskell/random/pull/92)

src/System/Random/Internal.hs

+16-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ module System.Random.Internal
6060
, uniformFloatPositive01M
6161
, uniformEnumM
6262
, uniformEnumRM
63+
, isInRangeOrd
64+
, isInRangeEnum
6365

6466
-- * Generators for sequences of pseudo-random bytes
6567
, uniformByteStringM
@@ -997,7 +999,9 @@ class UniformRange a where
997999
-- > isInRange (lo, hi) lo' && isInRange (lo, hi) hi' && isInRange (lo', hi') x
9981000
-- > ==> isInRange (lo, hi) x
9991001
--
1000-
-- There is a default implementation of 'isInRange' via 'Generic'.
1002+
-- There is a default implementation of 'isInRange' via 'Generic'. Other helper function
1003+
-- that can be used for implementing this function are `isInRangeOrd` and
1004+
-- `isInRangeEnum`
10011005
--
10021006
-- @since 1.3.0
10031007
isInRange :: (a, a) -> a -> Bool
@@ -1036,9 +1040,20 @@ instance (GUniformRange f, GUniformRange g) => GUniformRange (f :*: g) where
10361040
gisInRange (x1 :*: y1, x2 :*: y2) (x3 :*: y3) =
10371041
gisInRange (x1, x2) x3 && gisInRange (y1, y2) y3
10381042

1043+
-- | Utilize `Ord` instance to decide if a value is within the range. Designed to be used
1044+
-- for implementing `isInRange`
1045+
--
1046+
-- @since 1.3.0
10391047
isInRangeOrd :: Ord a => (a, a) -> a -> Bool
10401048
isInRangeOrd (a, b) x = min a b <= x && x <= max a b
10411049

1050+
-- | Utilize `Enum` instance to decide if a value is within the range. Designed to be used
1051+
-- for implementing `isInRange`
1052+
--
1053+
-- @since 1.3.0
1054+
isInRangeEnum :: Enum a => (a, a) -> a -> Bool
1055+
isInRangeEnum (a, b) x = isInRangeOrd (fromEnum a, fromEnum b) (fromEnum x)
1056+
10421057
instance UniformRange Integer where
10431058
uniformRM = uniformIntegralM
10441059
{-# INLINE uniformRM #-}

src/System/Random/Stateful.hs

+2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ module System.Random.Stateful
9393
, uniformListM
9494
, uniformViaFiniteM
9595
, UniformRange(..)
96+
, isInRangeOrd
97+
, isInRangeEnum
9698

9799
-- ** Generators for sequences of pseudo-random bytes
98100
, uniformByteArrayM

0 commit comments

Comments
 (0)