{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE RecordWildCards #-}

{- |
Resampling buffers that collect the incoming data in some data structure
and release all of it on output.
-}
module FRP.Rhine.ResamplingBuffer.Collect where

-- containers
import Data.Sequence

-- rhine
import FRP.Rhine.ResamplingBuffer
import FRP.Rhine.ResamplingBuffer.Timeless

{- | Collects all input in a list, with the newest element at the head,
   which is returned and emptied upon `get`.
-}
collect :: Monad m => ResamplingBuffer m cl1 cl2 a [a]
collect :: forall (m :: Type -> Type) cl1 cl2 a.
Monad m =>
ResamplingBuffer m cl1 cl2 a [a]
collect = forall (m :: Type -> Type) s a b cl1 cl2.
Monad m =>
AsyncMealy m s a b -> s -> ResamplingBuffer m cl1 cl2 a b
timelessResamplingBuffer AsyncMealy {forall {m :: Type -> Type} {a}. Monad m => [a] -> a -> m [a]
forall {m :: Type -> Type} {a} {a}. Monad m => a -> m (a, [a])
amGet :: [a] -> m ([a], [a])
amPut :: [a] -> a -> m [a]
amGet :: forall {m :: Type -> Type} {a} {a}. Monad m => a -> m (a, [a])
amPut :: forall {m :: Type -> Type} {a}. Monad m => [a] -> a -> m [a]
..} []
  where
    amPut :: [a] -> a -> m [a]
amPut [a]
as a
a = forall (m :: Type -> Type) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a
a forall a. a -> [a] -> [a]
: [a]
as
    amGet :: a -> m (a, [a])
amGet a
as = forall (m :: Type -> Type) a. Monad m => a -> m a
return (a
as, [])

{- | Reimplementation of 'collect' with sequences,
   which gives a performance benefit if the sequence needs to be reversed or searched.
-}
collectSequence :: Monad m => ResamplingBuffer m cl1 cl2 a (Seq a)
collectSequence :: forall (m :: Type -> Type) cl1 cl2 a.
Monad m =>
ResamplingBuffer m cl1 cl2 a (Seq a)
collectSequence = forall (m :: Type -> Type) s a b cl1 cl2.
Monad m =>
AsyncMealy m s a b -> s -> ResamplingBuffer m cl1 cl2 a b
timelessResamplingBuffer AsyncMealy {forall {m :: Type -> Type} {a}. Monad m => Seq a -> a -> m (Seq a)
forall {m :: Type -> Type} {a} {a}. Monad m => a -> m (a, Seq a)
amGet :: forall {m :: Type -> Type} {a} {a}. Monad m => a -> m (a, Seq a)
amPut :: forall {m :: Type -> Type} {a}. Monad m => Seq a -> a -> m (Seq a)
amGet :: Seq a -> m (Seq a, Seq a)
amPut :: Seq a -> a -> m (Seq a)
..} forall a. Seq a
empty
  where
    amPut :: Seq a -> a -> m (Seq a)
amPut Seq a
as a
a = forall (m :: Type -> Type) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ a
a forall a. a -> Seq a -> Seq a
<| Seq a
as
    amGet :: a -> m (a, Seq a)
amGet a
as = forall (m :: Type -> Type) a. Monad m => a -> m a
return (a
as, forall a. Seq a
empty)

{- | 'pureBuffer' collects all input values lazily in a list
   and processes it when output is required.
   Semantically, @pureBuffer f == collect >>-^ arr f@,
   but 'pureBuffer' is slightly more efficient.
-}
pureBuffer :: Monad m => ([a] -> b) -> ResamplingBuffer m cl1 cl2 a b
pureBuffer :: forall (m :: Type -> Type) a b cl1 cl2.
Monad m =>
([a] -> b) -> ResamplingBuffer m cl1 cl2 a b
pureBuffer [a] -> b
f = forall (m :: Type -> Type) s a b cl1 cl2.
Monad m =>
AsyncMealy m s a b -> s -> ResamplingBuffer m cl1 cl2 a b
timelessResamplingBuffer AsyncMealy {[a] -> m (b, [a])
forall {m :: Type -> Type} {a}. Monad m => [a] -> a -> m [a]
amGet :: [a] -> m (b, [a])
amPut :: forall {m :: Type -> Type} {a}. Monad m => [a] -> a -> m [a]
amGet :: [a] -> m (b, [a])
amPut :: [a] -> a -> m [a]
..} []
  where
    amPut :: [a] -> a -> m [a]
amPut [a]
as a
a = forall (m :: Type -> Type) a. Monad m => a -> m a
return (a
a forall a. a -> [a] -> [a]
: [a]
as)
    amGet :: [a] -> m (b, [a])
amGet [a]
as = forall (m :: Type -> Type) a. Monad m => a -> m a
return ([a] -> b
f [a]
as, [])

-- TODO Test whether strictness works here, or consider using deepSeq

{- | A buffer collecting all incoming values with a folding function.
   It is strict, i.e. the state value 'b' is calculated on every 'put'.
-}
foldBuffer ::
  Monad m =>
  -- | The folding function
  (a -> b -> b) ->
  -- | The initial value
  b ->
  ResamplingBuffer m cl1 cl2 a b
foldBuffer :: forall (m :: Type -> Type) a b cl1 cl2.
Monad m =>
(a -> b -> b) -> b -> ResamplingBuffer m cl1 cl2 a b
foldBuffer a -> b -> b
f = forall (m :: Type -> Type) s a b cl1 cl2.
Monad m =>
AsyncMealy m s a b -> s -> ResamplingBuffer m cl1 cl2 a b
timelessResamplingBuffer AsyncMealy {b -> a -> m b
forall {m :: Type -> Type} {b}. Monad m => b -> m (b, b)
amGet :: forall {m :: Type -> Type} {b}. Monad m => b -> m (b, b)
amPut :: b -> a -> m b
amGet :: b -> m (b, b)
amPut :: b -> a -> m b
..}
  where
    amPut :: b -> a -> m b
amPut b
b a
a = let !b' :: b
b' = a -> b -> b
f a
a b
b in forall (m :: Type -> Type) a. Monad m => a -> m a
return b
b'
    amGet :: b -> m (b, b)
amGet b
b = forall (m :: Type -> Type) a. Monad m => a -> m a
return (b
b, b
b)