Safe Haskell | None |
---|---|
Language | Haskell98 |
- newtype Size = Size {}
- data Range a = Range !a (Size -> (a, a))
- origin :: Range a -> a
- bounds :: Size -> Range a -> (a, a)
- lowerBound :: Ord a => Size -> Range a -> a
- upperBound :: Ord a => Size -> Range a -> a
- singleton :: a -> Range a
- constant :: a -> a -> Range a
- constantFrom :: a -> a -> a -> Range a
- constantBounded :: (Bounded a, Num a) => Range a
- linear :: Integral a => a -> a -> Range a
- linearFrom :: Integral a => a -> a -> a -> Range a
- linearFrac :: (Fractional a, Ord a) => a -> a -> Range a
- linearFracFrom :: (Fractional a, Ord a) => a -> a -> a -> Range a
- linearBounded :: (Bounded a, Integral a) => Range a
- clamp :: Ord a => a -> a -> a -> a
- scaleLinear :: Integral a => Size -> a -> a -> a
- scaleLinearFrac :: Fractional a => Size -> a -> a -> a
Size
Tests are parameterized by the size of the randomly-generated data, the meaning of which depends on the particular generator used.
Range
A range describes the bounds of a number to generate, which may or may not
be dependent on a Size
.
origin :: Range a -> a Source #
Get the origin of a range. This might be the mid-point or the lower bound, depending on what the range represents.
The bounds
of a range are scaled around this value when using the
linear
family of combinators.
When using a Range
to generate numbers, the shrinking function will
shrink towards the origin.
lowerBound :: Ord a => Size -> Range a -> a Source #
Get the lower bound of a range for the given size.
upperBound :: Ord a => Size -> Range a -> a Source #
Get the upper bound of a range for the given size.
Constant
singleton :: a -> Range a Source #
Construct a range which represents a constant single value.
>>>
bounds x $ singleton 5
(5,5)
>>>
origin $ singleton 5
5
constant :: a -> a -> Range a Source #
Construct a range which is unaffected by the size parameter.
A range from 0
to 10
, with the origin at 0
:
>>>
bounds x $ constant 0 10
(0,10)
>>>
origin $ constant 0 10
0
constantFrom :: a -> a -> a -> Range a Source #
Construct a range which is unaffected by the size parameter with a origin point which may differ from the bounds.
A range from -10
to 10
, with the origin at 0
:
>>>
bounds x $ constantFrom 0 (-10) 10
(-10,10)
>>>
origin $ constantFrom 0 (-10) 10
0
A range from 1970
to 2100
, with the origin at 2000
:
>>>
bounds x $ constantFrom 2000 1970 2100
(1970,2100)
>>>
origin $ constantFrom 2000 1970 2100
2000
constantBounded :: (Bounded a, Num a) => Range a Source #
Construct a range which is unaffected by the size parameter using the full range of a data type.
A range from -128
to 127
, with the origin at 0
:
>>>
bounds x (constantBounded :: Range Int8)
(-128,127)
>>>
origin (constantBounded :: Range Int8)
0
Linear
linear :: Integral a => a -> a -> Range a Source #
Construct a range which scales the second bound relative to the size parameter.
>>>
bounds 0 $ linear 0 10
(0,0)
>>>
bounds 50 $ linear 0 10
(0,5)
>>>
bounds 99 $ linear 0 10
(0,10)
linearFrom :: Integral a => a -> a -> a -> Range a Source #
Construct a range which scales the bounds relative to the size parameter.
>>>
bounds 0 $ linearFrom 0 (-10) 10
(0,0)
>>>
bounds 50 $ linearFrom 0 (-10) 20
(-5,10)
>>>
bounds 99 $ linearFrom 0 (-10) 20
(-10,20)
linearFrac :: (Fractional a, Ord a) => a -> a -> Range a Source #
Construct a range which scales the second bound relative to the size parameter.
This works the same as linear
, but for fractional values.
linearFracFrom :: (Fractional a, Ord a) => a -> a -> a -> Range a Source #
Construct a range which scales the bounds relative to the size parameter.
This works the same as linearFrom
, but for fractional values.
linearBounded :: (Bounded a, Integral a) => Range a Source #
Construct a range which is scaled relative to the size parameter and uses the full range of a data type.
>>>
bounds 0 (linearBounded :: Range Int8)
(0,0)
>>>
bounds 50 (linearBounded :: Range Int8)
(-64,64)
>>>
bounds 99 (linearBounded :: Range Int8)
(-128,127)
Internal
These functions are exported in case you need them in a pinch, but are not part of the public API and may change at any time, even as part of a minor update.
clamp :: Ord a => a -> a -> a -> a Source #
Truncate a value so it stays within some range.
>>>
clamp 5 10 15
10
>>>
clamp 5 10 0
5
scaleLinear :: Integral a => Size -> a -> a -> a Source #
Scale an integral linearly with the size parameter.
scaleLinearFrac :: Fractional a => Size -> a -> a -> a Source #
Scale a fractional number linearly with the size parameter.