| Copyright | (c) 2018 Harendra Kumar |
|---|---|
| License | BSD3 |
| Maintainer | streamly@composewell.com |
| Stability | experimental |
| Portability | GHC |
| Safe Haskell | None |
| Language | Haskell2010 |
Streamly.Internal.Data.Stream.Enumeration
Contents
Description
The functions defined in this module should be rarely needed for direct use,
try to use the operations from the Enumerable type class
instances instead.
This module provides an Enumerable type class to enumerate Enum types
into a stream. The operations in this type class correspond to similar
perations in the Enum type class, the only difference is that they produce
a stream instead of a list. These operations cannot be defined generically
based on the Enum type class. We provide instances for commonly used
types. If instances for other types are needed convenience functions defined
in this module can be used to define them. Alternatively, these functions
can be used directly.
Synopsis
- class Enum a => Enumerable a where
- enumerateFrom :: (IsStream t, Monad m) => a -> t m a
- enumerateFromTo :: (IsStream t, Monad m) => a -> a -> t m a
- enumerateFromThen :: (IsStream t, Monad m) => a -> a -> t m a
- enumerateFromThenTo :: (IsStream t, Monad m) => a -> a -> a -> t m a
- enumerate :: (IsStream t, Monad m, Bounded a, Enumerable a) => t m a
- enumerateTo :: (IsStream t, Monad m, Bounded a, Enumerable a) => a -> t m a
- enumerateFromBounded :: (IsStream t, Monad m, Enumerable a, Bounded a) => a -> t m a
- enumerateFromToSmall :: (IsStream t, Monad m, Enum a) => a -> a -> t m a
- enumerateFromThenToSmall :: (IsStream t, Monad m, Enum a) => a -> a -> a -> t m a
- enumerateFromThenSmallBounded :: (IsStream t, Monad m, Enumerable a, Bounded a) => a -> a -> t m a
- enumerateFromIntegral :: (IsStream t, Monad m, Integral a, Bounded a) => a -> t m a
- enumerateFromThenIntegral :: (IsStream t, Monad m, Integral a, Bounded a) => a -> a -> t m a
- enumerateFromToIntegral :: (IsStream t, Monad m, Integral a) => a -> a -> t m a
- enumerateFromThenToIntegral :: (IsStream t, Monad m, Integral a) => a -> a -> a -> t m a
- enumerateFromStepIntegral :: (IsStream t, Monad m, Integral a) => a -> a -> t m a
- enumerateFromFractional :: (IsStream t, Monad m, Fractional a) => a -> t m a
- enumerateFromToFractional :: (IsStream t, Monad m, Fractional a, Ord a) => a -> a -> t m a
- enumerateFromThenFractional :: (IsStream t, Monad m, Fractional a) => a -> a -> t m a
- enumerateFromThenToFractional :: (IsStream t, Monad m, Fractional a, Ord a) => a -> a -> a -> t m a
Documentation
class Enum a => Enumerable a where Source #
Types that can be enumerated as a stream. The operations in this type
class are equivalent to those in the Enum type class, except that these
generate a stream instead of a list. Use the functions in
Streamly.Internal.Data.Stream.Enumeration module to define new instances.
Since: 0.6.0
Methods
enumerateFrom :: (IsStream t, Monad m) => a -> t m a Source #
enumerateFrom from generates a stream starting with the element
from, enumerating up to maxBound when the type is Bounded or
generating an infinite stream when the type is not Bounded.
> S.toList $ S.take 4 $ S.enumerateFrom (0 :: Int) [0,1,2,3]
For Fractional types, enumeration is numerically stable. However, no
overflow or underflow checks are performed.
> S.toList $ S.take 4 $ S.enumerateFrom 1.1 [1.1,2.1,3.1,4.1]
Since: 0.6.0
enumerateFromTo :: (IsStream t, Monad m) => a -> a -> t m a Source #
Generate a finite stream starting with the element from, enumerating
the type up to the value to. If to is smaller than from then an
empty stream is returned.
> S.toList $ S.enumerateFromTo 0 4 [0,1,2,3,4]
For Fractional types, the last element is equal to the specified to
value after rounding to the nearest integral value.
> S.toList $ S.enumerateFromTo 1.1 4 [1.1,2.1,3.1,4.1] > S.toList $ S.enumerateFromTo 1.1 4.6 [1.1,2.1,3.1,4.1,5.1]
Since: 0.6.0
enumerateFromThen :: (IsStream t, Monad m) => a -> a -> t m a Source #
enumerateFromThen from then generates a stream whose first element
is from, the second element is then and the successive elements are
in increments of then - from. Enumeration can occur downwards or
upwards depending on whether then comes before or after from. For
Bounded types the stream ends when maxBound is reached, for
unbounded types it keeps enumerating infinitely.
> S.toList $ S.take 4 $ S.enumerateFromThen 0 2 [0,2,4,6] > S.toList $ S.take 4 $ S.enumerateFromThen 0 (-2) [0,-2,-4,-6]
Since: 0.6.0
enumerateFromThenTo :: (IsStream t, Monad m) => a -> a -> a -> t m a Source #
enumerateFromThenTo from then to generates a finite stream whose
first element is from, the second element is then and the successive
elements are in increments of then - from up to to. Enumeration can
occur downwards or upwards depending on whether then comes before or
after from.
> S.toList $ S.enumerateFromThenTo 0 2 6 [0,2,4,6] > S.toList $ S.enumerateFromThenTo 0 (-2) (-6) [0,-2,-4,-6]
Since: 0.6.0
Instances
Enumerating Bounded Enum Types
enumerateTo :: (IsStream t, Monad m, Bounded a, Enumerable a) => a -> t m a Source #
enumerateFromBounded :: (IsStream t, Monad m, Enumerable a, Bounded a) => a -> t m a Source #
enumerateFromBounded = enumerateFromTo from maxBound
enumerateFrom for Bounded Enum types.
Since: 0.6.0
Enumerating Enum Types not larger than Int
enumerateFromToSmall :: (IsStream t, Monad m, Enum a) => a -> a -> t m a Source #
enumerateFromTo for Enum types not larger than Int.
Since: 0.6.0
enumerateFromThenToSmall :: (IsStream t, Monad m, Enum a) => a -> a -> a -> t m a Source #
enumerateFromThenTo for Enum types not larger than Int.
Since: 0.6.0
enumerateFromThenSmallBounded :: (IsStream t, Monad m, Enumerable a, Bounded a) => a -> a -> t m a Source #
enumerateFromThen for Enum types not larger than Int.
Note: We convert the Enum to Int and enumerate the Int. If a
type is bounded but does not have a Bounded instance then we can go on
enumerating it beyond the legal values of the type, resulting in the failure
of toEnum when converting back to Enum. Therefore we require a Bounded
instance for this function to be safely used.
Since: 0.6.0
Enumerating Bounded Integral Types
enumerateFromThenIntegral :: (IsStream t, Monad m, Integral a, Bounded a) => a -> a -> t m a Source #
Enumerate an Integral type in steps. enumerateFromThenIntegral from
then generates a stream whose first element is from, the second element
is then and the successive elements are in increments of then - from.
The stream is bounded by the size of the Integral type.
> S.toList $ S.take 4 $ S.enumerateFromThenIntegral (0 :: Int) 2 [0,2,4,6] > S.toList $ S.take 4 $ S.enumerateFromThenIntegral (0 :: Int) (-2) [0,-2,-4,-6]
Since: 0.6.0
Enumerating Integral Types
enumerateFromToIntegral :: (IsStream t, Monad m, Integral a) => a -> a -> t m a Source #
Enumerate an Integral type up to a given limit.
enumerateFromToIntegral from to generates a finite stream whose first
element is from and successive elements are in increments of 1 up to
to.
> S.toList $ S.enumerateFromToIntegral 0 4 [0,1,2,3,4]
Since: 0.6.0
enumerateFromThenToIntegral :: (IsStream t, Monad m, Integral a) => a -> a -> a -> t m a Source #
Enumerate an Integral type in steps up to a given limit.
enumerateFromThenToIntegral from then to generates a finite stream whose
first element is from, the second element is then and the successive
elements are in increments of then - from up to to.
> S.toList $ S.enumerateFromThenToIntegral 0 2 6 [0,2,4,6] > S.toList $ S.enumerateFromThenToIntegral 0 (-2) (-6) [0,-2,-4,-6]
Since: 0.6.0
Enumerating unbounded Integral Types
enumerateFromStepIntegral :: (IsStream t, Monad m, Integral a) => a -> a -> t m a Source #
enumerateFromStepIntegral from step generates an infinite stream whose
first element is from and the successive elements are in increments of
step.
CAUTION: This function is not safe for finite integral types. It does not check for overflow, underflow or bounds.
> S.toList $ S.take 4 $ S.enumerateFromStepIntegral 0 2 [0,2,4,6] > S.toList $ S.take 3 $ S.enumerateFromStepIntegral 0 (-2) [0,-2,-4]
Since: 0.6.0
Enumerating Fractional Types
enumerateFromFractional :: (IsStream t, Monad m, Fractional a) => a -> t m a Source #
Numerically stable enumeration from a Fractional number in steps of size
1. enumerateFromFractional from generates a stream whose first element
is from and the successive elements are in increments of 1. No overflow
or underflow checks are performed.
This is the equivalent to enumFrom for Fractional types. For example:
> S.toList $ S.take 4 $ S.enumerateFromFractional 1.1 [1.1,2.1,3.1,4.1]
Since: 0.6.0
enumerateFromToFractional :: (IsStream t, Monad m, Fractional a, Ord a) => a -> a -> t m a Source #
Numerically stable enumeration from a Fractional number to a given
limit. enumerateFromToFractional from to generates a finite stream whose
first element is from and successive elements are in increments of 1 up
to to.
This is the equivalent of enumFromTo for Fractional types. For
example:
> S.toList $ S.enumerateFromToFractional 1.1 4 [1.1,2.1,3.1,4.1] > S.toList $ S.enumerateFromToFractional 1.1 4.6 [1.1,2.1,3.1,4.1,5.1]
Notice that the last element is equal to the specified to value after
rounding to the nearest integer.
Since: 0.6.0
enumerateFromThenFractional :: (IsStream t, Monad m, Fractional a) => a -> a -> t m a Source #
Numerically stable enumeration from a Fractional number in steps.
enumerateFromThenFractional from then generates a stream whose first
element is from, the second element is then and the successive elements
are in increments of then - from. No overflow or underflow checks are
performed.
This is the equivalent of enumFromThen for Fractional types. For
example:
> S.toList $ S.take 4 $ S.enumerateFromThenFractional 1.1 2.1 [1.1,2.1,3.1,4.1] > S.toList $ S.take 4 $ S.enumerateFromThenFractional 1.1 (-2.1) [1.1,-2.1,-5.300000000000001,-8.500000000000002]
Since: 0.6.0
enumerateFromThenToFractional :: (IsStream t, Monad m, Fractional a, Ord a) => a -> a -> a -> t m a Source #
Numerically stable enumeration from a Fractional number in steps up to a
given limit. enumerateFromThenToFractional from then to generates a
finite stream whose first element is from, the second element is then
and the successive elements are in increments of then - from up to to.
This is the equivalent of enumFromThenTo for Fractional types. For
example:
> S.toList $ S.enumerateFromThenToFractional 0.1 2 6 [0.1,2.0,3.9,5.799999999999999] > S.toList $ S.enumerateFromThenToFractional 0.1 (-2) (-6) [0.1,-2.0,-4.1000000000000005,-6.200000000000001]
Since: 0.6.0