{-# LANGUAGE CPP #-}
module Streamly.Internal.Data.Stream.Generate
(
nil
, nilM
, cons
, consM
, unfold
, unfoldr
, unfoldrM
, fromPure
, fromEffect
, repeat
, repeatM
, replicate
, replicateM
, enumerateFromStepNum
, enumerateFromNum
, enumerateFromThenNum
, enumerate
, enumerateTo
, enumerateFromBounded
, enumerateFromToSmall
, enumerateFromThenToSmall
, enumerateFromThenSmallBounded
, enumerateFromIntegral
, enumerateFromThenIntegral
, enumerateFromToIntegral
, enumerateFromThenToIntegral
, enumerateFromStepIntegral
, enumerateFromFractional
, enumerateFromToFractional
, enumerateFromThenFractional
, enumerateFromThenToFractional
, Enumerable(..)
, times
, timesWith
, absTimes
, absTimesWith
, relTimes
, relTimesWith
, durations
, timeout
, fromIndices
, fromIndicesM
, generate
, generateM
, iterate
, iterateM
, fromList
, fromListM
, fromFoldable
, fromFoldableM
, fromPtr
, fromPtrN
, fromByteStr#
, fromStreamK
, toStreamK
)
where
#include "inline.hs"
#include "ArrayMacros.h"
import Control.Monad.IO.Class (MonadIO(..))
import Data.Functor.Identity (Identity(..))
import Foreign.Ptr (Ptr, plusPtr)
import Foreign.Storable (Storable (peek), sizeOf)
import GHC.Exts (Addr#, Ptr (Ptr))
import Streamly.Internal.Data.Time.Clock
(Clock(Monotonic), asyncClock, readClock)
import Streamly.Internal.Data.Time.Units
(toAbsTime, AbsTime, toRelTime64, RelTime64, addToAbsTime64)
import Streamly.Internal.System.IO (unsafeInlineIO)
#ifdef USE_UNFOLDS_EVERYWHERE
import qualified Streamly.Internal.Data.Unfold as Unfold
#endif
import Data.Fixed
import Data.Int
import Data.Ratio
import Data.Word
import Numeric.Natural
import Prelude hiding (iterate, repeat, replicate, take, takeWhile)
import Streamly.Internal.Data.Stream.Type
#include "DocTestDataStream.hs"
{-# INLINE_NORMAL nil #-}
nil :: Applicative m => Stream m a
nil :: forall (m :: * -> *) a. Applicative m => Stream m a
nil = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ ()
_ -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall s a. Step s a
Stop) ()
{-# INLINE_NORMAL cons #-}
cons :: Applicative m => a -> Stream m a -> Stream m a
cons :: forall (m :: * -> *) a.
Applicative m =>
a -> Stream m a -> Stream m a
cons a
x (Stream State StreamK m a -> s -> m (Step s a)
step s
state) = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream State StreamK m a -> Maybe s -> m (Step (Maybe s) a)
step1 forall a. Maybe a
Nothing
where
{-# INLINE_LATE step1 #-}
step1 :: State StreamK m a -> Maybe s -> m (Step (Maybe s) a)
step1 State StreamK m a
_ Maybe s
Nothing = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (forall a. a -> Maybe a
Just s
state)
step1 State StreamK m a
gst (Just s
st) = do
(\case
Yield a
a s
s -> forall s a. a -> s -> Step s a
Yield a
a (forall a. a -> Maybe a
Just s
s)
Skip s
s -> forall s a. s -> Step s a
Skip (forall a. a -> Maybe a
Just s
s)
Step s a
Stop -> forall s a. Step s a
Stop) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> State StreamK m a -> s -> m (Step s a)
step State StreamK m a
gst s
st
{-# INLINE_NORMAL unfoldrM #-}
unfoldrM :: Monad m => (s -> m (Maybe (a, s))) -> s -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
unfoldrM next = unfold (Unfold.unfoldrM next)
#else
unfoldrM :: forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM s -> m (Maybe (a, s))
next = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {p}. p -> s -> m (Step s a)
step
where
{-# INLINE_LATE step #-}
step :: p -> s -> m (Step s a)
step p
_ s
st = do
Maybe (a, s)
r <- s -> m (Maybe (a, s))
next s
st
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Maybe (a, s)
r of
Just (a
x, s
s) -> forall s a. a -> s -> Step s a
Yield a
x s
s
Maybe (a, s)
Nothing -> forall s a. Step s a
Stop
#endif
{-# INLINE_LATE unfoldr #-}
unfoldr :: Monad m => (s -> Maybe (a, s)) -> s -> Stream m a
unfoldr :: forall (m :: * -> *) s a.
Monad m =>
(s -> Maybe (a, s)) -> s -> Stream m a
unfoldr s -> Maybe (a, s)
f = forall (m :: * -> *) s a.
Monad m =>
(s -> m (Maybe (a, s))) -> s -> Stream m a
unfoldrM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (a, s)
f)
{-# INLINE_NORMAL repeatM #-}
repeatM :: Monad m => m a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
repeatM = unfold Unfold.repeatM
#else
repeatM :: forall (m :: * -> *) a. Monad m => m a -> Stream m a
repeatM m a
x = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ ()
_ -> m a
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
r ()) ()
#endif
{-# INLINE_NORMAL repeat #-}
repeat :: Monad m => a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
repeat x = repeatM (pure x)
#else
repeat :: forall (m :: * -> *) a. Monad m => a -> Stream m a
repeat a
x = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ ()
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x ()) ()
#endif
{-# INLINE_NORMAL replicateM #-}
replicateM :: Monad m => Int -> m a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
replicateM n p = unfold Unfold.replicateM (n, p)
#else
replicateM :: forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
replicateM Int
n m a
p = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {p}. p -> Int -> m (Step Int a)
step Int
n
where
{-# INLINE_LATE step #-}
step :: p -> Int -> m (Step Int a)
step p
_ (Int
i :: Int)
| Int
i forall a. Ord a => a -> a -> Bool
<= Int
0 = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
| Bool
otherwise = do
a
x <- m a
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (Int
i forall a. Num a => a -> a -> a
- Int
1)
#endif
{-# INLINE_NORMAL replicate #-}
replicate :: Monad m => Int -> a -> Stream m a
replicate :: forall (m :: * -> *) a. Monad m => Int -> a -> Stream m a
replicate Int
n a
x = forall (m :: * -> *) a. Monad m => Int -> m a -> Stream m a
replicateM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return a
x)
{-# INLINE_NORMAL enumerateFromStepNum #-}
enumerateFromStepNum :: (Monad m, Num a) => a -> a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
enumerateFromStepNum from stride =
unfold Unfold.enumerateFromStepNum (from, stride)
#else
enumerateFromStepNum :: forall (m :: * -> *) a. (Monad m, Num a) => a -> a -> Stream m a
enumerateFromStepNum a
from a
stride = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {p}. Monad m => p -> a -> m (Step a a)
step a
0
where
{-# INLINE_LATE step #-}
step :: p -> a -> m (Step a a)
step p
_ !a
i = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ (forall s a. a -> s -> Step s a
Yield forall a b. (a -> b) -> a -> b
$! (a
from forall a. Num a => a -> a -> a
+ a
i forall a. Num a => a -> a -> a
* a
stride)) forall a b. (a -> b) -> a -> b
$! (a
i forall a. Num a => a -> a -> a
+ a
1)
#endif
{-# INLINE_NORMAL enumerateFromNum #-}
enumerateFromNum :: (Monad m, Num a) => a -> Stream m a
enumerateFromNum :: forall (m :: * -> *) a. (Monad m, Num a) => a -> Stream m a
enumerateFromNum a
from = forall (m :: * -> *) a. (Monad m, Num a) => a -> a -> Stream m a
enumerateFromStepNum a
from a
1
{-# INLINE_NORMAL enumerateFromThenNum #-}
enumerateFromThenNum :: (Monad m, Num a) => a -> a -> Stream m a
enumerateFromThenNum :: forall (m :: * -> *) a. (Monad m, Num a) => a -> a -> Stream m a
enumerateFromThenNum a
from a
next = forall (m :: * -> *) a. (Monad m, Num a) => a -> a -> Stream m a
enumerateFromStepNum a
from (a
next forall a. Num a => a -> a -> a
- a
from)
#ifndef USE_UNFOLDS_EVERYWHERE
data EnumState a = EnumInit | EnumYield a a a | EnumStop
{-# INLINE_NORMAL enumerateFromThenToIntegralUp #-}
enumerateFromThenToIntegralUp
:: (Monad m, Integral a)
=> a -> a -> a -> Stream m a
enumerateFromThenToIntegralUp :: forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegralUp a
from a
next a
to = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {p}.
Monad m =>
p -> EnumState a -> m (Step (EnumState a) a)
step forall a. EnumState a
EnumInit
where
{-# INLINE_LATE step #-}
step :: p -> EnumState a -> m (Step (EnumState a) a)
step p
_ EnumState a
EnumInit =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if a
to forall a. Ord a => a -> a -> Bool
< a
next
then if a
to forall a. Ord a => a -> a -> Bool
< a
from
then forall s a. Step s a
Stop
else forall s a. a -> s -> Step s a
Yield a
from forall a. EnumState a
EnumStop
else
let stride :: a
stride = a
next forall a. Num a => a -> a -> a
- a
from
in forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> EnumState a
EnumYield a
from a
stride (a
to forall a. Num a => a -> a -> a
- a
stride)
step p
_ (EnumYield a
x a
stride a
toMinus) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if a
x forall a. Ord a => a -> a -> Bool
> a
toMinus
then forall s a. a -> s -> Step s a
Yield a
x forall a. EnumState a
EnumStop
else forall s a. a -> s -> Step s a
Yield a
x forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> EnumState a
EnumYield (a
x forall a. Num a => a -> a -> a
+ a
stride) a
stride a
toMinus
step p
_ EnumState a
EnumStop = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE_NORMAL enumerateFromThenToIntegralDn #-}
enumerateFromThenToIntegralDn
:: (Monad m, Integral a)
=> a -> a -> a -> Stream m a
enumerateFromThenToIntegralDn :: forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegralDn a
from a
next a
to = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {p}.
Monad m =>
p -> EnumState a -> m (Step (EnumState a) a)
step forall a. EnumState a
EnumInit
where
{-# INLINE_LATE step #-}
step :: p -> EnumState a -> m (Step (EnumState a) a)
step p
_ EnumState a
EnumInit =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ if a
to forall a. Ord a => a -> a -> Bool
> a
next
then if a
to forall a. Ord a => a -> a -> Bool
> a
from
then forall s a. Step s a
Stop
else forall s a. a -> s -> Step s a
Yield a
from forall a. EnumState a
EnumStop
else
let stride :: a
stride = a
next forall a. Num a => a -> a -> a
- a
from
in forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> EnumState a
EnumYield a
from a
stride (a
to forall a. Num a => a -> a -> a
- a
stride)
step p
_ (EnumYield a
x a
stride a
toMinus) =
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
if a
x forall a. Ord a => a -> a -> Bool
< a
toMinus
then forall s a. a -> s -> Step s a
Yield a
x forall a. EnumState a
EnumStop
else forall s a. a -> s -> Step s a
Yield a
x forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> EnumState a
EnumYield (a
x forall a. Num a => a -> a -> a
+ a
stride) a
stride a
toMinus
step p
_ EnumState a
EnumStop = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
#endif
{-# INLINE_NORMAL enumerateFromThenToIntegral #-}
enumerateFromThenToIntegral
:: (Monad m, Integral a)
=> a -> a -> a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
enumerateFromThenToIntegral from next to =
unfold Unfold.enumerateFromThenToIntegral (from, next, to)
#else
enumerateFromThenToIntegral :: forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegral a
from a
next a
to
| a
next forall a. Ord a => a -> a -> Bool
>= a
from = forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegralUp a
from a
next a
to
| Bool
otherwise = forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegralDn a
from a
next a
to
#endif
{-# INLINE_NORMAL enumerateFromThenIntegral #-}
enumerateFromThenIntegral
:: (Monad m, Integral a, Bounded a)
=> a -> a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
enumerateFromThenIntegral from next =
unfold Unfold.enumerateFromThenIntegralBounded (from, next)
#else
enumerateFromThenIntegral :: forall (m :: * -> *) a.
(Monad m, Integral a, Bounded a) =>
a -> a -> Stream m a
enumerateFromThenIntegral a
from a
next =
if a
next forall a. Ord a => a -> a -> Bool
> a
from
then forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegralUp a
from a
next forall a. Bounded a => a
maxBound
else forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegralDn a
from a
next forall a. Bounded a => a
minBound
#endif
{-# INLINE_NORMAL enumerateFromStepIntegral #-}
enumerateFromStepIntegral :: (Integral a, Monad m) => a -> a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
enumerateFromStepIntegral from stride =
unfold Unfold.enumerateFromStepIntegral (from, stride)
#else
enumerateFromStepIntegral :: forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> Stream m a
enumerateFromStepIntegral a
from a
stride =
a
from seq :: forall a b. a -> b -> b
`seq` a
stride seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {p}. Monad m => p -> a -> m (Step a a)
step a
from
where
{-# INLINE_LATE step #-}
step :: p -> a -> m (Step a a)
step p
_ !a
x = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x forall a b. (a -> b) -> a -> b
$! (a
x forall a. Num a => a -> a -> a
+ a
stride)
#endif
{-# INLINE enumerateFromToIntegral #-}
enumerateFromToIntegral :: (Monad m, Integral a) => a -> a -> Stream m a
enumerateFromToIntegral :: forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> Stream m a
enumerateFromToIntegral a
from a
to =
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeWhile (forall a. Ord a => a -> a -> Bool
<= a
to) forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *).
(Integral a, Monad m) =>
a -> a -> Stream m a
enumerateFromStepIntegral a
from a
1
{-# INLINE enumerateFromIntegral #-}
enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => a -> Stream m a
enumerateFromIntegral :: forall (m :: * -> *) a.
(Monad m, Integral a, Bounded a) =>
a -> Stream m a
enumerateFromIntegral a
from = forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> Stream m a
enumerateFromToIntegral a
from forall a. Bounded a => a
maxBound
{-# INLINE enumerateFromFractional #-}
enumerateFromFractional :: (Monad m, Fractional a) => a -> Stream m a
enumerateFromFractional :: forall (m :: * -> *) a. (Monad m, Fractional a) => a -> Stream m a
enumerateFromFractional = forall (m :: * -> *) a. (Monad m, Num a) => a -> Stream m a
enumerateFromNum
{-# INLINE enumerateFromThenFractional #-}
enumerateFromThenFractional
:: (Monad m, Fractional a)
=> a -> a -> Stream m a
enumerateFromThenFractional :: forall (m :: * -> *) a.
(Monad m, Fractional a) =>
a -> a -> Stream m a
enumerateFromThenFractional = forall (m :: * -> *) a. (Monad m, Num a) => a -> a -> Stream m a
enumerateFromThenNum
{-# INLINE_NORMAL enumerateFromToFractional #-}
enumerateFromToFractional
:: (Monad m, Fractional a, Ord a)
=> a -> a -> Stream m a
enumerateFromToFractional :: forall (m :: * -> *) a.
(Monad m, Fractional a, Ord a) =>
a -> a -> Stream m a
enumerateFromToFractional a
from a
to =
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeWhile (forall a. Ord a => a -> a -> Bool
<= a
to forall a. Num a => a -> a -> a
+ a
1 forall a. Fractional a => a -> a -> a
/ a
2) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. (Monad m, Num a) => a -> a -> Stream m a
enumerateFromStepNum a
from a
1
{-# INLINE_NORMAL enumerateFromThenToFractional #-}
enumerateFromThenToFractional
:: (Monad m, Fractional a, Ord a)
=> a -> a -> a -> Stream m a
enumerateFromThenToFractional :: forall (m :: * -> *) a.
(Monad m, Fractional a, Ord a) =>
a -> a -> a -> Stream m a
enumerateFromThenToFractional a
from a
next a
to =
forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeWhile a -> Bool
predicate forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Fractional a) =>
a -> a -> Stream m a
enumerateFromThenFractional a
from a
next
where
mid :: a
mid = (a
next forall a. Num a => a -> a -> a
- a
from) forall a. Fractional a => a -> a -> a
/ a
2
predicate :: a -> Bool
predicate | a
next forall a. Ord a => a -> a -> Bool
>= a
from = (forall a. Ord a => a -> a -> Bool
<= a
to forall a. Num a => a -> a -> a
+ a
mid)
| Bool
otherwise = (forall a. Ord a => a -> a -> Bool
>= a
to forall a. Num a => a -> a -> a
+ a
mid)
{-# INLINE enumerateFromToSmall #-}
enumerateFromToSmall :: (Monad m, Enum a) => a -> a -> Stream m a
enumerateFromToSmall :: forall (m :: * -> *) a. (Monad m, Enum a) => a -> a -> Stream m a
enumerateFromToSmall a
from a
to =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Enum a => Int -> a
toEnum
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> Stream m a
enumerateFromToIntegral (forall a. Enum a => a -> Int
fromEnum a
from) (forall a. Enum a => a -> Int
fromEnum a
to)
{-# INLINE enumerateFromThenToSmall #-}
enumerateFromThenToSmall :: (Monad m, Enum a)
=> a -> a -> a -> Stream m a
enumerateFromThenToSmall :: forall (m :: * -> *) a.
(Monad m, Enum a) =>
a -> a -> a -> Stream m a
enumerateFromThenToSmall a
from a
next a
to =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Enum a => Int -> a
toEnum
forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Integral a) =>
a -> a -> a -> Stream m a
enumerateFromThenToIntegral
(forall a. Enum a => a -> Int
fromEnum a
from) (forall a. Enum a => a -> Int
fromEnum a
next) (forall a. Enum a => a -> Int
fromEnum a
to)
{-# INLINE enumerateFromThenSmallBounded #-}
enumerateFromThenSmallBounded :: (Monad m, Enumerable a, Bounded a)
=> a -> a -> Stream m a
enumerateFromThenSmallBounded :: forall (m :: * -> *) a.
(Monad m, Enumerable a, Bounded a) =>
a -> a -> Stream m a
enumerateFromThenSmallBounded a
from a
next =
if forall a. Enum a => a -> Int
fromEnum a
next forall a. Ord a => a -> a -> Bool
>= forall a. Enum a => a -> Int
fromEnum a
from
then forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> a -> Stream m a
enumerateFromThenTo a
from a
next forall a. Bounded a => a
maxBound
else forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> a -> Stream m a
enumerateFromThenTo a
from a
next forall a. Bounded a => a
minBound
class Enum a => Enumerable a where
enumerateFrom :: (Monad m) => a -> Stream m a
enumerateFromTo :: (Monad m) => a -> a -> Stream m a
enumerateFromThen :: (Monad m) => a -> a -> Stream m a
enumerateFromThenTo :: (Monad m) => a -> a -> a -> Stream m a
{-# INLINE enumerate #-}
enumerate :: (Monad m, Bounded a, Enumerable a) => Stream m a
enumerate :: forall (m :: * -> *) a.
(Monad m, Bounded a, Enumerable a) =>
Stream m a
enumerate = forall a (m :: * -> *). (Enumerable a, Monad m) => a -> Stream m a
enumerateFrom forall a. Bounded a => a
minBound
{-# INLINE enumerateTo #-}
enumerateTo :: (Monad m, Bounded a, Enumerable a) => a -> Stream m a
enumerateTo :: forall (m :: * -> *) a.
(Monad m, Bounded a, Enumerable a) =>
a -> Stream m a
enumerateTo = forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> Stream m a
enumerateFromTo forall a. Bounded a => a
minBound
{-# INLINE enumerateFromBounded #-}
enumerateFromBounded :: (Monad m, Enumerable a, Bounded a)
=> a -> Stream m a
enumerateFromBounded :: forall (m :: * -> *) a.
(Monad m, Enumerable a, Bounded a) =>
a -> Stream m a
enumerateFromBounded a
from = forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> Stream m a
enumerateFromTo a
from forall a. Bounded a => a
maxBound
#define ENUMERABLE_BOUNDED_SMALL(SMALL_TYPE) \
instance Enumerable SMALL_TYPE where { \
{-# INLINE enumerateFrom #-}; \
enumerateFrom = enumerateFromBounded; \
{-# INLINE enumerateFromThen #-}; \
enumerateFromThen = enumerateFromThenSmallBounded; \
{-# INLINE enumerateFromTo #-}; \
enumerateFromTo = enumerateFromToSmall; \
{-# INLINE enumerateFromThenTo #-}; \
enumerateFromThenTo = enumerateFromThenToSmall }
ENUMERABLE_BOUNDED_SMALL(())
ENUMERABLE_BOUNDED_SMALL(Bool)
ENUMERABLE_BOUNDED_SMALL(Ordering)
ENUMERABLE_BOUNDED_SMALL(Char)
#define ENUMERABLE_BOUNDED_INTEGRAL(INTEGRAL_TYPE) \
instance Enumerable INTEGRAL_TYPE where { \
{-# INLINE enumerateFrom #-}; \
enumerateFrom = enumerateFromIntegral; \
{-# INLINE enumerateFromThen #-}; \
enumerateFromThen = enumerateFromThenIntegral; \
{-# INLINE enumerateFromTo #-}; \
enumerateFromTo = enumerateFromToIntegral; \
{-# INLINE enumerateFromThenTo #-}; \
enumerateFromThenTo = enumerateFromThenToIntegral }
ENUMERABLE_BOUNDED_INTEGRAL(Int)
ENUMERABLE_BOUNDED_INTEGRAL(Int8)
ENUMERABLE_BOUNDED_INTEGRAL(Int16)
ENUMERABLE_BOUNDED_INTEGRAL(Int32)
ENUMERABLE_BOUNDED_INTEGRAL(Int64)
ENUMERABLE_BOUNDED_INTEGRAL(Word)
ENUMERABLE_BOUNDED_INTEGRAL(Word8)
ENUMERABLE_BOUNDED_INTEGRAL(Word16)
ENUMERABLE_BOUNDED_INTEGRAL(Word32)
ENUMERABLE_BOUNDED_INTEGRAL(Word64)
#define ENUMERABLE_UNBOUNDED_INTEGRAL(INTEGRAL_TYPE) \
instance Enumerable INTEGRAL_TYPE where { \
{-# INLINE enumerateFrom #-}; \
enumerateFrom from = enumerateFromStepIntegral from 1; \
{-# INLINE enumerateFromThen #-}; \
enumerateFromThen from next = \
enumerateFromStepIntegral from (next - from); \
{-# INLINE enumerateFromTo #-}; \
enumerateFromTo = enumerateFromToIntegral; \
{-# INLINE enumerateFromThenTo #-}; \
enumerateFromThenTo = enumerateFromThenToIntegral }
ENUMERABLE_UNBOUNDED_INTEGRAL(Integer)
ENUMERABLE_UNBOUNDED_INTEGRAL(Natural)
#define ENUMERABLE_FRACTIONAL(FRACTIONAL_TYPE,CONSTRAINT) \
instance (CONSTRAINT) => Enumerable FRACTIONAL_TYPE where { \
{-# INLINE enumerateFrom #-}; \
enumerateFrom = enumerateFromFractional; \
{-# INLINE enumerateFromThen #-}; \
enumerateFromThen = enumerateFromThenFractional; \
{-# INLINE enumerateFromTo #-}; \
enumerateFromTo = enumerateFromToFractional; \
{-# INLINE enumerateFromThenTo #-}; \
enumerateFromThenTo = enumerateFromThenToFractional }
ENUMERABLE_FRACTIONAL(Float,)
ENUMERABLE_FRACTIONAL(Double,)
ENUMERABLE_FRACTIONAL((Fixed a),HasResolution a)
ENUMERABLE_FRACTIONAL((Ratio a),Integral a)
instance Enumerable a => Enumerable (Identity a) where
{-# INLINE enumerateFrom #-}
enumerateFrom :: forall (m :: * -> *).
Monad m =>
Identity a -> Stream m (Identity a)
enumerateFrom (Identity a
from) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Identity a
Identity forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *). (Enumerable a, Monad m) => a -> Stream m a
enumerateFrom a
from
{-# INLINE enumerateFromThen #-}
enumerateFromThen :: forall (m :: * -> *).
Monad m =>
Identity a -> Identity a -> Stream m (Identity a)
enumerateFromThen (Identity a
from) (Identity a
next) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Identity a
Identity forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> Stream m a
enumerateFromThen a
from a
next
{-# INLINE enumerateFromTo #-}
enumerateFromTo :: forall (m :: * -> *).
Monad m =>
Identity a -> Identity a -> Stream m (Identity a)
enumerateFromTo (Identity a
from) (Identity a
to) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Identity a
Identity forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> Stream m a
enumerateFromTo a
from a
to
{-# INLINE enumerateFromThenTo #-}
enumerateFromThenTo :: forall (m :: * -> *).
Monad m =>
Identity a -> Identity a -> Identity a -> Stream m (Identity a)
enumerateFromThenTo (Identity a
from) (Identity a
next) (Identity a
to) =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Identity a
Identity
forall a b. (a -> b) -> a -> b
$ forall a (m :: * -> *).
(Enumerable a, Monad m) =>
a -> a -> a -> Stream m a
enumerateFromThenTo a
from a
next a
to
{-# INLINE_NORMAL timesWith #-}
timesWith :: MonadIO m => Double -> Stream m (AbsTime, RelTime64)
timesWith :: forall (m :: * -> *).
MonadIO m =>
Double -> Stream m (AbsTime, RelTime64)
timesWith Double
g = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {p}.
MonadIO m =>
p
-> Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64)
-> m (Step
(Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64))
(AbsTime, RelTime64))
step forall a. Maybe a
Nothing
where
{-# INLINE_LATE step #-}
step :: p
-> Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64)
-> m (Step
(Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64))
(AbsTime, RelTime64))
step p
_ Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64)
Nothing = do
(ThreadId, IORef MicroSecond64)
clock <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Clock -> Double -> IO (ThreadId, IORef MicroSecond64)
asyncClock Clock
Monotonic Double
g
MicroSecond64
a <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ (ThreadId, IORef MicroSecond64) -> IO MicroSecond64
readClock (ThreadId, IORef MicroSecond64)
clock
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. s -> Step s a
Skip forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just ((ThreadId, IORef MicroSecond64)
clock, MicroSecond64
a)
step p
_ s :: Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64)
s@(Just ((ThreadId, IORef MicroSecond64)
clock, MicroSecond64
t0)) = do
MicroSecond64
a <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ (ThreadId, IORef MicroSecond64) -> IO MicroSecond64
readClock (ThreadId, IORef MicroSecond64)
clock
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield (forall a. TimeUnit a => a -> AbsTime
toAbsTime MicroSecond64
t0, forall a. TimeUnit64 a => a -> RelTime64
toRelTime64 (MicroSecond64
a forall a. Num a => a -> a -> a
- MicroSecond64
t0)) Maybe ((ThreadId, IORef MicroSecond64), MicroSecond64)
s
{-# INLINE absTimesWith #-}
absTimesWith :: MonadIO m => Double -> Stream m AbsTime
absTimesWith :: forall (m :: * -> *). MonadIO m => Double -> Stream m AbsTime
absTimesWith = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry AbsTime -> RelTime64 -> AbsTime
addToAbsTime64) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadIO m =>
Double -> Stream m (AbsTime, RelTime64)
timesWith
{-# INLINE relTimesWith #-}
relTimesWith :: MonadIO m => Double -> Stream m RelTime64
relTimesWith :: forall (m :: * -> *). MonadIO m => Double -> Stream m RelTime64
relTimesWith = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *).
MonadIO m =>
Double -> Stream m (AbsTime, RelTime64)
timesWith
{-# INLINE times #-}
times :: MonadIO m => Stream m (AbsTime, RelTime64)
times :: forall (m :: * -> *). MonadIO m => Stream m (AbsTime, RelTime64)
times = forall (m :: * -> *).
MonadIO m =>
Double -> Stream m (AbsTime, RelTime64)
timesWith Double
0.01
{-# INLINE absTimes #-}
absTimes :: MonadIO m => Stream m AbsTime
absTimes :: forall (m :: * -> *). MonadIO m => Stream m AbsTime
absTimes = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry AbsTime -> RelTime64 -> AbsTime
addToAbsTime64) forall (m :: * -> *). MonadIO m => Stream m (AbsTime, RelTime64)
times
{-# INLINE relTimes #-}
relTimes :: MonadIO m => Stream m RelTime64
relTimes :: forall (m :: * -> *). MonadIO m => Stream m RelTime64
relTimes = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall (m :: * -> *). MonadIO m => Stream m (AbsTime, RelTime64)
times
{-# INLINE durations #-}
durations ::
Double -> t m RelTime64
durations :: forall (t :: * -> * -> *) m. Double -> t m RelTime64
durations = forall a. HasCallStack => a
undefined
{-# INLINE timeout #-}
timeout ::
AbsTime -> t m ()
timeout :: forall (t :: * -> * -> *) m. AbsTime -> t m ()
timeout = forall a. HasCallStack => a
undefined
{-# INLINE_NORMAL fromIndicesM #-}
fromIndicesM :: Monad m => (Int -> m a) -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
fromIndicesM gen = unfold (Unfold.fromIndicesM gen) 0
#else
fromIndicesM :: forall (m :: * -> *) a. Monad m => (Int -> m a) -> Stream m a
fromIndicesM Int -> m a
gen = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {p}. p -> Int -> m (Step Int a)
step Int
0
where
{-# INLINE_LATE step #-}
step :: p -> Int -> m (Step Int a)
step p
_ Int
i = do
a
x <- Int -> m a
gen Int
i
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (Int
i forall a. Num a => a -> a -> a
+ Int
1)
#endif
{-# INLINE fromIndices #-}
fromIndices :: Monad m => (Int -> a) -> Stream m a
fromIndices :: forall (m :: * -> *) a. Monad m => (Int -> a) -> Stream m a
fromIndices Int -> a
gen = forall (m :: * -> *) a. Monad m => (Int -> m a) -> Stream m a
fromIndicesM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
gen)
{-# INLINE_NORMAL generateM #-}
generateM :: Monad m => Int -> (Int -> m a) -> Stream m a
generateM :: forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
generateM Int
n Int -> m a
gen = Int
n seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {p}. p -> Int -> m (Step Int a)
step Int
0
where
{-# INLINE_LATE step #-}
step :: p -> Int -> m (Step Int a)
step p
_ Int
i | Int
i forall a. Ord a => a -> a -> Bool
< Int
n = do
a
x <- Int -> m a
gen Int
i
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (Int
i forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
{-# INLINE generate #-}
generate :: Monad m => Int -> (Int -> a) -> Stream m a
generate :: forall (m :: * -> *) a. Monad m => Int -> (Int -> a) -> Stream m a
generate Int
n Int -> a
gen = forall (m :: * -> *) a.
Monad m =>
Int -> (Int -> m a) -> Stream m a
generateM Int
n (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a
gen)
{-# INLINE_NORMAL iterateM #-}
iterateM :: Monad m => (a -> m a) -> m a -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
iterateM step = unfold (Unfold.iterateM step)
#else
iterateM :: forall (m :: * -> *) a. Monad m => (a -> m a) -> m a -> Stream m a
iterateM a -> m a
step = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream (\State StreamK m a
_ m a
st -> m a
st forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \(!a
x) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (a -> m a
step a
x))
#endif
{-# INLINE_NORMAL iterate #-}
iterate :: Monad m => (a -> a) -> a -> Stream m a
iterate :: forall (m :: * -> *) a. Monad m => (a -> a) -> a -> Stream m a
iterate a -> a
step a
st = forall (m :: * -> *) a. Monad m => (a -> m a) -> m a -> Stream m a
iterateM (forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
step) (forall (m :: * -> *) a. Monad m => a -> m a
return a
st)
{-# INLINE_LATE fromListM #-}
fromListM :: Monad m => [m a] -> Stream m a
#ifdef USE_UNFOLDS_EVERYWHERE
fromListM = unfold Unfold.fromListM
#else
fromListM :: forall (m :: * -> *) a. Monad m => [m a] -> Stream m a
fromListM = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {p} {a}.
Monad m =>
p -> [m a] -> m (Step [m a] a)
step
where
{-# INLINE_LATE step #-}
step :: p -> [m a] -> m (Step [m a] a)
step p
_ (m a
m:[m a]
ms) = m a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x [m a]
ms
step p
_ [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall s a. Step s a
Stop
#endif
{-# INLINE fromFoldable #-}
fromFoldable :: (Monad m, Foldable f) => f a -> Stream m a
fromFoldable :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Foldable f) =>
f a -> Stream m a
fromFoldable = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr forall (m :: * -> *) a.
Applicative m =>
a -> Stream m a -> Stream m a
cons forall (m :: * -> *) a. Applicative m => Stream m a
nil
{-# INLINE fromFoldableM #-}
fromFoldableM :: (Monad m, Foldable f) => f (m a) -> Stream m a
fromFoldableM :: forall (m :: * -> *) (f :: * -> *) a.
(Monad m, Foldable f) =>
f (m a) -> Stream m a
fromFoldableM = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Prelude.foldr forall (m :: * -> *) a.
Applicative m =>
m a -> Stream m a -> Stream m a
consM forall (m :: * -> *) a. Applicative m => Stream m a
nil
{-# INLINE fromPtr #-}
fromPtr :: forall m a. (Monad m, Storable a) => Ptr a -> Stream m a
fromPtr :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Ptr a -> Stream m a
fromPtr = forall (m :: * -> *) a s.
(State StreamK m a -> s -> m (Step s a)) -> s -> Stream m a
Stream forall {m :: * -> *} {a} {p} {b}.
(Monad m, Storable a) =>
p -> Ptr a -> m (Step (Ptr b) a)
step
where
{-# INLINE_LATE step #-}
step :: p -> Ptr a -> m (Step (Ptr b) a)
step p
_ Ptr a
p = do
let !x :: a
x = forall a. IO a -> a
unsafeInlineIO forall a b. (a -> b) -> a -> b
$ forall a. Storable a => Ptr a -> IO a
peek Ptr a
p
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s a. a -> s -> Step s a
Yield a
x (PTR_NEXT(p, a))
{-# INLINE fromPtrN #-}
fromPtrN :: (Monad m, Storable a) => Int -> Ptr a -> Stream m a
fromPtrN :: forall (m :: * -> *) a.
(Monad m, Storable a) =>
Int -> Ptr a -> Stream m a
fromPtrN Int
n = forall (m :: * -> *) a.
Applicative m =>
Int -> Stream m a -> Stream m a
take Int
n forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a.
(Monad m, Storable a) =>
Ptr a -> Stream m a
fromPtr
{-# INLINE fromByteStr# #-}
fromByteStr# :: Monad m => Addr# -> Stream m Word8
fromByteStr# :: forall (m :: * -> *). Monad m => Addr# -> Stream m Word8
fromByteStr# Addr#
addr = forall (m :: * -> *) a.
Monad m =>
(a -> Bool) -> Stream m a -> Stream m a
takeWhile (forall a. Eq a => a -> a -> Bool
/= Word8
0) forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a.
(Monad m, Storable a) =>
Ptr a -> Stream m a
fromPtr forall a b. (a -> b) -> a -> b
$ forall a. Addr# -> Ptr a
Ptr Addr#
addr