{-# LANGUAGE RecordWildCards #-}

{- |
A buffer keeping the last value, or zero-order hold.
-}
module FRP.Rhine.ResamplingBuffer.KeepLast where

-- automaton
import Data.Stream.Result (Result (..))

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

{- | Always keeps the last input value,
   or in case of no input an initialisation value.
   If @cl2@ approximates continuity,
   this behaves like a zero-order hold.
-}
keepLast :: (Monad m) => a -> ResamplingBuffer m cl1 cl2 a a
keepLast :: forall (m :: Type -> Type) a cl1 cl2.
Monad m =>
a -> ResamplingBuffer m cl1 cl2 a a
keepLast = AsyncMealy m a a a -> a -> ResamplingBuffer m cl1 cl2 a a
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 (Result a a)
a -> a -> m a
forall {m :: Type -> Type} {a}. Monad m => a -> m (Result a a)
forall {m :: Type -> Type} {p} {a}. Monad m => p -> a -> m a
amGet :: forall {m :: Type -> Type} {a}. Monad m => a -> m (Result a a)
amPut :: forall {m :: Type -> Type} {p} {a}. Monad m => p -> a -> m a
amPut :: a -> a -> m a
amGet :: a -> m (Result a a)
..}
  where
    amGet :: a -> m (Result a a)
amGet a
a = Result a a -> m (Result a a)
forall a. a -> m a
forall (m :: Type -> Type) a. Monad m => a -> m a
return (Result a a -> m (Result a a)) -> Result a a -> m (Result a a)
forall a b. (a -> b) -> a -> b
$! a -> a -> Result a a
forall s a. s -> a -> Result s a
Result a
a a
a
    amPut :: p -> a -> m a
amPut p
_ = a -> m a
forall a. a -> m a
forall (m :: Type -> Type) a. Monad m => a -> m a
return