stream-timers-0.1.0.0: Some stream timers, and experiments with them

Safe HaskellSafe
LanguageHaskell2010

Data.Timers

Synopsis

Documentation

foldrOnce :: (a -> b -> a) -> (a, [b]) -> (a, [b]) Source #

foldrOnce applies a right fold, only once (for streams, primarily).

foldrTimes :: (a -> b -> a) -> (a, [b]) -> Int -> (a, [b]) Source #

foldrTimes applies foldrOnce a given number of times.

foldlOnce :: (a -> b -> b) -> ([a], b) -> ([a], b) Source #

foldlOnce is the left-associative version of foldrOnce.

foldlTimes :: (a -> b -> b) -> ([a], b) -> Int -> ([a], b) Source #

foldrOnceM :: Monad m => (a -> b -> a) -> (a, [b]) -> m (a, [b]) Source #

foldrOnceM generalizes foldrOnceM to general monads.

foldrTimesM :: Monad m => (t -> t1 -> t) -> (t, [t1]) -> Int -> m (t, [t1]) Source #

foldlOnceM :: Monad m => (t1 -> t -> t) -> ([t1], t) -> m ([t1], t) Source #

foldlTimesM :: Monad m => (t1 -> t -> t) -> ([t1], t) -> Int -> m ([t1], t) Source #

nest :: (b -> b) -> b -> Int -> b Source #

nest applies f to x, n times.

nestM :: Monad m => (b -> m b) -> b -> Int -> m b Source #

nestM generalizes nest to functions returning Monad values.

nestByM :: Monad m => (b -> m b) -> b -> m Int -> m b Source #

nestByM generalizes nestM to numbers inside monads (for example, mn could be (read.getLine)).

nestMForever :: Monad m => (b -> m b) -> b -> m b Source #

nestMForever is equivalent to `nestM f x Infinity`.

λ> nestMForever (x -> print x >> return x) (return 10 :: Expire Int)
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {ge^Cpire = (1,Just 10)}
Expire {getExpire = (1,Just 10)}
Expire {getExpireInterrupted.
λ> nestMForever (x -> print x >> return x) (return 10 :: ExpireE Int)
ExpireE {getExpireE = (1,10)}
ExpireE {getExpireE = (1,10)}
ExpireE {getExpireE = (1,10)}
ExpireE {getExpireE = (1,10)}
ExpireE {getExpireE = (1,10)}
Ex^CE {getExpireE = (1,10)}
ExpireE {getExpireE = (1,10)}
ExpireE {getExpireE = (1,10)}Interrupted.
λ> dumpExpireIO == print) >> return x) (return 10 :: ExpireIO Int)
J^Cust (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1Interrupted.
λ> dumpExpireIO == print >> killExpireIO x) >> return x) (return 10 :: ExpireIO Int)
Just^Cust (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10Interrupted.
λ> dumpExpireIO == print) >> return x) (return 10 :: ExpireIO Int)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Just (1,10)
Jus^Ct (1,10)
JusInterrupted.

Real Mem: 157.4 MB

See `stable_infinite_ExpireIO_loop_sample.txt` for a sample

nestByOrd :: (a -> Ordering) -> (a -> a) -> (a -> a) -> a -> a Source #

This function assumes that applying f too many times is fine, it tries to apply f just enough times to hit EQ, then applies g and repeats forever

switchByPred :: (t1 -> t) -> (t1 -> t) -> (t1 -> Bool) -> t1 -> t Source #

switchByPred returns `f x` if `p x` else `g x`.

switchOnM :: (Monad m, Monad m1) => (b -> m1 b) -> (m1 b -> m b) -> m1 Int -> b -> m b Source #

switchEveryN :: Monad m => (b -> b) -> (b -> m b) -> Int -> b -> m b Source #

switchEveryN applies f, n times, then g once, then repeats forever.

switchEveryM :: Int -> Int -> (a -> IO a) -> (a -> IO a) -> a -> IO Void Source #

Seconds taken to be in [1..59]. This returns fElse of the input if the clock time's minutes == minutes, otherwise f x. Note that this function results in an infinite loop (which is why it has a return type of Void).

fixPt :: Eq a => (a -> a) -> a -> a Source #

fixPt gives the fixed point of f on x. Compare its type to that of fix:

 fix :: (a -> a) -> a

This fixed-point stops at equality, e.g. 1, 2, 3, 3, a.. -> 1, 2, 3.

printEveryN :: Show b => (b -> b) -> Int -> b -> IO b Source #

Nest the function the given number of times, on the given value, printing every iteration

 f n x -> (mapM_ print . take n . iterate f) x >> return (nest f n x)

sideEffectEveryN :: Monad m => (b -> m a) -> (b -> b) -> Int -> b -> m b Source #

Nest the function the given number of times, on the given value, resulting in the given side-effect every iteration

 s f n x -> (mapM_ s . take n . iterate f) x >> return (nest f n x)

switchEvery' :: (UTCTime, Integer, Int, IO t5 -> IO (IO t5), t5 -> IO (IO t5), IO t5) -> IO (UTCTime, Integer, Int, IO t5 -> IO (IO t5), t5 -> IO (IO t5), IO t5) Source #

The next step, switchEvery'' forever nestMForever switchEvery''

switchEvery'' :: (Integral t4, Integral t6) => (UTCTime, t6, t4, t7 -> IO t7, t5 -> IO t7, IO t5) -> IO (UTCTime, t6, Int, t7 -> IO t7, t5 -> IO t7, t7) Source #

This is a sketch of what a "log every so often, efficiently" function could look like.