{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE Trustworthy #-}
#ifdef USE_REFLEX_OPTIMIZER
{-# OPTIONS_GHC -fplugin=Reflex.Optimizer #-}
#endif

-- |
-- Module:
--   Reflex.Class
-- Description:
--   This module contains the Reflex interface, as well as a variety of
--   convenience functions for working with 'Event's, 'Behavior's, and other
--   signals.
module Reflex.Class
  ( module Data.Patch
    -- * Primitives
  , Reflex (..)
  , mergeInt
  , coerceBehavior
  , coerceEvent
  , coerceDynamic
  , coerceIncremental
  , MonadSample (..)
  , MonadHold (..)
    -- ** 'fan' related types
  , EventSelector (..)
  , EventSelectorG (..)
  , EventSelectorInt (..)
    -- * Convenience functions
  , constDyn
  , pushAlways
    -- ** Combining 'Event's
  , leftmost
  , merge
  , mergeIncremental
  , mergeIncrementalWithMove
  , mergeMap
  , mergeIntMap
  , mergeMapIncremental
  , mergeMapIncrementalWithMove
  , mergeIntMapIncremental
  , coincidencePatchMap
  , coincidencePatchMapWithMove
  , coincidencePatchIntMap
  , mergeList
  , mergeWith
  , difference
  , alignEventWithMaybe
    -- ** Breaking up 'Event's
  , splitE
  , fan
  , fanEither
  , fanThese
  , fanMap
  , dmapToThese
  , EitherTag (..)
  , eitherToDSum
  , dsumToEither
  , factorEvent
  , filterEventKey
    -- ** Collapsing 'Event . Event'
  , switchHold
  , switchHoldPromptly
  , switchHoldPromptOnly
  , switchHoldPromptOnlyIncremental
    -- ** Using 'Event's to sample 'Behavior's
  , tag
  , tagMaybe
  , attach
  , attachWith
  , attachWithMaybe
    -- ** Blocking an 'Event' based on a 'Behavior'
  , gate
    -- ** Combining 'Dynamic's
  , distributeDMapOverDynPure
  , distributeDMapOverDynPureG
  , distributeListOverDyn
  , distributeListOverDynWith
  , zipDyn
  , zipDynWith
    -- ** Accumulating state
  , Accumulator (..)
  , accumDyn
  , accumMDyn
  , accumMaybeDyn
  , accumMaybeMDyn
  , mapAccumDyn
  , mapAccumMDyn
  , mapAccumMaybeDyn
  , mapAccumMaybeMDyn
  , accumB
  , accumMB
  , accumMaybeB
  , accumMaybeMB
  , mapAccumB
  , mapAccumMB
  , mapAccumMaybeB
  , mapAccumMaybeMB
  , mapAccum_
  , mapAccumM_
  , mapAccumMaybe_
  , mapAccumMaybeM_
  , accumIncremental
  , accumMIncremental
  , accumMaybeIncremental
  , accumMaybeMIncremental
  , mapAccumIncremental
  , mapAccumMIncremental
  , mapAccumMaybeIncremental
  , mapAccumMaybeMIncremental
  , zipListWithEvent
  , numberOccurrences
  , numberOccurrencesFrom
  , numberOccurrencesFrom_
  , (<@>)
  , (<@)
  , tailE
  , headTailE
  , takeWhileE
  , takeWhileJustE
  , dropWhileE
  , takeDropWhileJustE
  , switcher
    -- * Debugging functions
  , traceEvent
  , traceEventWith
    -- * Unsafe functions
  , unsafeDynamic
  , unsafeMapIncremental
    -- * 'Filterable' convenience functions
  , FunctorMaybe -- fmapMaybe is purposely not exported from deprecated 'FunctorMaybe' and the new alias is exported instead
  , mapMaybe -- Re-exported for convenience
  , fmapMaybe
  , fforMaybe
  , ffilter
  , filterLeft
  , filterRight
    -- * Miscellaneous convenience functions
  , ffor
  , ffor2
  , ffor3
    -- * Deprecated functions
  , switchPromptly
  , switchPromptOnly
  -- * "Cheap" functions
  , fmapMaybeCheap
  , mapMaybeCheap
  , fmapCheap
  , fforCheap
  , fforMaybeCheap
  , pushAlwaysCheap
  , tagCheap
  , mergeWithCheap
  , mergeWithCheap'
    -- * Slow, but general, implementations
  , slowHeadE
  ) where

#ifdef MIN_VERSION_semialign
import Prelude hiding (zip, zipWith)

#if MIN_VERSION_these(0,8,0)
import Data.These.Combinators (justThese)
#endif
#if MIN_VERSION_semialign(1,1,0)
import Data.Zip (Zip (..), Unzip (..))
#endif
#endif

import Control.Applicative
import Control.Monad.Identity
import Control.Monad.Reader
import Control.Monad.State.Strict
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.RWS (RWST)
import Control.Monad.Trans.Writer (WriterT)
import Data.Align
import Data.Bifunctor
import Data.Coerce
import Data.Default
import Data.Dependent.Map (DMap)
import Data.Dependent.Sum (DSum (..))
import qualified Data.Dependent.Map as DMap
import Data.Functor.Compose
import Data.Functor.Product
import Data.GADT.Compare (GEq (..), GCompare (..))
import Data.FastMutableIntMap (PatchIntMap)
import Data.Foldable
import Data.Functor.Bind
import Data.Functor.Misc
import Data.Functor.Plus
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import Data.Kind (Type)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Map (Map)
import Data.Semigroup (Semigroup (..))
import Data.Some (Some(Some))
import Data.String
import Data.These
import Data.Type.Coercion
import Data.Type.Equality ((:~:) (..))
import Data.Witherable (Filterable(..))
import qualified Data.Witherable as W
import Reflex.FunctorMaybe (FunctorMaybe)
import qualified Reflex.FunctorMaybe
import Data.Patch
import qualified Data.Patch.MapWithMove as PatchMapWithMove

import Debug.Trace (trace)

-- | The 'Reflex' class contains all the primitive functionality needed for
-- Functional Reactive Programming (FRP).  The @/t/@ type parameter indicates
-- which "timeline" is in use.  Timelines are fully-independent FRP contexts,
-- and the type of the timeline determines the FRP engine to be used.  For most
-- purposes, the 'Reflex.Spider' implementation is recommended.
class ( MonadHold t (PushM t)
      , MonadSample t (PullM t)
      , MonadFix (PushM t)
      , Functor (Dynamic t)
      , Applicative (Dynamic t) -- Necessary for GHC <= 7.8
      , Monad (Dynamic t)
      ) => Reflex t where
  -- | A container for a value that can change over time.  'Behavior's can be
  -- sampled at will, but it is not possible to be notified when they change
  data Behavior t :: Type -> Type
  -- | A stream of occurrences.  During any given frame, an 'Event' is either
  -- occurring or not occurring; if it is occurring, it will contain a value of
  -- the given type (its "occurrence type")
  data Event t :: Type -> Type
  -- | A container for a value that can change over time and allows
  -- notifications on changes.  Basically a combination of a 'Behavior' and an
  -- 'Event', with a rule that the 'Behavior' will change if and only if the
  -- 'Event' fires.
  data Dynamic t :: Type -> Type
  -- | An 'Incremental' is a more general form of  a 'Dynamic'.
  -- Instead of always fully replacing the value, only parts of it can be patched.
  -- This is only needed for performance critical code via `mergeIncremental` to make small
  -- changes to large values.
  data Incremental t :: Type -> Type
  -- | A monad for doing complex push-based calculations efficiently
  type PushM t :: Type -> Type
  -- | A monad for doing complex pull-based calculations efficiently
  type PullM t :: Type -> Type
  -- | An 'Event' with no occurrences
  never :: Event t a
  -- | Create a 'Behavior' that always has the given value
  constant :: a -> Behavior t a --TODO: Refactor to use 'pure' from Applicative instead; however, we need to make sure that encouraging Applicative-style use of 'Behavior's doesn't have a negative performance impact
  -- | Create an 'Event' from another 'Event'; the provided function can sample
  -- 'Behavior's and hold 'Event's, and use the results to produce a occurring
  -- (Just) or non-occurring (Nothing) result
  push :: (a -> PushM t (Maybe b)) -> Event t a -> Event t b
  -- | Like 'push' but intended for functions that the implementation can consider cheap to compute for performance considerations. WARNING: The function passed to 'pushCheap' may be run multiple times without any caching.
  pushCheap :: (a -> PushM t (Maybe b)) -> Event t a -> Event t b
  -- | Create a 'Behavior' by reading from other 'Behavior's; the result will be
  -- recomputed whenever any of the read 'Behavior's changes
  pull :: PullM t a -> Behavior t a
  -- | Merge a collection of events; the resulting 'Event' will only occur if at
  -- least one input event is occurring, and will contain all of the input keys
  -- that are occurring simultaneously

   --TODO: Generalize to get rid of DMap use --TODO: Provide a type-level guarantee that the result is not empty
  mergeG :: GCompare k => (forall a. q a -> Event t (v a))
         -> DMap k q -> Event t (DMap k v)

  -- | Efficiently fan-out an event to many destinations.  You should save the
  -- result in a @let@-binding, and then repeatedly 'selectG' on the result to
  -- create child events
  fanG :: GCompare k => Event t (DMap k v) -> EventSelectorG t k v

  -- | Create an 'Event' that will occur whenever the currently-selected input
  -- 'Event' occurs
  switch :: Behavior t (Event t a) -> Event t a
  -- | Create an 'Event' that will occur whenever the input event is occurring -- and its occurrence value, another 'Event', is also occurring.
  --   You maybe looking for '@switchHold@ @never@' instead.
  coincidence :: Event t (Event t a) -> Event t a
  -- | Extract the 'Behavior' of a 'Dynamic'.
  current :: Dynamic t a -> Behavior t a
  -- | Extract the 'Event' of the 'Dynamic'.
  updated :: Dynamic t a -> Event t a
  -- | Create a new 'Dynamic'.  The given 'PullM' must always return the most
  -- recent firing of the given 'Event', if any.
  unsafeBuildDynamic :: PullM t a -> Event t a -> Dynamic t a
  -- | Create a new 'Incremental'.  The given "PullM"'s value must always change
  -- in the same way that the accumulated application of patches would change
  -- that value.
  unsafeBuildIncremental :: Patch p => PullM t (PatchTarget p) -> Event t p -> Incremental t p
  -- | Create a merge whose parents can change over time
  mergeIncrementalG :: GCompare k
    => (forall a. q a -> Event t (v a))
    -> Incremental t (PatchDMap k q)
    -> Event t (DMap k v)
  -- | Experimental: Create a merge whose parents can change over time; changing the key of an Event is more efficient than with mergeIncremental
  mergeIncrementalWithMoveG :: GCompare k
    => (forall a. q a -> Event t (v a))
    -> Incremental t (PatchDMapWithMove k q) -> Event t (DMap k v)
  -- | Extract the 'Behavior' component of an 'Incremental'
  currentIncremental :: Patch p => Incremental t p -> Behavior t (PatchTarget p)
  -- | Extract the 'Event' component of an 'Incremental'
  updatedIncremental :: Patch p => Incremental t p -> Event t p
  -- | Convert an 'Incremental' to a 'Dynamic'
  incrementalToDynamic :: Patch p => Incremental t p -> Dynamic t (PatchTarget p)
  -- | Construct a 'Coercion' for a 'Behavior' given an 'Coercion' for its
  -- occurrence type
  behaviorCoercion :: Coercion a b -> Coercion (Behavior t a) (Behavior t b)
  -- | Construct a 'Coercion' for an 'Event' given an 'Coercion' for its
  -- occurrence type
  eventCoercion :: Coercion a b -> Coercion (Event t a) (Event t b)
  -- | Construct a 'Coercion' for a 'Dynamic' given an 'Coercion' for its
  -- occurrence type
  dynamicCoercion :: Coercion a b -> Coercion (Dynamic t a) (Dynamic t b)
  -- | Construct a 'Coercion' for an 'Incremental' given 'Coercion's for its
  -- patch target and patch types.
  incrementalCoercion
    :: Coercion (PatchTarget a) (PatchTarget b) -> Coercion a b -> Coercion (Incremental t a) (Incremental t b)
  mergeIntIncremental :: Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
  fanInt :: Event t (IntMap a) -> EventSelectorInt t a

-- | Efficiently fan-out an event to many destinations. You should save the
-- result in a @let@-binding, and then repeatedly 'select' on the result to
-- create child events
fan :: forall t k. (Reflex t, GCompare k)
    => Event t (DMap k Identity) -> EventSelector t k
    --TODO: Can we help enforce the partial application discipline here?  The combinator is worthless without it
fan :: forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Event t (DMap k Identity) -> EventSelector t k
fan Event t (DMap k Identity)
e = (forall a. k a -> Event t a) -> EventSelector t k
forall {k} (t :: k) (k :: * -> *).
(forall a. k a -> Event t a) -> EventSelector t k
EventSelector ((k a -> Event t (Identity a)) -> k a -> Event t a
forall a. (k a -> Event t (Identity a)) -> k a -> Event t a
fixup (EventSelectorG t k Identity
-> forall a. k a -> Event t (Identity a)
forall {k} {k} (t :: k) (k :: k -> *) (v :: k -> *).
EventSelectorG t k v -> forall (a :: k). k a -> Event t (v a)
selectG (Event t (DMap k Identity) -> EventSelectorG t k Identity
forall {k} (t :: k) {k} (k :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
Event t (DMap k v) -> EventSelectorG t k v
forall {k} (k :: k -> *) (v :: k -> *).
GCompare k =>
Event t (DMap k v) -> EventSelectorG t k v
fanG Event t (DMap k Identity)
e) :: k a -> Event t (Identity a)) :: forall a. k a -> Event t a)
  where
    fixup :: forall a. (k a -> Event t (Identity a)) -> k a -> Event t a
    fixup :: forall a. (k a -> Event t (Identity a)) -> k a -> Event t a
fixup = case Coercion (Identity a) a
-> Coercion (Event t (Identity a)) (Event t a)
forall {k} (t :: k) a b.
Reflex t =>
Coercion a b -> Coercion (Event t a) (Event t b)
forall a b. Coercion a b -> Coercion (Event t a) (Event t b)
eventCoercion Coercion (Identity a) a
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion :: Coercion (Event t (Identity a)) (Event t a) of
              Coercion (Event t (Identity a)) (Event t a)
Coercion -> (k a -> Event t (Identity a)) -> k a -> Event t a
forall a b. Coercible a b => a -> b
coerce

--TODO: Specialize this so that we can take advantage of knowing that there's no changing going on
-- | Constructs a single 'Event' out of a map of events. The output event may fire with multiple
-- keys simultaneously.
mergeInt :: Reflex t => IntMap (Event t a) -> Event t (IntMap a)
mergeInt :: forall {k} (t :: k) a.
Reflex t =>
IntMap (Event t a) -> Event t (IntMap a)
mergeInt IntMap (Event t a)
m = Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
forall a.
Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
forall {k} (t :: k) a.
Reflex t =>
Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
mergeIntIncremental (Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a))
-> Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
forall a b. (a -> b) -> a -> b
$ PullM t (PatchTarget (PatchIntMap (Event t a)))
-> Event t (PatchIntMap (Event t a))
-> Incremental t (PatchIntMap (Event t a))
forall p.
Patch p =>
PullM t (PatchTarget p) -> Event t p -> Incremental t p
forall {k} (t :: k) p.
(Reflex t, Patch p) =>
PullM t (PatchTarget p) -> Event t p -> Incremental t p
unsafeBuildIncremental (IntMap (Event t a) -> PullM t (IntMap (Event t a))
forall a. a -> PullM t a
forall (m :: * -> *) a. Monad m => a -> m a
return IntMap (Event t a)
m) Event t (PatchIntMap (Event t a))
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never

-- | Coerce a 'Behavior' between representationally-equivalent value types
coerceBehavior :: (Reflex t, Coercible a b) => Behavior t a -> Behavior t b
coerceBehavior :: forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Behavior t a -> Behavior t b
coerceBehavior = Coercion (Behavior t a) (Behavior t b)
-> Behavior t a -> Behavior t b
forall a b. Coercion a b -> a -> b
coerceWith (Coercion (Behavior t a) (Behavior t b)
 -> Behavior t a -> Behavior t b)
-> Coercion (Behavior t a) (Behavior t b)
-> Behavior t a
-> Behavior t b
forall a b. (a -> b) -> a -> b
$ Coercion a b -> Coercion (Behavior t a) (Behavior t b)
forall {k} (t :: k) a b.
Reflex t =>
Coercion a b -> Coercion (Behavior t a) (Behavior t b)
forall a b. Coercion a b -> Coercion (Behavior t a) (Behavior t b)
behaviorCoercion Coercion a b
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion

-- | Coerce an 'Event' between representationally-equivalent occurrence types
coerceEvent :: (Reflex t, Coercible a b) => Event t a -> Event t b
coerceEvent :: forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Event t a -> Event t b
coerceEvent = Coercion (Event t a) (Event t b) -> Event t a -> Event t b
forall a b. Coercion a b -> a -> b
coerceWith (Coercion (Event t a) (Event t b) -> Event t a -> Event t b)
-> Coercion (Event t a) (Event t b) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ Coercion a b -> Coercion (Event t a) (Event t b)
forall {k} (t :: k) a b.
Reflex t =>
Coercion a b -> Coercion (Event t a) (Event t b)
forall a b. Coercion a b -> Coercion (Event t a) (Event t b)
eventCoercion Coercion a b
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion

-- | Coerce a 'Dynamic' between representationally-equivalent value types
coerceDynamic :: (Reflex t, Coercible a b) => Dynamic t a -> Dynamic t b
coerceDynamic :: forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Dynamic t a -> Dynamic t b
coerceDynamic = Coercion (Dynamic t a) (Dynamic t b) -> Dynamic t a -> Dynamic t b
forall a b. Coercion a b -> a -> b
coerceWith (Coercion (Dynamic t a) (Dynamic t b)
 -> Dynamic t a -> Dynamic t b)
-> Coercion (Dynamic t a) (Dynamic t b)
-> Dynamic t a
-> Dynamic t b
forall a b. (a -> b) -> a -> b
$ Coercion a b -> Coercion (Dynamic t a) (Dynamic t b)
forall {k} (t :: k) a b.
Reflex t =>
Coercion a b -> Coercion (Dynamic t a) (Dynamic t b)
forall a b. Coercion a b -> Coercion (Dynamic t a) (Dynamic t b)
dynamicCoercion Coercion a b
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion

-- | Coerce an 'Incremental' between representationally-equivalent value types
coerceIncremental
  :: (Reflex t, Coercible a b, Coercible (PatchTarget a) (PatchTarget b))
  => Incremental t a -> Incremental t b
coerceIncremental :: forall {k} (t :: k) a b.
(Reflex t, Coercible a b,
 Coercible (PatchTarget a) (PatchTarget b)) =>
Incremental t a -> Incremental t b
coerceIncremental = Coercion (Incremental t a) (Incremental t b)
-> Incremental t a -> Incremental t b
forall a b. Coercion a b -> a -> b
coerceWith (Coercion (Incremental t a) (Incremental t b)
 -> Incremental t a -> Incremental t b)
-> Coercion (Incremental t a) (Incremental t b)
-> Incremental t a
-> Incremental t b
forall a b. (a -> b) -> a -> b
$ Coercion (PatchTarget a) (PatchTarget b)
-> Coercion a b -> Coercion (Incremental t a) (Incremental t b)
forall {k} (t :: k) a b.
Reflex t =>
Coercion (PatchTarget a) (PatchTarget b)
-> Coercion a b -> Coercion (Incremental t a) (Incremental t b)
forall a b.
Coercion (PatchTarget a) (PatchTarget b)
-> Coercion a b -> Coercion (Incremental t a) (Incremental t b)
incrementalCoercion Coercion (PatchTarget a) (PatchTarget b)
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion Coercion a b
forall {k} (a :: k) (b :: k). Coercible a b => Coercion a b
Coercion

-- | Construct a 'Dynamic' from a 'Behavior' and an 'Event'.  The 'Behavior'
-- __must__ change when and only when the 'Event' fires, such that the
-- 'Behavior''s value is always equal to the most recent firing of the 'Event';
-- if this is not the case, the resulting 'Dynamic' will behave
-- nondeterministically.
unsafeDynamic :: Reflex t => Behavior t a -> Event t a -> Dynamic t a
unsafeDynamic :: forall {k} (t :: k) a.
Reflex t =>
Behavior t a -> Event t a -> Dynamic t a
unsafeDynamic = PullM t a -> Event t a -> Dynamic t a
forall a. PullM t a -> Event t a -> Dynamic t a
forall {k} (t :: k) a.
Reflex t =>
PullM t a -> Event t a -> Dynamic t a
unsafeBuildDynamic (PullM t a -> Event t a -> Dynamic t a)
-> (Behavior t a -> PullM t a)
-> Behavior t a
-> Event t a
-> Dynamic t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

-- | Construct a 'Dynamic' value that never changes
constDyn :: Reflex t => a -> Dynamic t a
constDyn :: forall {k} (t :: k) a. Reflex t => a -> Dynamic t a
constDyn = a -> Dynamic t a
forall a. a -> Dynamic t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

instance (Reflex t, Default a) => Default (Dynamic t a) where
  def :: Dynamic t a
def = a -> Dynamic t a
forall a. a -> Dynamic t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Default a => a
def

-- | 'MonadSample' designates monads that can read the current value of a
-- 'Behavior'.  This includes both 'PullM' and 'PushM'.
class (Applicative m, Monad m) => MonadSample t m | m -> t where
  -- | Get the current value in the 'Behavior'
  sample :: Behavior t a -> m a

-- | 'MonadHold' designates monads that can create new 'Behavior's based on
-- 'Event's; usually this will be 'PushM' or a monad based on it.  'MonadHold'
-- is required to create any stateful computations with Reflex.
class MonadSample t m => MonadHold t m where
  -- | Create a new 'Behavior' whose value will initially be equal to the given
  -- value and will be updated whenever the given 'Event' occurs.  The update
  -- takes effect immediately after the 'Event' occurs; if the occurrence that
  -- sets the 'Behavior' (or one that is simultaneous with it) is used to sample
  -- the 'Behavior', it will see the __old__ value of the 'Behavior', not the new
  -- one.
  hold :: a -> Event t a -> m (Behavior t a)
  default hold :: (m ~ f m', MonadTrans f, MonadHold t m') => a -> Event t a -> m (Behavior t a)
  hold a
v0 = m' (Behavior t a) -> m (Behavior t a)
m' (Behavior t a) -> f m' (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> f m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Behavior t a) -> m (Behavior t a))
-> (Event t a -> m' (Behavior t a))
-> Event t a
-> m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m' (Behavior t a)
forall a. a -> Event t a -> m' (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
v0
  -- | Create a 'Dynamic' value using the given initial value that changes every
  -- time the 'Event' occurs.
  holdDyn :: a -> Event t a -> m (Dynamic t a)
  default holdDyn :: (m ~ f m', MonadTrans f, MonadHold t m') => a -> Event t a -> m (Dynamic t a)
  holdDyn a
v0 = m' (Dynamic t a) -> m (Dynamic t a)
m' (Dynamic t a) -> f m' (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> f m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Dynamic t a) -> m (Dynamic t a))
-> (Event t a -> m' (Dynamic t a)) -> Event t a -> m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m' (Dynamic t a)
forall a. a -> Event t a -> m' (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
v0
  -- | Create an 'Incremental' value using the given initial value that changes
  -- every time the 'Event' occurs.
  holdIncremental :: Patch p => PatchTarget p -> Event t p -> m (Incremental t p)
  default holdIncremental :: (Patch p, m ~ f m', MonadTrans f, MonadHold t m') => PatchTarget p -> Event t p -> m (Incremental t p)
  holdIncremental PatchTarget p
v0 = m' (Incremental t p) -> m (Incremental t p)
m' (Incremental t p) -> f m' (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> f m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m' (Incremental t p) -> m (Incremental t p))
-> (Event t p -> m' (Incremental t p))
-> Event t p
-> m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m' (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m' (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
v0
  buildDynamic :: PushM t a -> Event t a -> m (Dynamic t a)
  {-
  default buildDynamic :: (m ~ f m', MonadTrans f, MonadHold t m') => PullM t a -> Event t a -> m (Dynamic t a)
  buildDynamic getV0 = lift . buildDynamic getV0
  -}
  -- | Create a new 'Event' that only occurs only once, on the first occurrence of
  -- the supplied 'Event'.
  headE :: Event t a -> m (Event t a)
  -- | An event which only occurs at the current moment in time, such that:
  --
  -- > coincidence (pushAlways (\a -> (a <$) <$> now) e) = e
  --
  now :: m (Event t ())
  default now :: (m ~ f m', MonadTrans f, MonadHold t m') => m (Event t ())
  now = m' (Event t ()) -> f m' (Event t ())
forall (m :: * -> *) a. Monad m => m a -> f m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m' (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

-- | Accumulate an 'Incremental' with the supplied initial value and the firings of the provided 'Event',
-- using the combining function to produce a patch.
accumIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> p)
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p)
accumIncremental :: forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> p)
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumIncremental PatchTarget p -> b -> p
f = (PatchTarget p -> b -> Maybe p)
-> PatchTarget p -> Event t b -> m (Incremental t p)
forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> Maybe p)
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumMaybeIncremental ((PatchTarget p -> b -> Maybe p)
 -> PatchTarget p -> Event t b -> m (Incremental t p))
-> (PatchTarget p -> b -> Maybe p)
-> PatchTarget p
-> Event t b
-> m (Incremental t p)
forall a b. (a -> b) -> a -> b
$ \PatchTarget p
v b
o -> p -> Maybe p
forall a. a -> Maybe a
Just (p -> Maybe p) -> p -> Maybe p
forall a b. (a -> b) -> a -> b
$ PatchTarget p -> b -> p
f PatchTarget p
v b
o

-- | Similar to 'accumIncremental' but the combining function runs in 'PushM'
accumMIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> PushM t p)
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p)
accumMIncremental :: forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t p)
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumMIncremental PatchTarget p -> b -> PushM t p
f = (PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p -> Event t b -> m (Incremental t p)
forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumMaybeMIncremental ((PatchTarget p -> b -> PushM t (Maybe p))
 -> PatchTarget p -> Event t b -> m (Incremental t p))
-> (PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p
-> Event t b
-> m (Incremental t p)
forall a b. (a -> b) -> a -> b
$ \PatchTarget p
v b
o -> p -> Maybe p
forall a. a -> Maybe a
Just (p -> Maybe p) -> PushM t p -> PushM t (Maybe p)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PatchTarget p -> b -> PushM t p
f PatchTarget p
v b
o

-- | Similar to 'accumIncremental' but allows filtering of updates (by dropping updates when the
-- combining function produces @Nothing@)
accumMaybeIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> Maybe p)
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p)
accumMaybeIncremental :: forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> Maybe p)
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumMaybeIncremental PatchTarget p -> b -> Maybe p
f = (PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p -> Event t b -> m (Incremental t p)
forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumMaybeMIncremental ((PatchTarget p -> b -> PushM t (Maybe p))
 -> PatchTarget p -> Event t b -> m (Incremental t p))
-> (PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p
-> Event t b
-> m (Incremental t p)
forall a b. (a -> b) -> a -> b
$ \PatchTarget p
v b
o -> Maybe p -> PushM t (Maybe p)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe p -> PushM t (Maybe p)) -> Maybe p -> PushM t (Maybe p)
forall a b. (a -> b) -> a -> b
$ PatchTarget p -> b -> Maybe p
f PatchTarget p
v b
o

-- | Similar to 'accumMaybeMIncremental' but the combining function runs in 'PushM'
accumMaybeMIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> PushM t (Maybe p))
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p)
accumMaybeMIncremental :: forall {k} (t :: k) p (m :: * -> *) b.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (Maybe p))
-> PatchTarget p -> Event t b -> m (Incremental t p)
accumMaybeMIncremental PatchTarget p -> b -> PushM t (Maybe p)
f PatchTarget p
z Event t b
e = do
  rec let e' :: Event t p
e' = ((b -> PushM t (Maybe p)) -> Event t b -> Event t p)
-> Event t b -> (b -> PushM t (Maybe p)) -> Event t p
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe p)) -> Event t b -> Event t p
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe p)) -> Event t p)
-> (b -> PushM t (Maybe p)) -> Event t p
forall a b. (a -> b) -> a -> b
$ \b
o -> do
            PatchTarget p
v <- Behavior t (PatchTarget p) -> PushM t (PatchTarget p)
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (PatchTarget p) -> PushM t (PatchTarget p))
-> Behavior t (PatchTarget p) -> PushM t (PatchTarget p)
forall a b. (a -> b) -> a -> b
$ Incremental t p -> Behavior t (PatchTarget p)
forall p. Patch p => Incremental t p -> Behavior t (PatchTarget p)
forall {k} (t :: k) p.
(Reflex t, Patch p) =>
Incremental t p -> Behavior t (PatchTarget p)
currentIncremental Incremental t p
d'
            PatchTarget p -> b -> PushM t (Maybe p)
f PatchTarget p
v b
o
      Incremental t p
d' <- PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
z Event t p
e'
  Incremental t p -> m (Incremental t p)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Incremental t p
d'

-- | Accumulate an 'Incremental' by folding occurrences of an 'Event'
-- with a function that both accumulates and produces a value to fire
-- as an 'Event'. Returns both the accumulated value and the constructed
-- 'Event'.
mapAccumIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> (p, c))
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p, Event t c)
mapAccumIncremental :: forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> (p, c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumIncremental PatchTarget p -> b -> (p, c)
f = (PatchTarget p -> b -> (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumMaybeIncremental ((PatchTarget p -> b -> (Maybe p, Maybe c))
 -> PatchTarget p -> Event t b -> m (Incremental t p, Event t c))
-> (PatchTarget p -> b -> (Maybe p, Maybe c))
-> PatchTarget p
-> Event t b
-> m (Incremental t p, Event t c)
forall a b. (a -> b) -> a -> b
$ \PatchTarget p
v b
o -> (p -> Maybe p) -> (c -> Maybe c) -> (p, c) -> (Maybe p, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap p -> Maybe p
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((p, c) -> (Maybe p, Maybe c)) -> (p, c) -> (Maybe p, Maybe c)
forall a b. (a -> b) -> a -> b
$ PatchTarget p -> b -> (p, c)
f PatchTarget p
v b
o

-- | Like 'mapAccumIncremental' but the combining function runs in 'PushM'
mapAccumMIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> PushM t (p, c))
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p, Event t c)
mapAccumMIncremental :: forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (p, c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumMIncremental PatchTarget p -> b -> PushM t (p, c)
f = (PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumMaybeMIncremental ((PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
 -> PatchTarget p -> Event t b -> m (Incremental t p, Event t c))
-> (PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p
-> Event t b
-> m (Incremental t p, Event t c)
forall a b. (a -> b) -> a -> b
$ \PatchTarget p
v b
o -> (p -> Maybe p) -> (c -> Maybe c) -> (p, c) -> (Maybe p, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap p -> Maybe p
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((p, c) -> (Maybe p, Maybe c))
-> PushM t (p, c) -> PushM t (Maybe p, Maybe c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PatchTarget p -> b -> PushM t (p, c)
f PatchTarget p
v b
o

-- | Accumulate an 'Incremental' by folding occurrences of an 'Event' with
-- a function that both optionally accumulates and optionally produces
-- a value to fire as a separate output 'Event'.
-- Note that because 'Nothing's are discarded in both cases, the output
-- 'Event' may fire even though the output 'Incremental' has not changed, and
-- the output 'Incremental' may update even when the output 'Event' is not firing.
mapAccumMaybeIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> (Maybe p, Maybe c))
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p, Event t c)
mapAccumMaybeIncremental :: forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumMaybeIncremental PatchTarget p -> b -> (Maybe p, Maybe c)
f = (PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumMaybeMIncremental ((PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
 -> PatchTarget p -> Event t b -> m (Incremental t p, Event t c))
-> (PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p
-> Event t b
-> m (Incremental t p, Event t c)
forall a b. (a -> b) -> a -> b
$ \PatchTarget p
v b
o -> (Maybe p, Maybe c) -> PushM t (Maybe p, Maybe c)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Maybe p, Maybe c) -> PushM t (Maybe p, Maybe c))
-> (Maybe p, Maybe c) -> PushM t (Maybe p, Maybe c)
forall a b. (a -> b) -> a -> b
$ PatchTarget p -> b -> (Maybe p, Maybe c)
f PatchTarget p
v b
o

-- | Like 'mapAccumMaybeIncremental' but the combining function is a 'PushM' action
mapAccumMaybeMIncremental
  :: (Reflex t, Patch p, MonadHold t m, MonadFix m)
  => (PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
  -> PatchTarget p
  -> Event t b
  -> m (Incremental t p, Event t c)
mapAccumMaybeMIncremental :: forall {k} (t :: k) p (m :: * -> *) b c.
(Reflex t, Patch p, MonadHold t m, MonadFix m) =>
(PatchTarget p -> b -> PushM t (Maybe p, Maybe c))
-> PatchTarget p -> Event t b -> m (Incremental t p, Event t c)
mapAccumMaybeMIncremental PatchTarget p -> b -> PushM t (Maybe p, Maybe c)
f PatchTarget p
z Event t b
e = do
  rec let e' :: Event t (Maybe p, Maybe c)
e' = ((b -> PushM t (Maybe (Maybe p, Maybe c)))
 -> Event t b -> Event t (Maybe p, Maybe c))
-> Event t b
-> (b -> PushM t (Maybe (Maybe p, Maybe c)))
-> Event t (Maybe p, Maybe c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe (Maybe p, Maybe c)))
-> Event t b -> Event t (Maybe p, Maybe c)
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe (Maybe p, Maybe c)))
 -> Event t (Maybe p, Maybe c))
-> (b -> PushM t (Maybe (Maybe p, Maybe c)))
-> Event t (Maybe p, Maybe c)
forall a b. (a -> b) -> a -> b
$ \b
o -> do
            PatchTarget p
v <- Behavior t (PatchTarget p) -> PushM t (PatchTarget p)
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (PatchTarget p) -> PushM t (PatchTarget p))
-> Behavior t (PatchTarget p) -> PushM t (PatchTarget p)
forall a b. (a -> b) -> a -> b
$ Incremental t p -> Behavior t (PatchTarget p)
forall p. Patch p => Incremental t p -> Behavior t (PatchTarget p)
forall {k} (t :: k) p.
(Reflex t, Patch p) =>
Incremental t p -> Behavior t (PatchTarget p)
currentIncremental Incremental t p
d'
            (Maybe p, Maybe c)
result <- PatchTarget p -> b -> PushM t (Maybe p, Maybe c)
f PatchTarget p
v b
o
            Maybe (Maybe p, Maybe c) -> PushM t (Maybe (Maybe p, Maybe c))
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Maybe p, Maybe c) -> PushM t (Maybe (Maybe p, Maybe c)))
-> Maybe (Maybe p, Maybe c) -> PushM t (Maybe (Maybe p, Maybe c))
forall a b. (a -> b) -> a -> b
$ case (Maybe p, Maybe c)
result of
              (Maybe p
Nothing, Maybe c
Nothing) -> Maybe (Maybe p, Maybe c)
forall a. Maybe a
Nothing
              (Maybe p, Maybe c)
_ -> (Maybe p, Maybe c) -> Maybe (Maybe p, Maybe c)
forall a. a -> Maybe a
Just (Maybe p, Maybe c)
result
      Incremental t p
d' <- PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
z (Event t p -> m (Incremental t p))
-> Event t p -> m (Incremental t p)
forall a b. (a -> b) -> a -> b
$ ((Maybe p, Maybe c) -> Maybe p)
-> Event t (Maybe p, Maybe c) -> Event t p
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (Maybe p, Maybe c) -> Maybe p
forall a b. (a, b) -> a
fst Event t (Maybe p, Maybe c)
e'
  (Incremental t p, Event t c) -> m (Incremental t p, Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Incremental t p
d', ((Maybe p, Maybe c) -> Maybe c)
-> Event t (Maybe p, Maybe c) -> Event t c
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (Maybe p, Maybe c) -> Maybe c
forall a b. (a, b) -> b
snd Event t (Maybe p, Maybe c)
e')

-- | A somewhat slow implementation of 'headE'
slowHeadE :: (Reflex t, MonadHold t m, MonadFix m) => Event t a -> m (Event t a)
slowHeadE :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m, MonadFix m) =>
Event t a -> m (Event t a)
slowHeadE Event t a
e = do
  rec Behavior t (Event t a)
be <- Event t a -> Event t (Event t a) -> m (Behavior t (Event t a))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold Event t a
e (Event t (Event t a) -> m (Behavior t (Event t a)))
-> Event t (Event t a) -> m (Behavior t (Event t a))
forall a b. (a -> b) -> a -> b
$ (a -> Event t a) -> Event t a -> Event t (Event t a)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (Event t a -> a -> Event t a
forall a b. a -> b -> a
const Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never) Event t a
e'
      let e' :: Event t a
e' = Behavior t (Event t a) -> Event t a
forall a. Behavior t (Event t a) -> Event t a
forall {k} (t :: k) a.
Reflex t =>
Behavior t (Event t a) -> Event t a
switch Behavior t (Event t a)
be
  Event t a -> m (Event t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Event t a
e'

-- | An 'EventSelector' allows you to efficiently 'select' an 'Event' based on a
-- key.  This is much more efficient than filtering for each key with
-- 'mapMaybe'.
newtype EventSelector t k = EventSelector
  { -- | Retrieve the 'Event' for the given key.  The type of the 'Event' is
    -- determined by the type of the key, so this can be used to fan-out
    -- 'Event's whose sub-'Event's have different types.
    --
    -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
    -- (but equivalent to) using 'mapMaybe' to select only the relevant
    -- occurrences of an 'Event'.
    forall {k} (t :: k) (k :: * -> *).
EventSelector t k -> forall a. k a -> Event t a
select :: forall a. k a -> Event t a
  }

newtype EventSelectorG t k v = EventSelectorG
  { -- | Retrieve the 'Event' for the given key.  The type of the 'Event' is
    -- determined by the type of the key, so this can be used to fan-out
    -- 'Event's whose sub-'Event's have different types.
    --
    -- Using 'EventSelector's and the 'fan' primitive is far more efficient than
    -- (but equivalent to) using 'mapMaybe' to select only the relevant
    -- occurrences of an 'Event'.
    forall {k} {k} (t :: k) (k :: k -> *) (v :: k -> *).
EventSelectorG t k v -> forall (a :: k). k a -> Event t (v a)
selectG :: forall a. k a -> Event t (v a)
  }

-- | Efficiently select an 'Event' keyed on 'Int'. This is more efficient than manually
-- filtering by key.
newtype EventSelectorInt t a = EventSelectorInt { forall {k} (t :: k) a. EventSelectorInt t a -> Int -> Event t a
selectInt :: Int -> Event t a }

--------------------------------------------------------------------------------
-- Instances
--------------------------------------------------------------------------------

instance MonadSample t m => MonadSample t (ReaderT r m) where
  sample :: forall a. Behavior t a -> ReaderT r m a
sample = m a -> ReaderT r m a
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ReaderT r m a)
-> (Behavior t a -> m a) -> Behavior t a -> ReaderT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance MonadHold t m => MonadHold t (ReaderT r m) where
  hold :: forall a. a -> Event t a -> ReaderT r m (Behavior t a)
hold a
a0 = m (Behavior t a) -> ReaderT r m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> ReaderT r m (Behavior t a))
-> (Event t a -> m (Behavior t a))
-> Event t a
-> ReaderT r m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
a0
  holdDyn :: forall a. a -> Event t a -> ReaderT r m (Dynamic t a)
holdDyn a
a0 = m (Dynamic t a) -> ReaderT r m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> ReaderT r m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> ReaderT r m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
a0
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> ReaderT r m (Incremental t p)
holdIncremental PatchTarget p
a0 = m (Incremental t p) -> ReaderT r m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> ReaderT r m (Incremental t p))
-> (Event t p -> m (Incremental t p))
-> Event t p
-> ReaderT r m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
a0
  buildDynamic :: forall a. PushM t a -> Event t a -> ReaderT r m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> ReaderT r m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> ReaderT r m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> ReaderT r m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  headE :: forall a. Event t a -> ReaderT r m (Event t a)
headE = m (Event t a) -> ReaderT r m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> ReaderT r m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> ReaderT r m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  now :: ReaderT r m (Event t ())
now = m (Event t ()) -> ReaderT r m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> ReaderT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

instance (MonadSample t m, Monoid r) => MonadSample t (WriterT r m) where
  sample :: forall a. Behavior t a -> WriterT r m a
sample = m a -> WriterT r m a
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> WriterT r m a)
-> (Behavior t a -> m a) -> Behavior t a -> WriterT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance (MonadHold t m, Monoid r) => MonadHold t (WriterT r m) where
  hold :: forall a. a -> Event t a -> WriterT r m (Behavior t a)
hold a
a0 = m (Behavior t a) -> WriterT r m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> WriterT r m (Behavior t a))
-> (Event t a -> m (Behavior t a))
-> Event t a
-> WriterT r m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
a0
  holdDyn :: forall a. a -> Event t a -> WriterT r m (Dynamic t a)
holdDyn a
a0 = m (Dynamic t a) -> WriterT r m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> WriterT r m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> WriterT r m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
a0
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> WriterT r m (Incremental t p)
holdIncremental PatchTarget p
a0 = m (Incremental t p) -> WriterT r m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> WriterT r m (Incremental t p))
-> (Event t p -> m (Incremental t p))
-> Event t p
-> WriterT r m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
a0
  buildDynamic :: forall a. PushM t a -> Event t a -> WriterT r m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> WriterT r m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> WriterT r m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> WriterT r m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  headE :: forall a. Event t a -> WriterT r m (Event t a)
headE = m (Event t a) -> WriterT r m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> WriterT r m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> WriterT r m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  now :: WriterT r m (Event t ())
now = m (Event t ()) -> WriterT r m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> WriterT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

instance MonadSample t m => MonadSample t (StateT s m) where
  sample :: forall a. Behavior t a -> StateT s m a
sample = m a -> StateT s m a
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> StateT s m a)
-> (Behavior t a -> m a) -> Behavior t a -> StateT s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance MonadHold t m => MonadHold t (StateT s m) where
  hold :: forall a. a -> Event t a -> StateT s m (Behavior t a)
hold a
a0 = m (Behavior t a) -> StateT s m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> StateT s m (Behavior t a))
-> (Event t a -> m (Behavior t a))
-> Event t a
-> StateT s m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
a0
  holdDyn :: forall a. a -> Event t a -> StateT s m (Dynamic t a)
holdDyn a
a0 = m (Dynamic t a) -> StateT s m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> StateT s m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> StateT s m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
a0
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> StateT s m (Incremental t p)
holdIncremental PatchTarget p
a0 = m (Incremental t p) -> StateT s m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> StateT s m (Incremental t p))
-> (Event t p -> m (Incremental t p))
-> Event t p
-> StateT s m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
a0
  buildDynamic :: forall a. PushM t a -> Event t a -> StateT s m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> StateT s m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> StateT s m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> StateT s m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  headE :: forall a. Event t a -> StateT s m (Event t a)
headE = m (Event t a) -> StateT s m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> StateT s m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> StateT s m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  now :: StateT s m (Event t ())
now = m (Event t ()) -> StateT s m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> StateT s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

instance MonadSample t m => MonadSample t (ExceptT e m) where
  sample :: forall a. Behavior t a -> ExceptT e m a
sample = m a -> ExceptT e m a
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ExceptT e m a)
-> (Behavior t a -> m a) -> Behavior t a -> ExceptT e m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance MonadHold t m => MonadHold t (ExceptT e m) where
  hold :: forall a. a -> Event t a -> ExceptT e m (Behavior t a)
hold a
a0 = m (Behavior t a) -> ExceptT e m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> ExceptT e m (Behavior t a))
-> (Event t a -> m (Behavior t a))
-> Event t a
-> ExceptT e m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
a0
  holdDyn :: forall a. a -> Event t a -> ExceptT e m (Dynamic t a)
holdDyn a
a0 = m (Dynamic t a) -> ExceptT e m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> ExceptT e m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> ExceptT e m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
a0
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> ExceptT e m (Incremental t p)
holdIncremental PatchTarget p
a0 = m (Incremental t p) -> ExceptT e m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> ExceptT e m (Incremental t p))
-> (Event t p -> m (Incremental t p))
-> Event t p
-> ExceptT e m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
a0
  buildDynamic :: forall a. PushM t a -> Event t a -> ExceptT e m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> ExceptT e m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> ExceptT e m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> ExceptT e m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  headE :: forall a. Event t a -> ExceptT e m (Event t a)
headE = m (Event t a) -> ExceptT e m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> ExceptT e m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> ExceptT e m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  now :: ExceptT e m (Event t ())
now = m (Event t ()) -> ExceptT e m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> ExceptT e m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

instance (MonadSample t m, Monoid w) => MonadSample t (RWST r w s m) where
  sample :: forall a. Behavior t a -> RWST r w s m a
sample = m a -> RWST r w s m a
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> RWST r w s m a)
-> (Behavior t a -> m a) -> Behavior t a -> RWST r w s m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance (MonadHold t m, Monoid w) => MonadHold t (RWST r w s m) where
  hold :: forall a. a -> Event t a -> RWST r w s m (Behavior t a)
hold a
a0 = m (Behavior t a) -> RWST r w s m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> RWST r w s m (Behavior t a))
-> (Event t a -> m (Behavior t a))
-> Event t a
-> RWST r w s m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
a0
  holdDyn :: forall a. a -> Event t a -> RWST r w s m (Dynamic t a)
holdDyn a
a0 = m (Dynamic t a) -> RWST r w s m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> RWST r w s m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> RWST r w s m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
a0
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> RWST r w s m (Incremental t p)
holdIncremental PatchTarget p
a0 = m (Incremental t p) -> RWST r w s m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> RWST r w s m (Incremental t p))
-> (Event t p -> m (Incremental t p))
-> Event t p
-> RWST r w s m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
a0
  buildDynamic :: forall a. PushM t a -> Event t a -> RWST r w s m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> RWST r w s m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> RWST r w s m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> RWST r w s m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  headE :: forall a. Event t a -> RWST r w s m (Event t a)
headE = m (Event t a) -> RWST r w s m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> RWST r w s m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> RWST r w s m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  now :: RWST r w s m (Event t ())
now = m (Event t ()) -> RWST r w s m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> RWST r w s m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

instance MonadSample t m => MonadSample t (ContT r m) where
  sample :: forall a. Behavior t a -> ContT r m a
sample = m a -> ContT r m a
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m a -> ContT r m a)
-> (Behavior t a -> m a) -> Behavior t a -> ContT r m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> m a
forall a. Behavior t a -> m a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance MonadHold t m => MonadHold t (ContT r m) where
  hold :: forall a. a -> Event t a -> ContT r m (Behavior t a)
hold a
a0 = m (Behavior t a) -> ContT r m (Behavior t a)
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Behavior t a) -> ContT r m (Behavior t a))
-> (Event t a -> m (Behavior t a))
-> Event t a
-> ContT r m (Behavior t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
a0
  holdDyn :: forall a. a -> Event t a -> ContT r m (Dynamic t a)
holdDyn a
a0 = m (Dynamic t a) -> ContT r m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> ContT r m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> ContT r m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
a0
  holdIncremental :: forall p.
Patch p =>
PatchTarget p -> Event t p -> ContT r m (Incremental t p)
holdIncremental PatchTarget p
a0 = m (Incremental t p) -> ContT r m (Incremental t p)
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Incremental t p) -> ContT r m (Incremental t p))
-> (Event t p -> m (Incremental t p))
-> Event t p
-> ContT r m (Incremental t p)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatchTarget p -> Event t p -> m (Incremental t p)
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental PatchTarget p
a0
  buildDynamic :: forall a. PushM t a -> Event t a -> ContT r m (Dynamic t a)
buildDynamic PushM t a
a0 = m (Dynamic t a) -> ContT r m (Dynamic t a)
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Dynamic t a) -> ContT r m (Dynamic t a))
-> (Event t a -> m (Dynamic t a))
-> Event t a
-> ContT r m (Dynamic t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PushM t a -> Event t a -> m (Dynamic t a)
forall a. PushM t a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
PushM t a -> Event t a -> m (Dynamic t a)
buildDynamic PushM t a
a0
  headE :: forall a. Event t a -> ContT r m (Event t a)
headE = m (Event t a) -> ContT r m (Event t a)
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (Event t a) -> ContT r m (Event t a))
-> (Event t a -> m (Event t a))
-> Event t a
-> ContT r m (Event t a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE
  now :: ContT r m (Event t ())
now = m (Event t ()) -> ContT r m (Event t ())
forall (m :: * -> *) a. Monad m => m a -> ContT r m a
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift m (Event t ())
forall {k} (t :: k) (m :: * -> *). MonadHold t m => m (Event t ())
now

--------------------------------------------------------------------------------
-- Convenience functions
--------------------------------------------------------------------------------

-- | Create an 'Event' from another 'Event'.  The provided function can sample
-- 'Behavior's and hold 'Event's.
pushAlways :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
pushAlways :: forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t b) -> Event t a -> Event t b
pushAlways a -> PushM t b
f = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push ((b -> Maybe b) -> PushM t b -> PushM t (Maybe b)
forall a b. (a -> b) -> PushM t a -> PushM t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> Maybe b
forall a. a -> Maybe a
Just (PushM t b -> PushM t (Maybe b))
-> (a -> PushM t b) -> a -> PushM t (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> PushM t b
f)

-- | Flipped version of 'fmap'.
ffor :: Functor f => f a -> (a -> b) -> f b
ffor :: forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor = ((a -> b) -> f a -> f b) -> f a -> (a -> b) -> f b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> f a -> f b
forall a b. (a -> b) -> f a -> f b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap

-- | Rotated version of 'liftA2'.
ffor2 :: Applicative f => f a -> f b -> (a -> b -> c) -> f c
ffor2 :: forall (f :: * -> *) a b c.
Applicative f =>
f a -> f b -> (a -> b -> c) -> f c
ffor2 f a
a f b
b a -> b -> c
f = (a -> b -> c) -> f a -> f b -> f c
forall a b c. (a -> b -> c) -> f a -> f b -> f c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> c
f f a
a f b
b

-- | Rotated version of 'liftA3'.
ffor3 :: Applicative f => f a -> f b -> f c -> (a -> b -> c -> d) -> f d
ffor3 :: forall (f :: * -> *) a b c d.
Applicative f =>
f a -> f b -> f c -> (a -> b -> c -> d) -> f d
ffor3 f a
a f b
b f c
c a -> b -> c -> d
f = (a -> b -> c -> d) -> f a -> f b -> f c -> f d
forall (f :: * -> *) a b c d.
Applicative f =>
(a -> b -> c -> d) -> f a -> f b -> f c -> f d
liftA3 a -> b -> c -> d
f f a
a f b
b f c
c

instance Reflex t => Applicative (Behavior t) where
  pure :: forall a. a -> Behavior t a
pure = a -> Behavior t a
forall a. a -> Behavior t a
forall k (t :: k) a. Reflex t => a -> Behavior t a
constant
  Behavior t (a -> b)
f <*> :: forall a b. Behavior t (a -> b) -> Behavior t a -> Behavior t b
<*> Behavior t a
x = PullM t b -> Behavior t b
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t b -> Behavior t b) -> PullM t b -> Behavior t b
forall a b. (a -> b) -> a -> b
$ Behavior t (a -> b) -> PullM t (a -> b)
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t (a -> b)
f PullM t (a -> b) -> PullM t a -> PullM t b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
`ap` Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
x
  Behavior t a
_ *> :: forall a b. Behavior t a -> Behavior t b -> Behavior t b
*> Behavior t b
b = Behavior t b
b
  Behavior t a
a <* :: forall a b. Behavior t a -> Behavior t b -> Behavior t a
<* Behavior t b
_ = Behavior t a
a

instance Reflex t => Apply (Behavior t) where
  <.> :: forall a b. Behavior t (a -> b) -> Behavior t a -> Behavior t b
(<.>) = Behavior t (a -> b) -> Behavior t a -> Behavior t b
forall a b. Behavior t (a -> b) -> Behavior t a -> Behavior t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
(<*>)

instance Reflex t => Bind (Behavior t) where
  >>- :: forall a b. Behavior t a -> (a -> Behavior t b) -> Behavior t b
(>>-) = Behavior t a -> (a -> Behavior t b) -> Behavior t b
forall a b. Behavior t a -> (a -> Behavior t b) -> Behavior t b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
(>>=)

instance (Reflex t, Fractional a) => Fractional (Behavior t a) where
  / :: Behavior t a -> Behavior t a -> Behavior t a
(/) = (a -> a -> a) -> Behavior t a -> Behavior t a -> Behavior t a
forall a b c.
(a -> b -> c) -> Behavior t a -> Behavior t b -> Behavior t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Fractional a => a -> a -> a
(/)
  fromRational :: Rational -> Behavior t a
fromRational = a -> Behavior t a
forall a. a -> Behavior t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Behavior t a) -> (Rational -> a) -> Rational -> Behavior t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> a
forall a. Fractional a => Rational -> a
fromRational
  recip :: Behavior t a -> Behavior t a
recip = (a -> a) -> Behavior t a -> Behavior t a
forall a b. (a -> b) -> Behavior t a -> Behavior t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Fractional a => a -> a
recip

instance Reflex t => Functor (Behavior t) where
  fmap :: forall a b. (a -> b) -> Behavior t a -> Behavior t b
fmap a -> b
f = PullM t b -> Behavior t b
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t b -> Behavior t b)
-> (Behavior t a -> PullM t b) -> Behavior t a -> Behavior t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> PullM t a -> PullM t b
forall a b. (a -> b) -> PullM t a -> PullM t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f (PullM t a -> PullM t b)
-> (Behavior t a -> PullM t a) -> Behavior t a -> PullM t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance (Reflex t, IsString a) => IsString (Behavior t a) where
  fromString :: String -> Behavior t a
fromString = a -> Behavior t a
forall a. a -> Behavior t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Behavior t a) -> (String -> a) -> String -> Behavior t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> a
forall a. IsString a => String -> a
fromString

instance Reflex t => Monad (Behavior t) where
  Behavior t a
a >>= :: forall a b. Behavior t a -> (a -> Behavior t b) -> Behavior t b
>>= a -> Behavior t b
f = PullM t b -> Behavior t b
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t b -> Behavior t b) -> PullM t b -> Behavior t b
forall a b. (a -> b) -> a -> b
$ Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
a PullM t a -> (a -> PullM t b) -> PullM t b
forall a b. PullM t a -> (a -> PullM t b) -> PullM t b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Behavior t b -> PullM t b
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t b -> PullM t b)
-> (a -> Behavior t b) -> a -> PullM t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Behavior t b
f
  -- Note: it is tempting to write (_ >> b = b); however, this would result in (fail x >> return y) succeeding (returning y), which violates the law that (a >> b = a >>= \_ -> b), since the implementation of (>>=) above actually will fail.  Since we can't examine 'Behavior's other than by using sample, I don't think it's possible to write (>>) to be more efficient than the (>>=) above.
  return :: forall a. a -> Behavior t a
return = a -> Behavior t a
forall a. a -> Behavior t a
forall k (t :: k) a. Reflex t => a -> Behavior t a
constant
#if !MIN_VERSION_base(4,13,0)
  fail = error "Monad (Behavior t) does not support fail"
#endif

instance (Reflex t, Monoid a) => Monoid (Behavior t a) where
  mempty :: Behavior t a
mempty = a -> Behavior t a
forall a. a -> Behavior t a
forall k (t :: k) a. Reflex t => a -> Behavior t a
constant a
forall a. Monoid a => a
mempty
  mappend :: Behavior t a -> Behavior t a -> Behavior t a
mappend Behavior t a
a Behavior t a
b = PullM t a -> Behavior t a
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t a -> Behavior t a) -> PullM t a -> Behavior t a
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> PullM t a -> PullM t a -> PullM t a
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> a -> a
forall a. Monoid a => a -> a -> a
mappend (Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
a) (Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
b)
  mconcat :: [Behavior t a] -> Behavior t a
mconcat = PullM t a -> Behavior t a
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t a -> Behavior t a)
-> ([Behavior t a] -> PullM t a) -> [Behavior t a] -> Behavior t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([a] -> a) -> PullM t [a] -> PullM t a
forall a b. (a -> b) -> PullM t a -> PullM t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [a] -> a
forall a. Monoid a => [a] -> a
mconcat (PullM t [a] -> PullM t a)
-> ([Behavior t a] -> PullM t [a]) -> [Behavior t a] -> PullM t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Behavior t a -> PullM t a) -> [Behavior t a] -> PullM t [a]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample

instance (Reflex t, Num a) => Num (Behavior t a) where
  + :: Behavior t a -> Behavior t a -> Behavior t a
(+) = (a -> a -> a) -> Behavior t a -> Behavior t a -> Behavior t a
forall a b c.
(a -> b -> c) -> Behavior t a -> Behavior t b -> Behavior t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  (-) = (a -> a -> a) -> Behavior t a -> Behavior t a -> Behavior t a
forall a b c.
(a -> b -> c) -> Behavior t a -> Behavior t b -> Behavior t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)
  * :: Behavior t a -> Behavior t a -> Behavior t a
(*) = (a -> a -> a) -> Behavior t a -> Behavior t a -> Behavior t a
forall a b c.
(a -> b -> c) -> Behavior t a -> Behavior t b -> Behavior t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  abs :: Behavior t a -> Behavior t a
abs = (a -> a) -> Behavior t a -> Behavior t a
forall a b. (a -> b) -> Behavior t a -> Behavior t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
  fromInteger :: Integer -> Behavior t a
fromInteger = a -> Behavior t a
forall a. a -> Behavior t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Behavior t a) -> (Integer -> a) -> Integer -> Behavior t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
  negate :: Behavior t a -> Behavior t a
negate = (a -> a) -> Behavior t a -> Behavior t a
forall a b. (a -> b) -> Behavior t a -> Behavior t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
negate
  signum :: Behavior t a -> Behavior t a
signum = (a -> a) -> Behavior t a -> Behavior t a
forall a b. (a -> b) -> Behavior t a -> Behavior t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum

instance (Num a, Reflex t) => Num (Dynamic t a) where
  + :: Dynamic t a -> Dynamic t a -> Dynamic t a
(+) = (a -> a -> a) -> Dynamic t a -> Dynamic t a -> Dynamic t a
forall a b c.
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(+)
  * :: Dynamic t a -> Dynamic t a -> Dynamic t a
(*) = (a -> a -> a) -> Dynamic t a -> Dynamic t a -> Dynamic t a
forall a b c.
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> a -> a
forall a. Num a => a -> a -> a
(*)
  abs :: Dynamic t a -> Dynamic t a
abs = (a -> a) -> Dynamic t a -> Dynamic t a
forall a b. (a -> b) -> Dynamic t a -> Dynamic t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
abs
  signum :: Dynamic t a -> Dynamic t a
signum = (a -> a) -> Dynamic t a -> Dynamic t a
forall a b. (a -> b) -> Dynamic t a -> Dynamic t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
signum
  fromInteger :: Integer -> Dynamic t a
fromInteger = a -> Dynamic t a
forall a. a -> Dynamic t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Dynamic t a) -> (Integer -> a) -> Integer -> Dynamic t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> a
forall a. Num a => Integer -> a
fromInteger
  negate :: Dynamic t a -> Dynamic t a
negate = (a -> a) -> Dynamic t a -> Dynamic t a
forall a b. (a -> b) -> Dynamic t a -> Dynamic t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> a
forall a. Num a => a -> a
negate
  (-) = (a -> a -> a) -> Dynamic t a -> Dynamic t a -> Dynamic t a
forall a b c.
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (-)

instance (Reflex t, Semigroup a) => Semigroup (Behavior t a) where
  Behavior t a
a <> :: Behavior t a -> Behavior t a -> Behavior t a
<> Behavior t a
b = PullM t a -> Behavior t a
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t a -> Behavior t a) -> PullM t a -> Behavior t a
forall a b. (a -> b) -> a -> b
$ (a -> a -> a) -> PullM t a -> PullM t a -> PullM t a
forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) (Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
a) (Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
b)
  sconcat :: NonEmpty (Behavior t a) -> Behavior t a
sconcat = PullM t a -> Behavior t a
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t a -> Behavior t a)
-> (NonEmpty (Behavior t a) -> PullM t a)
-> NonEmpty (Behavior t a)
-> Behavior t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (NonEmpty a -> a) -> PullM t (NonEmpty a) -> PullM t a
forall a b. (a -> b) -> PullM t a -> PullM t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty a -> a
forall a. Semigroup a => NonEmpty a -> a
sconcat (PullM t (NonEmpty a) -> PullM t a)
-> (NonEmpty (Behavior t a) -> PullM t (NonEmpty a))
-> NonEmpty (Behavior t a)
-> PullM t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Behavior t a -> PullM t a)
-> NonEmpty (Behavior t a) -> PullM t (NonEmpty a)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> NonEmpty a -> m (NonEmpty b)
mapM Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample
  stimes :: forall b. Integral b => b -> Behavior t a -> Behavior t a
stimes b
n = (a -> a) -> Behavior t a -> Behavior t a
forall a b. (a -> b) -> Behavior t a -> Behavior t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Behavior t a -> Behavior t a)
-> (a -> a) -> Behavior t a -> Behavior t a
forall a b. (a -> b) -> a -> b
$ b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n

-- | Alias for 'mapMaybe'
fmapMaybe :: Filterable f => (a -> Maybe b) -> f a -> f b
fmapMaybe :: forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
fmapMaybe = (a -> Maybe b) -> f a -> f b
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe

-- | Flipped version of 'mapMaybe'.
fforMaybe :: Filterable f => f a -> (a -> Maybe b) -> f b
fforMaybe :: forall (f :: * -> *) a b.
Filterable f =>
f a -> (a -> Maybe b) -> f b
fforMaybe = ((a -> Maybe b) -> f a -> f b) -> f a -> (a -> Maybe b) -> f b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> Maybe b) -> f a -> f b
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe

-- | Filter 'f a' using the provided predicate.
ffilter :: Filterable f => (a -> Bool) -> f a -> f a
ffilter :: forall (f :: * -> *) a. Filterable f => (a -> Bool) -> f a -> f a
ffilter = (a -> Bool) -> f a -> f a
forall a. (a -> Bool) -> f a -> f a
forall (f :: * -> *) a. Filterable f => (a -> Bool) -> f a -> f a
W.filter

-- | Filter 'Left's from 'f (Either a b)' into 'a'.
filterLeft :: Filterable f => f (Either a b) -> f a
filterLeft :: forall (f :: * -> *) a b. Filterable f => f (Either a b) -> f a
filterLeft = (Either a b -> Maybe a) -> f (Either a b) -> f a
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe ((a -> Maybe a) -> (b -> Maybe a) -> Either a b -> Maybe a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Maybe a
forall a. a -> Maybe a
Just (Maybe a -> b -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing))

-- | Filter 'Right's from 'f (Either a b)' into 'b'.
filterRight :: Filterable f => f (Either a b) -> f b
filterRight :: forall (f :: * -> *) a b. Filterable f => f (Either a b) -> f b
filterRight = (Either a b -> Maybe b) -> f (Either a b) -> f b
forall a b. (a -> Maybe b) -> f a -> f b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe ((a -> Maybe b) -> (b -> Maybe b) -> Either a b -> Maybe b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe b -> a -> Maybe b
forall a b. a -> b -> a
const Maybe b
forall a. Maybe a
Nothing) b -> Maybe b
forall a. a -> Maybe a
Just)

-- | Left-biased event union (prefers left event on simultaneous
-- occurrence).
instance Reflex t => Alt (Event t) where
  Event t a
ev1 <!> :: forall a. Event t a -> Event t a -> Event t a
<!> Event t a
ev2 = [Event t a] -> Event t a
forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost [Event t a
ev1, Event t a
ev2]

-- | 'Event' intersection. Only occurs when both events are co-incident.
instance Reflex t => Apply (Event t) where
  Event t (a -> b)
evf <.> :: forall a b. Event t (a -> b) -> Event t a -> Event t b
<.> Event t a
evx = (These (a -> b) a -> Maybe b)
-> Event t (These (a -> b) a) -> Event t b
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe These (a -> b) a -> Maybe b
forall {t} {a}. These (t -> a) t -> Maybe a
f (Event t (a -> b) -> Event t a -> Event t (These (a -> b) a)
forall a b. Event t a -> Event t b -> Event t (These a b)
forall (f :: * -> *) a b.
Semialign f =>
f a -> f b -> f (These a b)
align Event t (a -> b)
evf Event t a
evx) where
    f :: These (t -> a) t -> Maybe a
f (These t -> a
g t
a) = a -> Maybe a
forall a. a -> Maybe a
Just (t -> a
g t
a)
    f These (t -> a) t
_           = Maybe a
forall a. Maybe a
Nothing


-- | 'Event' intersection (convenient interface to 'coincidence').
instance Reflex t => Bind (Event t) where
  Event t a
evx >>- :: forall a b. Event t a -> (a -> Event t b) -> Event t b
>>- a -> Event t b
f = Event t (Event t b) -> Event t b
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence (a -> Event t b
f (a -> Event t b) -> Event t a -> Event t (Event t b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t a
evx)
  join :: forall a. Event t (Event t a) -> Event t a
join = Event t (Event t a) -> Event t a
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence

instance Reflex t => Functor (Event t) where
  {-# INLINE fmap #-}
  fmap :: forall a b. (a -> b) -> Event t a -> Event t b
fmap a -> b
f = (a -> Maybe b) -> Event t a -> Event t b
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe ((a -> Maybe b) -> Event t a -> Event t b)
-> (a -> Maybe b) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> (a -> b) -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f
  {-# INLINE (<$) #-}
  a
x <$ :: forall a b. a -> Event t b -> Event t a
<$ Event t b
e = (b -> a) -> Event t b -> Event t a
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (a -> b -> a
forall a b. a -> b -> a
const a
x) Event t b
e

-- TODO Remove this instance
instance Reflex t => FunctorMaybe (Event t) where
  {-# INLINE fmapMaybe #-}
  fmapMaybe :: forall a b. (a -> Maybe b) -> Event t a -> Event t b
fmapMaybe = (a -> Maybe b) -> Event t a -> Event t b
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe

instance Reflex t => Filterable (Event t) where
  {-# INLINE mapMaybe #-}
  mapMaybe :: forall a b. (a -> Maybe b) -> Event t a -> Event t b
mapMaybe a -> Maybe b
f = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push ((a -> PushM t (Maybe b)) -> Event t a -> Event t b)
-> (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ Maybe b -> PushM t (Maybe b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b -> PushM t (Maybe b))
-> (a -> Maybe b) -> a -> PushM t (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe b
f

-- | Never: @'zero' = 'never'@.
instance Reflex t => Plus (Event t) where
  zero :: forall a. Event t a
zero = Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never

-- | Replace each occurrence value of the 'Event' with the value of the
-- 'Behavior' at the time of that occurrence.
tag :: Reflex t => Behavior t b -> Event t a -> Event t b
tag :: forall {k} (t :: k) b a.
Reflex t =>
Behavior t b -> Event t a -> Event t b
tag Behavior t b
b = (a -> PushM t b) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t b) -> Event t a -> Event t b
pushAlways ((a -> PushM t b) -> Event t a -> Event t b)
-> (a -> PushM t b) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ \a
_ -> Behavior t b -> PushM t b
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t b
b

-- | Replace each occurrence value of the 'Event' with the value of the
-- 'Behavior' at that time; if it is 'Just', fire with the contained value; if
-- it is 'Nothing', drop the occurrence.
tagMaybe :: Reflex t => Behavior t (Maybe b) -> Event t a -> Event t b
tagMaybe :: forall {k} (t :: k) b a.
Reflex t =>
Behavior t (Maybe b) -> Event t a -> Event t b
tagMaybe Behavior t (Maybe b)
b = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push ((a -> PushM t (Maybe b)) -> Event t a -> Event t b)
-> (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ \a
_ -> Behavior t (Maybe b) -> PushM t (Maybe b)
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t (Maybe b)
b

-- | Create a new 'Event' that combines occurrences of supplied 'Event' with the
-- current value of the 'Behavior'.
attach :: Reflex t => Behavior t a -> Event t b -> Event t (a, b)
attach :: forall {k} (t :: k) a b.
Reflex t =>
Behavior t a -> Event t b -> Event t (a, b)
attach = (a -> b -> (a, b)) -> Behavior t a -> Event t b -> Event t (a, b)
forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> c) -> Behavior t a -> Event t b -> Event t c
attachWith (,)

-- | Create a new 'Event' that occurs when the supplied 'Event' occurs by
-- combining it with the current value of the 'Behavior'.
attachWith :: Reflex t => (a -> b -> c) -> Behavior t a -> Event t b -> Event t c
attachWith :: forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> c) -> Behavior t a -> Event t b -> Event t c
attachWith a -> b -> c
f = (a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
attachWithMaybe ((a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c)
-> (a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
forall a b. (a -> b) -> a -> b
$ \a
a b
b -> c -> Maybe c
forall a. a -> Maybe a
Just (c -> Maybe c) -> c -> Maybe c
forall a b. (a -> b) -> a -> b
$ a -> b -> c
f a
a b
b

-- | Create a new 'Event' by combining each occurrence with the current value of
-- the 'Behavior'. The occurrence is discarded if the combining function returns
-- Nothing
attachWithMaybe :: Reflex t => (a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
attachWithMaybe :: forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
attachWithMaybe a -> b -> Maybe c
f Behavior t a
b Event t b
e = ((b -> PushM t (Maybe c)) -> Event t b -> Event t c)
-> Event t b -> (b -> PushM t (Maybe c)) -> Event t c
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe c)) -> Event t b -> Event t c
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe c)) -> Event t c)
-> (b -> PushM t (Maybe c)) -> Event t c
forall a b. (a -> b) -> a -> b
$ \b
o -> (a -> b -> Maybe c
`f` b
o) (a -> Maybe c) -> PushM t a -> PushM t (Maybe c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior t a -> PushM t a
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
b

-- | Create a new 'Event' that occurs on all but the first occurrence of the
-- supplied 'Event'.
tailE :: (Reflex t, MonadHold t m) => Event t a -> m (Event t a)
tailE :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> m (Event t a)
tailE Event t a
e = (Event t a, Event t a) -> Event t a
forall a b. (a, b) -> b
snd ((Event t a, Event t a) -> Event t a)
-> m (Event t a, Event t a) -> m (Event t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t a -> m (Event t a, Event t a)
forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> m (Event t a, Event t a)
headTailE Event t a
e

-- | Create a tuple of two 'Event's with the first one occurring only the first
-- time the supplied 'Event' occurs and the second occurring on all but the first
-- occurrence.
headTailE :: (Reflex t, MonadHold t m) => Event t a -> m (Event t a, Event t a)
headTailE :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> m (Event t a, Event t a)
headTailE Event t a
e = do
  Event t a
eHead <- Event t a -> m (Event t a)
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE Event t a
e
  Behavior t (Event t a)
be <- Event t a -> Event t (Event t a) -> m (Behavior t (Event t a))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never (Event t (Event t a) -> m (Behavior t (Event t a)))
-> Event t (Event t a) -> m (Behavior t (Event t a))
forall a b. (a -> b) -> a -> b
$ (a -> Event t a) -> Event t a -> Event t (Event t a)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Event t a -> a -> Event t a
forall a b. a -> b -> a
const Event t a
e) Event t a
eHead
  (Event t a, Event t a) -> m (Event t a, Event t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Event t a
eHead, Behavior t (Event t a) -> Event t a
forall a. Behavior t (Event t a) -> Event t a
forall {k} (t :: k) a.
Reflex t =>
Behavior t (Event t a) -> Event t a
switch Behavior t (Event t a)
be)

-- | Take the streak of occurrences starting at the current time for which the
-- event returns 'True'.
--
-- Starting at the current time, fire all the occurrences of the 'Event' for
-- which the given predicate returns 'True'.  When first 'False' is returned,
-- do not fire, and permanently stop firing, even if 'True' values would have
-- been encountered later.
takeWhileE
  :: forall t m a
  .  (Reflex t, MonadFix m, MonadHold t m)
  => (a -> Bool)
  -> Event t a
  -> m (Event t a)
takeWhileE :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Bool) -> Event t a -> m (Event t a)
takeWhileE a -> Bool
f = (a -> Maybe a) -> Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Maybe b) -> Event t a -> m (Event t b)
takeWhileJustE ((a -> Maybe a) -> Event t a -> m (Event t a))
-> (a -> Maybe a) -> Event t a -> m (Event t a)
forall a b. (a -> b) -> a -> b
$ \a
v -> Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (a -> Bool
f a
v) Maybe () -> a -> Maybe a
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> a
v

-- | Take the streak of occurrences starting at the current time for which the
-- event returns 'Just b'.
--
-- Starting at the current time, fire all the occurrences of the 'Event' for
-- which the given predicate returns 'Just b'.  When first 'Nothing' is returned,
-- do not fire, and permanently stop firing, even if 'Just b' values would have
-- been encountered later.
takeWhileJustE
  :: forall t m a b
  .  (Reflex t, MonadFix m, MonadHold t m)
  => (a -> Maybe b)
  -> Event t a
  -> m (Event t b)
takeWhileJustE :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Maybe b) -> Event t a -> m (Event t b)
takeWhileJustE a -> Maybe b
f Event t a
e = do
  rec let (Event t (Event t a)
eBad, Event t b
eTrue) = Event t (Either (Event t a) b) -> (Event t (Event t a), Event t b)
forall {k} (t :: k) a b.
Reflex t =>
Event t (Either a b) -> (Event t a, Event t b)
fanEither (Event t (Either (Event t a) b)
 -> (Event t (Event t a), Event t b))
-> Event t (Either (Event t a) b)
-> (Event t (Event t a), Event t b)
forall a b. (a -> b) -> a -> b
$ Event t a
-> (a -> Either (Event t a) b) -> Event t (Either (Event t a) b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t a
e' ((a -> Either (Event t a) b) -> Event t (Either (Event t a) b))
-> (a -> Either (Event t a) b) -> Event t (Either (Event t a) b)
forall a b. (a -> b) -> a -> b
$ \a
a -> case a -> Maybe b
f a
a of
            Maybe b
Nothing -> Event t a -> Either (Event t a) b
forall a b. a -> Either a b
Left Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never
            Just b
b  -> b -> Either (Event t a) b
forall a b. b -> Either a b
Right b
b
      Event t (Event t a)
eFirstBad <- Event t (Event t a) -> m (Event t (Event t a))
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE Event t (Event t a)
eBad
      Event t a
e' <- Event t a -> Event t (Event t a) -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHold Event t a
e Event t (Event t a)
eFirstBad
  Event t b -> m (Event t b)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Event t b
eTrue

-- | Drop the streak of occurrences starting at the current time for which the
-- event returns 'True'.
--
-- Starting at the current time, do not fire all the occurrences of the 'Event'
-- for which the given predicate returns 'True'.  When 'False' is first
-- returned, do fire, and permanently continue firing, even if 'True' values
-- would have been encountered later.
dropWhileE
  :: forall t m a
  .  (Reflex t, MonadFix m, MonadHold t m)
  => (a -> Bool)
  -> Event t a
  -> m (Event t a)
dropWhileE :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Bool) -> Event t a -> m (Event t a)
dropWhileE a -> Bool
f Event t a
e = (Event t a, Event t a) -> Event t a
forall a b. (a, b) -> b
snd ((Event t a, Event t a) -> Event t a)
-> m (Event t a, Event t a) -> m (Event t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> Maybe a) -> Event t a -> m (Event t a, Event t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Maybe b) -> Event t a -> m (Event t b, Event t a)
takeDropWhileJustE (\a
v -> Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (a -> Bool
f a
v) Maybe () -> a -> Maybe a
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> a
v) Event t a
e

-- | Both take and drop the streak of occurrences starting at the current time
-- for which the event returns 'Just b'.
--
-- For the left event, starting at the current time, fire all the occurrences
-- of the 'Event' for which the given function returns 'Just b'.  When
-- 'Nothing' is returned, do not fire, and permanently stop firing, even if
-- 'Just b' values would have been encountered later.
--
-- For the right event, do not fire until the first occurrence where the given
-- function returns 'Nothing', and fire that one and all subsequent
-- occurrences. Even if the function would have again returned 'Just b', keep
-- on firing.
takeDropWhileJustE
  :: forall t m a b
  . (Reflex t, MonadFix m, MonadHold t m)
  => (a -> Maybe b)
  -> Event t a
  -> m (Event t b, Event t a)
takeDropWhileJustE :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Maybe b) -> Event t a -> m (Event t b, Event t a)
takeDropWhileJustE a -> Maybe b
f Event t a
e = do
  rec let (Event t ()
eBad, Event t b
eGood) = Event t (Either () b) -> (Event t (), Event t b)
forall {k} (t :: k) a b.
Reflex t =>
Event t (Either a b) -> (Event t a, Event t b)
fanEither (Event t (Either () b) -> (Event t (), Event t b))
-> Event t (Either () b) -> (Event t (), Event t b)
forall a b. (a -> b) -> a -> b
$ Event t a -> (a -> Either () b) -> Event t (Either () b)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t a
e' ((a -> Either () b) -> Event t (Either () b))
-> (a -> Either () b) -> Event t (Either () b)
forall a b. (a -> b) -> a -> b
$ \a
a -> case a -> Maybe b
f a
a of
            Maybe b
Nothing -> () -> Either () b
forall a b. a -> Either a b
Left ()
            Just b
b  -> b -> Either () b
forall a b. b -> Either a b
Right b
b
      Event t ()
eFirstBad <- Event t () -> m (Event t ())
forall a. Event t a -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
Event t a -> m (Event t a)
headE Event t ()
eBad
      Event t a
e' <- Event t a -> Event t (Event t a) -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHold Event t a
e (Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never Event t a -> Event t () -> Event t (Event t a)
forall a b. a -> Event t b -> Event t a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Event t ()
eFirstBad)
  Event t a
eRest <- Event t a -> Event t (Event t a) -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptOnly Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never (Event t a
e Event t a -> Event t () -> Event t (Event t a)
forall a b. a -> Event t b -> Event t a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Event t ()
eFirstBad)
  (Event t b, Event t a) -> m (Event t b, Event t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Event t b
eGood, Event t a
eRest)

-- | Split the supplied 'Event' into two individual 'Event's occurring at the
-- same time with the respective values from the tuple.
splitE :: Reflex t => Event t (a, b) -> (Event t a, Event t b)
splitE :: forall {k} (t :: k) a b.
Reflex t =>
Event t (a, b) -> (Event t a, Event t b)
splitE Event t (a, b)
e = (((a, b) -> a) -> Event t (a, b) -> Event t a
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, b) -> a
forall a b. (a, b) -> a
fst Event t (a, b)
e, ((a, b) -> b) -> Event t (a, b) -> Event t b
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, b) -> b
forall a b. (a, b) -> b
snd Event t (a, b)
e)

-- | Print the supplied 'String' and the value of the 'Event' on each
-- occurrence. This should /only/ be used for debugging.
--
-- Note: As with Debug.Trace.trace, the message will only be printed if the
-- 'Event' is actually used.
traceEvent :: (Reflex t, Show a) => String -> Event t a -> Event t a
traceEvent :: forall {k} (t :: k) a.
(Reflex t, Show a) =>
String -> Event t a -> Event t a
traceEvent String
s = (a -> String) -> Event t a -> Event t a
forall {k} (t :: k) a.
Reflex t =>
(a -> String) -> Event t a -> Event t a
traceEventWith ((a -> String) -> Event t a -> Event t a)
-> (a -> String) -> Event t a -> Event t a
forall a b. (a -> b) -> a -> b
$ \a
x -> String
s String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
": " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
x

-- | Print the output of the supplied function on each occurrence of the
-- 'Event'. This should /only/ be used for debugging.
--
-- Note: As with Debug.Trace.trace, the message will only be printed if the
-- 'Event' is actually used.
traceEventWith :: Reflex t => (a -> String) -> Event t a -> Event t a
traceEventWith :: forall {k} (t :: k) a.
Reflex t =>
(a -> String) -> Event t a -> Event t a
traceEventWith a -> String
f = (a -> PushM t (Maybe a)) -> Event t a -> Event t a
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push ((a -> PushM t (Maybe a)) -> Event t a -> Event t a)
-> (a -> PushM t (Maybe a)) -> Event t a -> Event t a
forall a b. (a -> b) -> a -> b
$ \a
x -> String -> PushM t (Maybe a) -> PushM t (Maybe a)
forall a. String -> a -> a
trace (a -> String
f a
x) (PushM t (Maybe a) -> PushM t (Maybe a))
-> PushM t (Maybe a) -> PushM t (Maybe a)
forall a b. (a -> b) -> a -> b
$ Maybe a -> PushM t (Maybe a)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> PushM t (Maybe a)) -> Maybe a -> PushM t (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> Maybe a
forall a. a -> Maybe a
Just a
x

instance (Semigroup a, Reflex t) => Semigroup (Event t a) where
  <> :: Event t a -> Event t a -> Event t a
(<>) = (These a a -> a) -> Event t a -> Event t a -> Event t a
forall a b c.
(These a b -> c) -> Event t a -> Event t b -> Event t c
forall (f :: * -> *) a b c.
Semialign f =>
(These a b -> c) -> f a -> f b -> f c
alignWith ((a -> a -> a) -> These a a -> a
forall a. (a -> a -> a) -> These a a -> a
mergeThese a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>))
  sconcat :: NonEmpty (Event t a) -> Event t a
sconcat = (NonEmpty a -> a) -> Event t (NonEmpty a) -> Event t a
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty a -> a
forall a. Semigroup a => NonEmpty a -> a
sconcat (Event t (NonEmpty a) -> Event t a)
-> (NonEmpty (Event t a) -> Event t (NonEmpty a))
-> NonEmpty (Event t a)
-> Event t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Event t a] -> Event t (NonEmpty a)
forall {k} (t :: k) a.
Reflex t =>
[Event t a] -> Event t (NonEmpty a)
mergeList ([Event t a] -> Event t (NonEmpty a))
-> (NonEmpty (Event t a) -> [Event t a])
-> NonEmpty (Event t a)
-> Event t (NonEmpty a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Event t a) -> [Event t a]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
  stimes :: forall b. Integral b => b -> Event t a -> Event t a
stimes b
n = (a -> a) -> Event t a -> Event t a
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Event t a -> Event t a)
-> (a -> a) -> Event t a -> Event t a
forall a b. (a -> b) -> a -> b
$ b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n

instance (Semigroup a, Reflex t) => Monoid (Event t a) where
  mempty :: Event t a
mempty = Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never
  mappend :: Event t a -> Event t a -> Event t a
mappend = Event t a -> Event t a -> Event t a
forall a. Semigroup a => a -> a -> a
(<>)
  mconcat :: [Event t a] -> Event t a
mconcat = (NonEmpty a -> a) -> Event t (NonEmpty a) -> Event t a
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap NonEmpty a -> a
forall a. Semigroup a => NonEmpty a -> a
sconcat (Event t (NonEmpty a) -> Event t a)
-> ([Event t a] -> Event t (NonEmpty a))
-> [Event t a]
-> Event t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Event t a] -> Event t (NonEmpty a)
forall {k} (t :: k) a.
Reflex t =>
[Event t a] -> Event t (NonEmpty a)
mergeList

-- | Create a new 'Event' that occurs if at least one of the 'Event's in the
-- list occurs. If multiple occur at the same time they are folded from the left
-- with the given function.
{-# INLINE mergeWith #-}
mergeWith :: Reflex t => (a -> a -> a) -> [Event t a] -> Event t a
mergeWith :: forall {k} (t :: k) a.
Reflex t =>
(a -> a -> a) -> [Event t a] -> Event t a
mergeWith = (a -> a) -> (a -> a -> a) -> [Event t a] -> Event t a
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
mergeWith' a -> a
forall a. a -> a
id

{-# INLINE mergeWith' #-}
mergeWith' :: Reflex t => (a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
mergeWith' :: forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
mergeWith' a -> b
f b -> b -> b
g [Event t a]
es = (IntMap a -> b) -> Event t (IntMap a) -> Event t b
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((b -> b -> b) -> IntMap b -> b
forall a. (a -> a -> a) -> IntMap a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Prelude.foldl1 b -> b -> b
g (IntMap b -> b) -> (IntMap a -> IntMap b) -> IntMap a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> IntMap a -> IntMap b
forall a b. (a -> b) -> IntMap a -> IntMap b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)
                  (Event t (IntMap a) -> Event t b)
-> ([(Int, Event t a)] -> Event t (IntMap a))
-> [(Int, Event t a)]
-> Event t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap (Event t a) -> Event t (IntMap a)
forall {k} (t :: k) a.
Reflex t =>
IntMap (Event t a) -> Event t (IntMap a)
mergeInt
                  (IntMap (Event t a) -> Event t (IntMap a))
-> ([(Int, Event t a)] -> IntMap (Event t a))
-> [(Int, Event t a)]
-> Event t (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, Event t a)] -> IntMap (Event t a)
forall a. [(Int, a)] -> IntMap a
IntMap.fromDistinctAscList
                  ([(Int, Event t a)] -> Event t b)
-> [(Int, Event t a)] -> Event t b
forall a b. (a -> b) -> a -> b
$ [Int] -> [Event t a] -> [(Int, Event t a)]
forall a b. [a] -> [b] -> [(a, b)]
forall (f :: * -> *) a b. Zip f => f a -> f b -> f (a, b)
zip [Int
0 :: Int ..] [Event t a]
es

-- | Create a new 'Event' that occurs if at least one of the 'Event's in the
-- list occurs. If multiple occur at the same time the value is the value of the
-- leftmost event.
{-# INLINE leftmost #-}
leftmost :: Reflex t => [Event t a] -> Event t a
leftmost :: forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost = (a -> a -> a) -> [Event t a] -> Event t a
forall {k} (t :: k) a.
Reflex t =>
(a -> a -> a) -> [Event t a] -> Event t a
mergeWith a -> a -> a
forall a b. a -> b -> a
const

-- | Create a new 'Event' that occurs if at least one of the 'Event's in the
-- list occurs and has a list of the values of all 'Event's occurring at that
-- time.
mergeList :: Reflex t => [Event t a] -> Event t (NonEmpty a)
mergeList :: forall {k} (t :: k) a.
Reflex t =>
[Event t a] -> Event t (NonEmpty a)
mergeList [] = Event t (NonEmpty a)
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never
mergeList [Event t a]
es = (NonEmpty a -> NonEmpty a) -> [Event t a] -> Event t (NonEmpty a)
forall {k} (t :: k) a b.
Reflex t =>
(NonEmpty a -> b) -> [Event t a] -> Event t b
mergeWithFoldCheap' NonEmpty a -> NonEmpty a
forall a. a -> a
id [Event t a]
es

unsafeMapIncremental
  :: (Reflex t, Patch p, Patch p')
  => (PatchTarget p -> PatchTarget p')
  -> (p -> p')
  -> Incremental t p
  -> Incremental t p'
unsafeMapIncremental :: forall {k} (t :: k) p p'.
(Reflex t, Patch p, Patch p') =>
(PatchTarget p -> PatchTarget p')
-> (p -> p') -> Incremental t p -> Incremental t p'
unsafeMapIncremental PatchTarget p -> PatchTarget p'
f p -> p'
g Incremental t p
a = PullM t (PatchTarget p') -> Event t p' -> Incremental t p'
forall p.
Patch p =>
PullM t (PatchTarget p) -> Event t p -> Incremental t p
forall {k} (t :: k) p.
(Reflex t, Patch p) =>
PullM t (PatchTarget p) -> Event t p -> Incremental t p
unsafeBuildIncremental ((PatchTarget p -> PatchTarget p')
-> PullM t (PatchTarget p) -> PullM t (PatchTarget p')
forall a b. (a -> b) -> PullM t a -> PullM t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PatchTarget p -> PatchTarget p'
f (PullM t (PatchTarget p) -> PullM t (PatchTarget p'))
-> PullM t (PatchTarget p) -> PullM t (PatchTarget p')
forall a b. (a -> b) -> a -> b
$ Behavior t (PatchTarget p) -> PullM t (PatchTarget p)
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (PatchTarget p) -> PullM t (PatchTarget p))
-> Behavior t (PatchTarget p) -> PullM t (PatchTarget p)
forall a b. (a -> b) -> a -> b
$ Incremental t p -> Behavior t (PatchTarget p)
forall p. Patch p => Incremental t p -> Behavior t (PatchTarget p)
forall {k} (t :: k) p.
(Reflex t, Patch p) =>
Incremental t p -> Behavior t (PatchTarget p)
currentIncremental Incremental t p
a) (Event t p' -> Incremental t p') -> Event t p' -> Incremental t p'
forall a b. (a -> b) -> a -> b
$ p -> p'
g (p -> p') -> Event t p -> Event t p'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Incremental t p -> Event t p
forall p. Patch p => Incremental t p -> Event t p
forall {k} (t :: k) p.
(Reflex t, Patch p) =>
Incremental t p -> Event t p
updatedIncremental Incremental t p
a

-- | Create a new 'Event' combining the map of 'Event's into an 'Event' that
-- occurs if at least one of them occurs and has a map of values of all 'Event's
-- occurring at that time.
mergeMap :: (Reflex t, Ord k) => Map k (Event t a) -> Event t (Map k a)
mergeMap :: forall {k} (t :: k) k a.
(Reflex t, Ord k) =>
Map k (Event t a) -> Event t (Map k a)
mergeMap = (DMap (Const2 k a) Identity -> Map k a)
-> Event t (DMap (Const2 k a) Identity) -> Event t (Map k a)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DMap (Const2 k a) Identity -> Map k a
forall k v. DMap (Const2 k v) Identity -> Map k v
dmapToMap (Event t (DMap (Const2 k a) Identity) -> Event t (Map k a))
-> (Map k (Event t a) -> Event t (DMap (Const2 k a) Identity))
-> Map k (Event t a)
-> Event t (Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap (Const2 k a) (Event t) -> Event t (DMap (Const2 k a) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
DMap k (Event t) -> Event t (DMap k Identity)
merge (DMap (Const2 k a) (Event t)
 -> Event t (DMap (Const2 k a) Identity))
-> (Map k (Event t a) -> DMap (Const2 k a) (Event t))
-> Map k (Event t a)
-> Event t (DMap (Const2 k a) Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map k (Event t a) -> DMap (Const2 k a) (Event t)
forall {k1} k2 (f :: k1 -> *) (v :: k1).
Map k2 (f v) -> DMap (Const2 k2 v) f
mapWithFunctorToDMap

-- | Like 'mergeMap' but for 'IntMap'.
mergeIntMap :: Reflex t => IntMap (Event t a) -> Event t (IntMap a)
mergeIntMap :: forall {k} (t :: k) a.
Reflex t =>
IntMap (Event t a) -> Event t (IntMap a)
mergeIntMap = (DMap (Const2 Int a) Identity -> IntMap a)
-> Event t (DMap (Const2 Int a) Identity) -> Event t (IntMap a)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DMap (Const2 Int a) Identity -> IntMap a
forall v. DMap (Const2 Int v) Identity -> IntMap v
dmapToIntMap (Event t (DMap (Const2 Int a) Identity) -> Event t (IntMap a))
-> (IntMap (Event t a) -> Event t (DMap (Const2 Int a) Identity))
-> IntMap (Event t a)
-> Event t (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap (Const2 Int a) (Event t)
-> Event t (DMap (Const2 Int a) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
DMap k (Event t) -> Event t (DMap k Identity)
merge (DMap (Const2 Int a) (Event t)
 -> Event t (DMap (Const2 Int a) Identity))
-> (IntMap (Event t a) -> DMap (Const2 Int a) (Event t))
-> IntMap (Event t a)
-> Event t (DMap (Const2 Int a) Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap (Event t a) -> DMap (Const2 Int a) (Event t)
forall {k} (f :: k -> *) (v :: k).
IntMap (f v) -> DMap (Const2 Int v) f
intMapWithFunctorToDMap

-- | Create a merge whose parents can change over time
mergeMapIncremental :: (Reflex t, Ord k) => Incremental t (PatchMap k (Event t a)) -> Event t (Map k a)
mergeMapIncremental :: forall {k} (t :: k) k a.
(Reflex t, Ord k) =>
Incremental t (PatchMap k (Event t a)) -> Event t (Map k a)
mergeMapIncremental = (DMap (Const2 k a) Identity -> Map k a)
-> Event t (DMap (Const2 k a) Identity) -> Event t (Map k a)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DMap (Const2 k a) Identity -> Map k a
forall k v. DMap (Const2 k v) Identity -> Map k v
dmapToMap (Event t (DMap (Const2 k a) Identity) -> Event t (Map k a))
-> (Incremental t (PatchMap k (Event t a))
    -> Event t (DMap (Const2 k a) Identity))
-> Incremental t (PatchMap k (Event t a))
-> Event t (Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Incremental t (PatchDMap (Const2 k a) (Event t))
-> Event t (DMap (Const2 k a) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Incremental t (PatchDMap k (Event t)) -> Event t (DMap k Identity)
mergeIncremental (Incremental t (PatchDMap (Const2 k a) (Event t))
 -> Event t (DMap (Const2 k a) Identity))
-> (Incremental t (PatchMap k (Event t a))
    -> Incremental t (PatchDMap (Const2 k a) (Event t)))
-> Incremental t (PatchMap k (Event t a))
-> Event t (DMap (Const2 k a) Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PatchTarget (PatchMap k (Event t a))
 -> PatchTarget (PatchDMap (Const2 k a) (Event t)))
-> (PatchMap k (Event t a) -> PatchDMap (Const2 k a) (Event t))
-> Incremental t (PatchMap k (Event t a))
-> Incremental t (PatchDMap (Const2 k a) (Event t))
forall {k} (t :: k) p p'.
(Reflex t, Patch p, Patch p') =>
(PatchTarget p -> PatchTarget p')
-> (p -> p') -> Incremental t p -> Incremental t p'
unsafeMapIncremental Map k (Event t a) -> DMap (Const2 k a) (Event t)
PatchTarget (PatchMap k (Event t a))
-> PatchTarget (PatchDMap (Const2 k a) (Event t))
forall {k1} k2 (f :: k1 -> *) (v :: k1).
Map k2 (f v) -> DMap (Const2 k2 v) f
mapWithFunctorToDMap ((Event t a -> Event t a)
-> PatchMap k (Event t a) -> PatchDMap (Const2 k a) (Event t)
forall {k1} k2 v (v' :: k1 -> *) (a :: k1).
(v -> v' a) -> PatchMap k2 v -> PatchDMap (Const2 k2 a) v'
const2PatchDMapWith Event t a -> Event t a
forall a. a -> a
id)

-- | Create a merge whose parents can change over time
mergeIntMapIncremental :: Reflex t => Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
mergeIntMapIncremental :: forall {k} (t :: k) a.
Reflex t =>
Incremental t (PatchIntMap (Event t a)) -> Event t (IntMap a)
mergeIntMapIncremental = (DMap (Const2 Int a) Identity -> IntMap a)
-> Event t (DMap (Const2 Int a) Identity) -> Event t (IntMap a)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DMap (Const2 Int a) Identity -> IntMap a
forall v. DMap (Const2 Int v) Identity -> IntMap v
dmapToIntMap (Event t (DMap (Const2 Int a) Identity) -> Event t (IntMap a))
-> (Incremental t (PatchIntMap (Event t a))
    -> Event t (DMap (Const2 Int a) Identity))
-> Incremental t (PatchIntMap (Event t a))
-> Event t (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Incremental t (PatchDMap (Const2 Int a) (Event t))
-> Event t (DMap (Const2 Int a) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Incremental t (PatchDMap k (Event t)) -> Event t (DMap k Identity)
mergeIncremental (Incremental t (PatchDMap (Const2 Int a) (Event t))
 -> Event t (DMap (Const2 Int a) Identity))
-> (Incremental t (PatchIntMap (Event t a))
    -> Incremental t (PatchDMap (Const2 Int a) (Event t)))
-> Incremental t (PatchIntMap (Event t a))
-> Event t (DMap (Const2 Int a) Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PatchTarget (PatchIntMap (Event t a))
 -> PatchTarget (PatchDMap (Const2 Int a) (Event t)))
-> (PatchIntMap (Event t a) -> PatchDMap (Const2 Int a) (Event t))
-> Incremental t (PatchIntMap (Event t a))
-> Incremental t (PatchDMap (Const2 Int a) (Event t))
forall {k} (t :: k) p p'.
(Reflex t, Patch p, Patch p') =>
(PatchTarget p -> PatchTarget p')
-> (p -> p') -> Incremental t p -> Incremental t p'
unsafeMapIncremental IntMap (Event t a) -> DMap (Const2 Int a) (Event t)
PatchTarget (PatchIntMap (Event t a))
-> PatchTarget (PatchDMap (Const2 Int a) (Event t))
forall {k} (f :: k -> *) (v :: k).
IntMap (f v) -> DMap (Const2 Int v) f
intMapWithFunctorToDMap ((Event t a -> Event t a)
-> PatchIntMap (Event t a) -> PatchDMap (Const2 Int a) (Event t)
forall {k} v (f :: k -> *) (a :: k).
(v -> f a) -> PatchIntMap v -> PatchDMap (Const2 Int a) f
const2IntPatchDMapWith Event t a -> Event t a
forall a. a -> a
id)

-- | Experimental: Create a merge whose parents can change over time; changing the key of an Event is more efficient than with mergeIncremental
mergeMapIncrementalWithMove :: (Reflex t, Ord k) => Incremental t (PatchMapWithMove k (Event t a)) -> Event t (Map k a)
mergeMapIncrementalWithMove :: forall {k} (t :: k) k a.
(Reflex t, Ord k) =>
Incremental t (PatchMapWithMove k (Event t a)) -> Event t (Map k a)
mergeMapIncrementalWithMove = (DMap (Const2 k a) Identity -> Map k a)
-> Event t (DMap (Const2 k a) Identity) -> Event t (Map k a)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap DMap (Const2 k a) Identity -> Map k a
forall k v. DMap (Const2 k v) Identity -> Map k v
dmapToMap (Event t (DMap (Const2 k a) Identity) -> Event t (Map k a))
-> (Incremental t (PatchMapWithMove k (Event t a))
    -> Event t (DMap (Const2 k a) Identity))
-> Incremental t (PatchMapWithMove k (Event t a))
-> Event t (Map k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Incremental t (PatchDMapWithMove (Const2 k a) (Event t))
-> Event t (DMap (Const2 k a) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Incremental t (PatchDMapWithMove k (Event t))
-> Event t (DMap k Identity)
mergeIncrementalWithMove (Incremental t (PatchDMapWithMove (Const2 k a) (Event t))
 -> Event t (DMap (Const2 k a) Identity))
-> (Incremental t (PatchMapWithMove k (Event t a))
    -> Incremental t (PatchDMapWithMove (Const2 k a) (Event t)))
-> Incremental t (PatchMapWithMove k (Event t a))
-> Event t (DMap (Const2 k a) Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PatchTarget (PatchMapWithMove k (Event t a))
 -> PatchTarget (PatchDMapWithMove (Const2 k a) (Event t)))
-> (PatchMapWithMove k (Event t a)
    -> PatchDMapWithMove (Const2 k a) (Event t))
-> Incremental t (PatchMapWithMove k (Event t a))
-> Incremental t (PatchDMapWithMove (Const2 k a) (Event t))
forall {k} (t :: k) p p'.
(Reflex t, Patch p, Patch p') =>
(PatchTarget p -> PatchTarget p')
-> (p -> p') -> Incremental t p -> Incremental t p'
unsafeMapIncremental Map k (Event t a) -> DMap (Const2 k a) (Event t)
PatchTarget (PatchMapWithMove k (Event t a))
-> PatchTarget (PatchDMapWithMove (Const2 k a) (Event t))
forall {k1} k2 (f :: k1 -> *) (v :: k1).
Map k2 (f v) -> DMap (Const2 k2 v) f
mapWithFunctorToDMap ((Event t a -> Event t a)
-> PatchMapWithMove k (Event t a)
-> PatchDMapWithMove (Const2 k a) (Event t)
forall {k1} k2 v (v' :: k1 -> *) (a :: k1).
(v -> v' a)
-> PatchMapWithMove k2 v -> PatchDMapWithMove (Const2 k2 a) v'
const2PatchDMapWithMoveWith Event t a -> Event t a
forall a. a -> a
id)

-- | Split the event into separate events for 'Left' and 'Right' values.
fanEither :: Reflex t => Event t (Either a b) -> (Event t a, Event t b)
fanEither :: forall {k} (t :: k) a b.
Reflex t =>
Event t (Either a b) -> (Event t a, Event t b)
fanEither Event t (Either a b)
e =
  let justLeft :: Either a b -> Maybe a
justLeft = (a -> Maybe a) -> (b -> Maybe a) -> Either a b -> Maybe a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either a -> Maybe a
forall a. a -> Maybe a
Just (Maybe a -> b -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing)
      justRight :: Either a a -> Maybe a
justRight = (a -> Maybe a) -> (a -> Maybe a) -> Either a a -> Maybe a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe a -> a -> Maybe a
forall a b. a -> b -> a
const Maybe a
forall a. Maybe a
Nothing) a -> Maybe a
forall a. a -> Maybe a
Just
  in ((Either a b -> Maybe a) -> Event t (Either a b) -> Event t a
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe Either a b -> Maybe a
forall {a} {b}. Either a b -> Maybe a
justLeft Event t (Either a b)
e, (Either a b -> Maybe b) -> Event t (Either a b) -> Event t b
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe Either a b -> Maybe b
forall {a} {a}. Either a a -> Maybe a
justRight Event t (Either a b)
e)

-- | Split the event into separate events for 'This' and 'That' values,
-- allowing them to fire simultaneously when the input value is 'These'.
fanThese :: Reflex t => Event t (These a b) -> (Event t a, Event t b)
fanThese :: forall {k} (t :: k) a b.
Reflex t =>
Event t (These a b) -> (Event t a, Event t b)
fanThese Event t (These a b)
e =
  let this :: These a b -> Maybe a
this (This a
x) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
      this (These a
x b
_) = a -> Maybe a
forall a. a -> Maybe a
Just a
x
      this These a b
_ = Maybe a
forall a. Maybe a
Nothing
      that :: These a a -> Maybe a
that (That a
y) = a -> Maybe a
forall a. a -> Maybe a
Just a
y
      that (These a
_ a
y) = a -> Maybe a
forall a. a -> Maybe a
Just a
y
      that These a a
_ = Maybe a
forall a. Maybe a
Nothing
  in ((These a b -> Maybe a) -> Event t (These a b) -> Event t a
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe These a b -> Maybe a
forall {a} {b}. These a b -> Maybe a
this Event t (These a b)
e, (These a b -> Maybe b) -> Event t (These a b) -> Event t b
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe These a b -> Maybe b
forall {a} {a}. These a a -> Maybe a
that Event t (These a b)
e)

-- | Split the event into an 'EventSelector' that allows efficient selection of
-- the individual 'Event's.
fanMap :: (Reflex t, Ord k) => Event t (Map k a) -> EventSelector t (Const2 k a)
fanMap :: forall {k} (t :: k) k a.
(Reflex t, Ord k) =>
Event t (Map k a) -> EventSelector t (Const2 k a)
fanMap = Event t (DMap (Const2 k a) Identity)
-> EventSelector t (Const2 k a)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Event t (DMap k Identity) -> EventSelector t k
fan (Event t (DMap (Const2 k a) Identity)
 -> EventSelector t (Const2 k a))
-> (Event t (Map k a) -> Event t (DMap (Const2 k a) Identity))
-> Event t (Map k a)
-> EventSelector t (Const2 k a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Map k a -> DMap (Const2 k a) Identity)
-> Event t (Map k a) -> Event t (DMap (Const2 k a) Identity)
forall a b. (a -> b) -> Event t a -> Event t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Map k a -> DMap (Const2 k a) Identity
forall k v. Map k v -> DMap (Const2 k v) Identity
mapToDMap

-- | Switches to the new event whenever it receives one. Only the old event is
-- considered the moment a new one is switched in; the output event will fire at
-- that moment only if the old event does.
--
-- Because the simultaneous firing case is irrelevant, this function imposes
-- laxer "timing requirements" on the overall circuit, avoiding many potential
-- cyclic dependency / metastability failures. It's also more performant. Use
-- this rather than 'switchHoldPromptly' and 'switchHoldPromptOnly' unless you
-- are absolutely sure you need to act on the new event in the coincidental
-- case.
switchHold :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
switchHold :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHold Event t a
ea0 Event t (Event t a)
eea = Behavior t (Event t a) -> Event t a
forall a. Behavior t (Event t a) -> Event t a
forall {k} (t :: k) a.
Reflex t =>
Behavior t (Event t a) -> Event t a
switch (Behavior t (Event t a) -> Event t a)
-> m (Behavior t (Event t a)) -> m (Event t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t a -> Event t (Event t a) -> m (Behavior t (Event t a))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold Event t a
ea0 Event t (Event t a)
eea

-- | Switches to the new event whenever it receives one. Whenever a new event is
-- provided, if it is firing, its value will be the resulting event's value; if
-- it is not firing, but the old one is, the old one's value will be used.
--
-- 'switchHold', by always forwarding the old event the moment it is switched
-- out, avoids many potential cyclic dependency problems / metastability
-- problems. It's also more performant. Use it instead unless you are sure you
-- cannot.
switchHoldPromptly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptly :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptly Event t a
ea0 Event t (Event t a)
eea = do
  Behavior t (Event t a)
bea <- Event t a -> Event t (Event t a) -> m (Behavior t (Event t a))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold Event t a
ea0 Event t (Event t a)
eea
  let eLag :: Event t a
eLag = Behavior t (Event t a) -> Event t a
forall a. Behavior t (Event t a) -> Event t a
forall {k} (t :: k) a.
Reflex t =>
Behavior t (Event t a) -> Event t a
switch Behavior t (Event t a)
bea
      eCoincidences :: Event t a
eCoincidences = Event t (Event t a) -> Event t a
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence Event t (Event t a)
eea
  Event t a -> m (Event t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Event t a -> m (Event t a)) -> Event t a -> m (Event t a)
forall a b. (a -> b) -> a -> b
$ [Event t a] -> Event t a
forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost [Event t a
eCoincidences, Event t a
eLag]

-- | switches to a new event whenever it receives one.  At the moment of
-- switching, the old event will be ignored if it fires, and the new one will be
-- used if it fires; this is the opposite of 'switch', which will use only the
-- old value.
--
-- 'switchHold', by always forwarding the old event the moment it is switched
-- out, avoids many potential cyclic dependency problems / metastability
-- problems. It's also more performant. Use it instead unless you are sure you
-- cannot.
switchHoldPromptOnly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptOnly :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptOnly Event t a
e0 Event t (Event t a)
e' = do
  Event t a
eLag <- Behavior t (Event t a) -> Event t a
forall a. Behavior t (Event t a) -> Event t a
forall {k} (t :: k) a.
Reflex t =>
Behavior t (Event t a) -> Event t a
switch (Behavior t (Event t a) -> Event t a)
-> m (Behavior t (Event t a)) -> m (Event t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event t a -> Event t (Event t a) -> m (Behavior t (Event t a))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold Event t a
e0 Event t (Event t a)
e'
  Event t a -> m (Event t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Event t a -> m (Event t a)) -> Event t a -> m (Event t a)
forall a b. (a -> b) -> a -> b
$ Event t (Event t a) -> Event t a
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence (Event t (Event t a) -> Event t a)
-> Event t (Event t a) -> Event t a
forall a b. (a -> b) -> a -> b
$ [Event t (Event t a)] -> Event t (Event t a)
forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost [Event t (Event t a)
e', Event t a
eLag Event t a -> Event t a -> Event t (Event t a)
forall a b. a -> Event t b -> Event t a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Event t a
eLag]

-- | When the given outer event fires, condense the inner events into the contained patch.  Non-firing inner events will be replaced with deletions.
coincidencePatchMap :: (Reflex t, Ord k) => Event t (PatchMap k (Event t v)) -> Event t (PatchMap k v)
coincidencePatchMap :: forall {k} (t :: k) k v.
(Reflex t, Ord k) =>
Event t (PatchMap k (Event t v)) -> Event t (PatchMap k v)
coincidencePatchMap Event t (PatchMap k (Event t v))
e = (Map k (Maybe v) -> PatchMap k v)
-> Event t (Map k (Maybe v)) -> Event t (PatchMap k v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap Map k (Maybe v) -> PatchMap k v
forall k v. Map k (Maybe v) -> PatchMap k v
PatchMap (Event t (Map k (Maybe v)) -> Event t (PatchMap k v))
-> Event t (Map k (Maybe v)) -> Event t (PatchMap k v)
forall a b. (a -> b) -> a -> b
$ Event t (Event t (Map k (Maybe v))) -> Event t (Map k (Maybe v))
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence (Event t (Event t (Map k (Maybe v))) -> Event t (Map k (Maybe v)))
-> Event t (Event t (Map k (Maybe v))) -> Event t (Map k (Maybe v))
forall a b. (a -> b) -> a -> b
$ Event t (PatchMap k (Event t v))
-> (PatchMap k (Event t v) -> Event t (Map k (Maybe v)))
-> Event t (Event t (Map k (Maybe v)))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t (PatchMap k (Event t v))
e ((PatchMap k (Event t v) -> Event t (Map k (Maybe v)))
 -> Event t (Event t (Map k (Maybe v))))
-> (PatchMap k (Event t v) -> Event t (Map k (Maybe v)))
-> Event t (Event t (Map k (Maybe v)))
forall a b. (a -> b) -> a -> b
$ \(PatchMap Map k (Maybe (Event t v))
m) -> Map k (Event t (Maybe v)) -> Event t (Map k (Maybe v))
forall {k} (t :: k) k a.
(Reflex t, Ord k) =>
Map k (Event t a) -> Event t (Map k a)
mergeMap (Map k (Event t (Maybe v)) -> Event t (Map k (Maybe v)))
-> Map k (Event t (Maybe v)) -> Event t (Map k (Maybe v))
forall a b. (a -> b) -> a -> b
$ Map k (Maybe (Event t v))
-> (Maybe (Event t v) -> Event t (Maybe v))
-> Map k (Event t (Maybe v))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Map k (Maybe (Event t v))
m ((Maybe (Event t v) -> Event t (Maybe v))
 -> Map k (Event t (Maybe v)))
-> (Maybe (Event t v) -> Event t (Maybe v))
-> Map k (Event t (Maybe v))
forall a b. (a -> b) -> a -> b
$ \case
  Maybe (Event t v)
Nothing -> (PatchMap k (Event t v) -> Maybe v)
-> Event t (PatchMap k (Event t v)) -> Event t (Maybe v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (Maybe v -> PatchMap k (Event t v) -> Maybe v
forall a b. a -> b -> a
const Maybe v
forall a. Maybe a
Nothing) Event t (PatchMap k (Event t v))
e
  Just Event t v
ev -> [Event t (Maybe v)] -> Event t (Maybe v)
forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost [(v -> Maybe v) -> Event t v -> Event t (Maybe v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap v -> Maybe v
forall a. a -> Maybe a
Just Event t v
ev, (PatchMap k (Event t v) -> Maybe v)
-> Event t (PatchMap k (Event t v)) -> Event t (Maybe v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (Maybe v -> PatchMap k (Event t v) -> Maybe v
forall a b. a -> b -> a
const Maybe v
forall a. Maybe a
Nothing) Event t (PatchMap k (Event t v))
e]

-- | See 'coincidencePatchMap'
coincidencePatchIntMap :: Reflex t => Event t (PatchIntMap (Event t v)) -> Event t (PatchIntMap v)
coincidencePatchIntMap :: forall {k} (t :: k) v.
Reflex t =>
Event t (PatchIntMap (Event t v)) -> Event t (PatchIntMap v)
coincidencePatchIntMap Event t (PatchIntMap (Event t v))
e = (IntMap (Maybe v) -> PatchIntMap v)
-> Event t (IntMap (Maybe v)) -> Event t (PatchIntMap v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap IntMap (Maybe v) -> PatchIntMap v
forall a. IntMap (Maybe a) -> PatchIntMap a
PatchIntMap (Event t (IntMap (Maybe v)) -> Event t (PatchIntMap v))
-> Event t (IntMap (Maybe v)) -> Event t (PatchIntMap v)
forall a b. (a -> b) -> a -> b
$ Event t (Event t (IntMap (Maybe v))) -> Event t (IntMap (Maybe v))
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence (Event t (Event t (IntMap (Maybe v)))
 -> Event t (IntMap (Maybe v)))
-> Event t (Event t (IntMap (Maybe v)))
-> Event t (IntMap (Maybe v))
forall a b. (a -> b) -> a -> b
$ Event t (PatchIntMap (Event t v))
-> (PatchIntMap (Event t v) -> Event t (IntMap (Maybe v)))
-> Event t (Event t (IntMap (Maybe v)))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t (PatchIntMap (Event t v))
e ((PatchIntMap (Event t v) -> Event t (IntMap (Maybe v)))
 -> Event t (Event t (IntMap (Maybe v))))
-> (PatchIntMap (Event t v) -> Event t (IntMap (Maybe v)))
-> Event t (Event t (IntMap (Maybe v)))
forall a b. (a -> b) -> a -> b
$ \(PatchIntMap IntMap (Maybe (Event t v))
m) -> IntMap (Event t (Maybe v)) -> Event t (IntMap (Maybe v))
forall {k} (t :: k) a.
Reflex t =>
IntMap (Event t a) -> Event t (IntMap a)
mergeIntMap (IntMap (Event t (Maybe v)) -> Event t (IntMap (Maybe v)))
-> IntMap (Event t (Maybe v)) -> Event t (IntMap (Maybe v))
forall a b. (a -> b) -> a -> b
$ IntMap (Maybe (Event t v))
-> (Maybe (Event t v) -> Event t (Maybe v))
-> IntMap (Event t (Maybe v))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor IntMap (Maybe (Event t v))
m ((Maybe (Event t v) -> Event t (Maybe v))
 -> IntMap (Event t (Maybe v)))
-> (Maybe (Event t v) -> Event t (Maybe v))
-> IntMap (Event t (Maybe v))
forall a b. (a -> b) -> a -> b
$ \case
  Maybe (Event t v)
Nothing -> (PatchIntMap (Event t v) -> Maybe v)
-> Event t (PatchIntMap (Event t v)) -> Event t (Maybe v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (Maybe v -> PatchIntMap (Event t v) -> Maybe v
forall a b. a -> b -> a
const Maybe v
forall a. Maybe a
Nothing) Event t (PatchIntMap (Event t v))
e
  Just Event t v
ev -> [Event t (Maybe v)] -> Event t (Maybe v)
forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost [(v -> Maybe v) -> Event t v -> Event t (Maybe v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap v -> Maybe v
forall a. a -> Maybe a
Just Event t v
ev, (PatchIntMap (Event t v) -> Maybe v)
-> Event t (PatchIntMap (Event t v)) -> Event t (Maybe v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (Maybe v -> PatchIntMap (Event t v) -> Maybe v
forall a b. a -> b -> a
const Maybe v
forall a. Maybe a
Nothing) Event t (PatchIntMap (Event t v))
e]

-- | See 'coincidencePatchMap'
coincidencePatchMapWithMove :: (Reflex t, Ord k) => Event t (PatchMapWithMove k (Event t v)) -> Event t (PatchMapWithMove k v)
coincidencePatchMapWithMove :: forall {k} (t :: k) k v.
(Reflex t, Ord k) =>
Event t (PatchMapWithMove k (Event t v))
-> Event t (PatchMapWithMove k v)
coincidencePatchMapWithMove Event t (PatchMapWithMove k (Event t v))
e = (Map k (NodeInfo k v) -> PatchMapWithMove k v)
-> Event t (Map k (NodeInfo k v)) -> Event t (PatchMapWithMove k v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap Map k (NodeInfo k v) -> PatchMapWithMove k v
forall k v. Map k (NodeInfo k v) -> PatchMapWithMove k v
unsafePatchMapWithMove (Event t (Map k (NodeInfo k v)) -> Event t (PatchMapWithMove k v))
-> Event t (Map k (NodeInfo k v)) -> Event t (PatchMapWithMove k v)
forall a b. (a -> b) -> a -> b
$ Event t (Event t (Map k (NodeInfo k v)))
-> Event t (Map k (NodeInfo k v))
forall a. Event t (Event t a) -> Event t a
forall k (t :: k) a. Reflex t => Event t (Event t a) -> Event t a
coincidence (Event t (Event t (Map k (NodeInfo k v)))
 -> Event t (Map k (NodeInfo k v)))
-> Event t (Event t (Map k (NodeInfo k v)))
-> Event t (Map k (NodeInfo k v))
forall a b. (a -> b) -> a -> b
$ Event t (PatchMapWithMove k (Event t v))
-> (PatchMapWithMove k (Event t v)
    -> Event t (Map k (NodeInfo k v)))
-> Event t (Event t (Map k (NodeInfo k v)))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor Event t (PatchMapWithMove k (Event t v))
e ((PatchMapWithMove k (Event t v) -> Event t (Map k (NodeInfo k v)))
 -> Event t (Event t (Map k (NodeInfo k v))))
-> (PatchMapWithMove k (Event t v)
    -> Event t (Map k (NodeInfo k v)))
-> Event t (Event t (Map k (NodeInfo k v)))
forall a b. (a -> b) -> a -> b
$ \PatchMapWithMove k (Event t v)
p -> Map k (Event t (NodeInfo k v)) -> Event t (Map k (NodeInfo k v))
forall {k} (t :: k) k a.
(Reflex t, Ord k) =>
Map k (Event t a) -> Event t (Map k a)
mergeMap (Map k (Event t (NodeInfo k v)) -> Event t (Map k (NodeInfo k v)))
-> Map k (Event t (NodeInfo k v)) -> Event t (Map k (NodeInfo k v))
forall a b. (a -> b) -> a -> b
$ Map k (NodeInfo k (Event t v))
-> (NodeInfo k (Event t v) -> Event t (NodeInfo k v))
-> Map k (Event t (NodeInfo k v))
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor (PatchMapWithMove k (Event t v) -> Map k (NodeInfo k (Event t v))
forall k v. PatchMapWithMove k v -> Map k (NodeInfo k v)
unPatchMapWithMove PatchMapWithMove k (Event t v)
p) ((NodeInfo k (Event t v) -> Event t (NodeInfo k v))
 -> Map k (Event t (NodeInfo k v)))
-> (NodeInfo k (Event t v) -> Event t (NodeInfo k v))
-> Map k (Event t (NodeInfo k v))
forall a b. (a -> b) -> a -> b
$ \NodeInfo k (Event t v)
ni -> case NodeInfo k (Event t v) -> From k (Event t v)
forall k v. NodeInfo k v -> From k v
PatchMapWithMove._nodeInfo_from NodeInfo k (Event t v)
ni of
  From k (Event t v)
PatchMapWithMove.From_Delete -> Event t (PatchMapWithMove k (Event t v))
-> (PatchMapWithMove k (Event t v) -> NodeInfo k v)
-> Event t (NodeInfo k v)
forall {k} (t :: k) a b.
Reflex t =>
Event t a -> (a -> b) -> Event t b
fforCheap Event t (PatchMapWithMove k (Event t v))
e ((PatchMapWithMove k (Event t v) -> NodeInfo k v)
 -> Event t (NodeInfo k v))
-> (PatchMapWithMove k (Event t v) -> NodeInfo k v)
-> Event t (NodeInfo k v)
forall a b. (a -> b) -> a -> b
$ \PatchMapWithMove k (Event t v)
_ ->
    NodeInfo k (Event t v)
ni { _nodeInfo_from :: From k v
PatchMapWithMove._nodeInfo_from = From k v
forall k v. From k v
PatchMapWithMove.From_Delete }
  PatchMapWithMove.From_Move k
k -> Event t (PatchMapWithMove k (Event t v))
-> (PatchMapWithMove k (Event t v) -> NodeInfo k v)
-> Event t (NodeInfo k v)
forall {k} (t :: k) a b.
Reflex t =>
Event t a -> (a -> b) -> Event t b
fforCheap Event t (PatchMapWithMove k (Event t v))
e ((PatchMapWithMove k (Event t v) -> NodeInfo k v)
 -> Event t (NodeInfo k v))
-> (PatchMapWithMove k (Event t v) -> NodeInfo k v)
-> Event t (NodeInfo k v)
forall a b. (a -> b) -> a -> b
$ \PatchMapWithMove k (Event t v)
_ ->
    NodeInfo k (Event t v)
ni { _nodeInfo_from :: From k v
PatchMapWithMove._nodeInfo_from = k -> From k v
forall k v. k -> From k v
PatchMapWithMove.From_Move k
k }
  PatchMapWithMove.From_Insert Event t v
ev -> [Event t (NodeInfo k v)] -> Event t (NodeInfo k v)
forall {k} (t :: k) a. Reflex t => [Event t a] -> Event t a
leftmost
    [ Event t v -> (v -> NodeInfo k v) -> Event t (NodeInfo k v)
forall {k} (t :: k) a b.
Reflex t =>
Event t a -> (a -> b) -> Event t b
fforCheap Event t v
ev ((v -> NodeInfo k v) -> Event t (NodeInfo k v))
-> (v -> NodeInfo k v) -> Event t (NodeInfo k v)
forall a b. (a -> b) -> a -> b
$ \v
v ->
        NodeInfo k (Event t v)
ni { _nodeInfo_from :: From k v
PatchMapWithMove._nodeInfo_from = v -> From k v
forall v k. v -> From k v
PatchMapWithMove.From_Insert v
v }
    , Event t (PatchMapWithMove k (Event t v))
-> (PatchMapWithMove k (Event t v) -> NodeInfo k v)
-> Event t (NodeInfo k v)
forall {k} (t :: k) a b.
Reflex t =>
Event t a -> (a -> b) -> Event t b
fforCheap Event t (PatchMapWithMove k (Event t v))
e ((PatchMapWithMove k (Event t v) -> NodeInfo k v)
 -> Event t (NodeInfo k v))
-> (PatchMapWithMove k (Event t v) -> NodeInfo k v)
-> Event t (NodeInfo k v)
forall a b. (a -> b) -> a -> b
$ \PatchMapWithMove k (Event t v)
_ ->
        NodeInfo k (Event t v)
ni { _nodeInfo_from :: From k v
PatchMapWithMove._nodeInfo_from = From k v
forall k v. From k v
PatchMapWithMove.From_Delete }
    ]

-- | Given a 'PatchTarget' of events (e.g., a 'Map' with 'Event' values) and an event of 'Patch'es
-- (e.g., a 'PatchMap' with 'Event' values), produce an 'Event' of the 'PatchTarget' type that
-- fires with the patched value.
switchHoldPromptOnlyIncremental
  :: forall t m p pt w
  .  ( Reflex t
     , MonadHold t m
     , Patch (p (Event t w))
     , PatchTarget (p (Event t w)) ~ pt (Event t w)
     , Patch (p w)
     , PatchTarget (p w) ~ pt w
     , Monoid (pt w)
     )
  => (Incremental t (p (Event t w)) -> Event t (pt w))
  -> (Event t (p (Event t w)) -> Event t (p w))
  -> pt (Event t w)
  -> Event t (p (Event t w))
  -> m (Event t (pt w))
switchHoldPromptOnlyIncremental :: forall {k} (t :: k) (m :: * -> *) (p :: * -> *) (pt :: * -> *) w.
(Reflex t, MonadHold t m, Patch (p (Event t w)),
 PatchTarget (p (Event t w)) ~ pt (Event t w), Patch (p w),
 PatchTarget (p w) ~ pt w, Monoid (pt w)) =>
(Incremental t (p (Event t w)) -> Event t (pt w))
-> (Event t (p (Event t w)) -> Event t (p w))
-> pt (Event t w)
-> Event t (p (Event t w))
-> m (Event t (pt w))
switchHoldPromptOnlyIncremental Incremental t (p (Event t w)) -> Event t (pt w)
mergePatchIncremental Event t (p (Event t w)) -> Event t (p w)
coincidencePatch pt (Event t w)
e0 Event t (p (Event t w))
e' = do
  Event t (pt w)
lag <- Incremental t (p (Event t w)) -> Event t (pt w)
mergePatchIncremental (Incremental t (p (Event t w)) -> Event t (pt w))
-> m (Incremental t (p (Event t w))) -> m (Event t (pt w))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> PatchTarget (p (Event t w))
-> Event t (p (Event t w)) -> m (Incremental t (p (Event t w)))
forall p.
Patch p =>
PatchTarget p -> Event t p -> m (Incremental t p)
forall {k} (t :: k) (m :: * -> *) p.
(MonadHold t m, Patch p) =>
PatchTarget p -> Event t p -> m (Incremental t p)
holdIncremental pt (Event t w)
PatchTarget (p (Event t w))
e0 Event t (p (Event t w))
e'
  Event t (pt w) -> m (Event t (pt w))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Event t (pt w) -> m (Event t (pt w)))
-> Event t (pt w) -> m (Event t (pt w))
forall a b. (a -> b) -> a -> b
$ Event t (These (pt w) (p w))
-> (These (pt w) (p w) -> pt w) -> Event t (pt w)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
ffor (Event t (pt w) -> Event t (p w) -> Event t (These (pt w) (p w))
forall a b. Event t a -> Event t b -> Event t (These a b)
forall (f :: * -> *) a b.
Semialign f =>
f a -> f b -> f (These a b)
align Event t (pt w)
lag (Event t (p (Event t w)) -> Event t (p w)
coincidencePatch Event t (p (Event t w))
e')) ((These (pt w) (p w) -> pt w) -> Event t (pt w))
-> (These (pt w) (p w) -> pt w) -> Event t (pt w)
forall a b. (a -> b) -> a -> b
$ \case
    This pt w
old -> pt w
old
    That p w
new -> p w
new p w -> PatchTarget (p w) -> PatchTarget (p w)
forall p. Patch p => p -> PatchTarget p -> PatchTarget p
`applyAlways` pt w
PatchTarget (p w)
forall a. Monoid a => a
mempty
    These pt w
old p w
new -> p w
new p w -> PatchTarget (p w) -> PatchTarget (p w)
forall p. Patch p => p -> PatchTarget p -> PatchTarget p
`applyAlways` pt w
PatchTarget (p w)
old

instance Reflex t => Align (Event t) where
  nil :: forall a. Event t a
nil = Event t a
forall a. Event t a
forall {k} (t :: k) a. Reflex t => Event t a
never
#if MIN_VERSION_these(0, 8, 0)
instance Reflex t => Semialign (Event t) where
#endif
  align :: forall a b. Event t a -> Event t b -> Event t (These a b)
align = (These a b -> Maybe (These a b))
-> Event t a -> Event t b -> Event t (These a b)
forall {k} (t :: k) a b c.
Reflex t =>
(These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
alignEventWithMaybe These a b -> Maybe (These a b)
forall a. a -> Maybe a
Just

#ifdef MIN_VERSION_semialign
#if MIN_VERSION_semialign(1,1,0)
instance Reflex t => Zip (Event t) where
#endif
  zip :: forall a b. Event t a -> Event t b -> Event t (a, b)
zip Event t a
x Event t b
y = (These a b -> Maybe (a, b))
-> Event t (These a b) -> Event t (a, b)
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe These a b -> Maybe (a, b)
forall a b. These a b -> Maybe (a, b)
justThese (Event t (These a b) -> Event t (a, b))
-> Event t (These a b) -> Event t (a, b)
forall a b. (a -> b) -> a -> b
$ Event t a -> Event t b -> Event t (These a b)
forall a b. Event t a -> Event t b -> Event t (These a b)
forall (f :: * -> *) a b.
Semialign f =>
f a -> f b -> f (These a b)
align Event t a
x Event t b
y
#endif

#ifdef MIN_VERSION_semialign
#if MIN_VERSION_semialign(1,1,0)
instance Reflex t => Unzip (Event t) where
  unzip :: forall a b. Event t (a, b) -> (Event t a, Event t b)
unzip = Event t (a, b) -> (Event t a, Event t b)
forall {k} (t :: k) a b.
Reflex t =>
Event t (a, b) -> (Event t a, Event t b)
splitE
#endif
#endif

-- | Create a new 'Event' that only occurs if the supplied 'Event' occurs and
-- the 'Behavior' is true at the time of occurrence.
gate :: Reflex t => Behavior t Bool -> Event t a -> Event t a
gate :: forall {k} (t :: k) a.
Reflex t =>
Behavior t Bool -> Event t a -> Event t a
gate = (Bool -> a -> Maybe a) -> Behavior t Bool -> Event t a -> Event t a
forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> Maybe c) -> Behavior t a -> Event t b -> Event t c
attachWithMaybe ((Bool -> a -> Maybe a)
 -> Behavior t Bool -> Event t a -> Event t a)
-> (Bool -> a -> Maybe a)
-> Behavior t Bool
-> Event t a
-> Event t a
forall a b. (a -> b) -> a -> b
$ \Bool
allow a
a -> if Bool
allow then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
forall a. Maybe a
Nothing

-- | Create a new behavior given a starting behavior and switch to the behavior
-- carried by the event when it fires.
switcher :: (Reflex t, MonadHold t m)
        => Behavior t a -> Event t (Behavior t a) -> m (Behavior t a)
switcher :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Behavior t a -> Event t (Behavior t a) -> m (Behavior t a)
switcher Behavior t a
b Event t (Behavior t a)
eb = PullM t a -> Behavior t a
forall a. PullM t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => PullM t a -> Behavior t a
pull (PullM t a -> Behavior t a)
-> (Behavior t (Behavior t a) -> PullM t a)
-> Behavior t (Behavior t a)
-> Behavior t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t a -> PullM t a)
-> (Behavior t (Behavior t a) -> PullM t (Behavior t a))
-> Behavior t (Behavior t a)
-> PullM t a
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Behavior t (Behavior t a) -> PullM t (Behavior t a)
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample) (Behavior t (Behavior t a) -> Behavior t a)
-> m (Behavior t (Behavior t a)) -> m (Behavior t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior t a
-> Event t (Behavior t a) -> m (Behavior t (Behavior t a))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold Behavior t a
b Event t (Behavior t a)
eb

instance (Reflex t, IsString a) => IsString (Dynamic t a) where
  fromString :: String -> Dynamic t a
fromString = a -> Dynamic t a
forall a. a -> Dynamic t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Dynamic t a) -> (String -> a) -> String -> Dynamic t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> a
forall a. IsString a => String -> a
fromString

-- | Combine two 'Dynamic's.  The result will change whenever either (or both)
-- input 'Dynamic' changes.  Equivalent to @zipDynWith (,)@.
zipDyn :: Reflex t => Dynamic t a -> Dynamic t b -> Dynamic t (a, b)
zipDyn :: forall {k} (t :: k) a b.
Reflex t =>
Dynamic t a -> Dynamic t b -> Dynamic t (a, b)
zipDyn = (a -> b -> (a, b))
-> Dynamic t a -> Dynamic t b -> Dynamic t (a, b)
forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
zipDynWith (,)

-- | Combine two 'Dynamic's with a combining function.  The result will change
-- whenever either (or both) input 'Dynamic' changes.
-- More efficient than 'liftA2'.
zipDynWith :: Reflex t => (a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
zipDynWith :: forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
zipDynWith a -> b -> c
f Dynamic t a
da Dynamic t b
db =
  let eab :: Event t (These a b)
eab = Event t a -> Event t b -> Event t (These a b)
forall a b. Event t a -> Event t b -> Event t (These a b)
forall (f :: * -> *) a b.
Semialign f =>
f a -> f b -> f (These a b)
align (Dynamic t a -> Event t a
forall a. Dynamic t a -> Event t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Event t a
updated Dynamic t a
da) (Dynamic t b -> Event t b
forall a. Dynamic t a -> Event t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Event t a
updated Dynamic t b
db)
      ec :: Event t c
ec = ((These a b -> PushM t (Maybe c))
 -> Event t (These a b) -> Event t c)
-> Event t (These a b)
-> (These a b -> PushM t (Maybe c))
-> Event t c
forall a b c. (a -> b -> c) -> b -> a -> c
flip (These a b -> PushM t (Maybe c))
-> Event t (These a b) -> Event t c
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t (These a b)
eab ((These a b -> PushM t (Maybe c)) -> Event t c)
-> (These a b -> PushM t (Maybe c)) -> Event t c
forall a b. (a -> b) -> a -> b
$ \These a b
o -> do
        (a
a, b
b) <- case These a b
o of
          This a
a -> do
            b
b <- Behavior t b -> PushM t b
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t b -> PushM t b) -> Behavior t b -> PushM t b
forall a b. (a -> b) -> a -> b
$ Dynamic t b -> Behavior t b
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t b
db
            (a, b) -> PushM t (a, b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b)
          That b
b -> do
            a
a <- Behavior t a -> PushM t a
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t a -> PushM t a) -> Behavior t a -> PushM t a
forall a b. (a -> b) -> a -> b
$ Dynamic t a -> Behavior t a
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t a
da
            (a, b) -> PushM t (a, b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b)
          These a
a b
b -> (a, b) -> PushM t (a, b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, b
b)
        Maybe c -> PushM t (Maybe c)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe c -> PushM t (Maybe c)) -> Maybe c -> PushM t (Maybe c)
forall a b. (a -> b) -> a -> b
$ c -> Maybe c
forall a. a -> Maybe a
Just (c -> Maybe c) -> c -> Maybe c
forall a b. (a -> b) -> a -> b
$ a -> b -> c
f a
a b
b
  in PullM t c -> Event t c -> Dynamic t c
forall a. PullM t a -> Event t a -> Dynamic t a
forall {k} (t :: k) a.
Reflex t =>
PullM t a -> Event t a -> Dynamic t a
unsafeBuildDynamic (a -> b -> c
f (a -> b -> c) -> PullM t a -> PullM t (b -> c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior t a -> PullM t a
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Dynamic t a -> Behavior t a
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t a
da) PullM t (b -> c) -> PullM t b -> PullM t c
forall a b. PullM t (a -> b) -> PullM t a -> PullM t b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Behavior t b -> PullM t b
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Dynamic t b -> Behavior t b
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t b
db)) Event t c
ec

instance (Reflex t, Semigroup a) => Semigroup (Dynamic t a) where
  <> :: Dynamic t a -> Dynamic t a -> Dynamic t a
(<>) = (a -> a -> a) -> Dynamic t a -> Dynamic t a -> Dynamic t a
forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
zipDynWith a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>)
  stimes :: forall b. Integral b => b -> Dynamic t a -> Dynamic t a
stimes b
n = (a -> a) -> Dynamic t a -> Dynamic t a
forall a b. (a -> b) -> Dynamic t a -> Dynamic t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> a) -> Dynamic t a -> Dynamic t a)
-> (a -> a) -> Dynamic t a -> Dynamic t a
forall a b. (a -> b) -> a -> b
$ b -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes b
n

instance (Reflex t, Monoid a) => Monoid (Dynamic t a) where
  mconcat :: [Dynamic t a] -> Dynamic t a
mconcat = ([a] -> a) -> [Dynamic t a] -> Dynamic t a
forall {k} (t :: k) a b.
Reflex t =>
([a] -> b) -> [Dynamic t a] -> Dynamic t b
distributeListOverDynWith [a] -> a
forall a. Monoid a => [a] -> a
mconcat
  mempty :: Dynamic t a
mempty = a -> Dynamic t a
forall {k} (t :: k) a. Reflex t => a -> Dynamic t a
constDyn a
forall a. Monoid a => a
mempty
  mappend :: Dynamic t a -> Dynamic t a -> Dynamic t a
mappend = (a -> a -> a) -> Dynamic t a -> Dynamic t a -> Dynamic t a
forall {k} (t :: k) a b c.
Reflex t =>
(a -> b -> c) -> Dynamic t a -> Dynamic t b -> Dynamic t c
zipDynWith a -> a -> a
forall a. Monoid a => a -> a -> a
mappend

-- | This function converts a 'DMap' whose elements are 'Dynamic's into a
-- 'Dynamic' 'DMap'.  Its implementation is more efficient than doing the same
-- through the use of multiple uses of 'zipDynWith' or 'Applicative' operators.
distributeDMapOverDynPure :: forall t k. (Reflex t, GCompare k) => DMap k (Dynamic t) -> Dynamic t (DMap k Identity)
distributeDMapOverDynPure :: forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
DMap k (Dynamic t) -> Dynamic t (DMap k Identity)
distributeDMapOverDynPure = (forall a. Dynamic t a -> Dynamic t (Identity a))
-> DMap k (Dynamic t) -> Dynamic t (DMap k Identity)
forall {k} {k} (t :: k) (k :: k -> *) (q :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
(forall (a :: k). q a -> Dynamic t (v a))
-> DMap k q -> Dynamic t (DMap k v)
distributeDMapOverDynPureG Dynamic t a -> Dynamic t (Identity a)
forall a. Dynamic t a -> Dynamic t (Identity a)
forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Dynamic t a -> Dynamic t b
coerceDynamic

-- | This function converts a 'DMap' whose elements are 'Dynamic's into a
-- 'Dynamic' 'DMap'.  Its implementation is more efficient than doing the same
-- through the use of multiple uses of 'zipDynWith' or 'Applicative' operators.
distributeDMapOverDynPureG
  :: forall t k q v. (Reflex t, GCompare k)
  => (forall a. q a -> Dynamic t (v a))
  -> DMap k q -> Dynamic t (DMap k v)
distributeDMapOverDynPureG :: forall {k} {k} (t :: k) (k :: k -> *) (q :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
(forall (a :: k). q a -> Dynamic t (v a))
-> DMap k q -> Dynamic t (DMap k v)
distributeDMapOverDynPureG forall (a :: k). q a -> Dynamic t (v a)
nt DMap k q
dm = case DMap k q -> [DSum k q]
forall {k1} (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> [DSum k2 f]
DMap.toList DMap k q
dm of
  [] -> DMap k v -> Dynamic t (DMap k v)
forall {k} (t :: k) a. Reflex t => a -> Dynamic t a
constDyn DMap k v
forall {k1} (k2 :: k1 -> *) (f :: k1 -> *). DMap k2 f
DMap.empty
  [k a
k :=> q a
v] -> k a -> v a -> DMap k v
forall {k1} (k2 :: k1 -> *) (v :: k1) (f :: k1 -> *).
k2 v -> f v -> DMap k2 f
DMap.singleton k a
k (v a -> DMap k v) -> Dynamic t (v a) -> Dynamic t (DMap k v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> q a -> Dynamic t (v a)
forall (a :: k). q a -> Dynamic t (v a)
nt q a
v
  [DSum k q]
_ ->
    let getInitial :: PullM t (DMap k v)
getInitial = (forall (v :: k). k v -> q v -> PullM t (v v))
-> DMap k q -> PullM t (DMap k v)
forall {k1} (t :: * -> *) (k2 :: k1 -> *) (f :: k1 -> *)
       (g :: k1 -> *).
Applicative t =>
(forall (v :: k1). k2 v -> f v -> t (g v))
-> DMap k2 f -> t (DMap k2 g)
DMap.traverseWithKey (\k v
_ -> Behavior t (v v) -> PullM t (v v)
forall a. Behavior t a -> PullM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (v v) -> PullM t (v v))
-> (q v -> Behavior t (v v)) -> q v -> PullM t (v v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dynamic t (v v) -> Behavior t (v v)
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current (Dynamic t (v v) -> Behavior t (v v))
-> (q v -> Dynamic t (v v)) -> q v -> Behavior t (v v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. q v -> Dynamic t (v v)
forall (a :: k). q a -> Dynamic t (v a)
nt) DMap k q
dm
        edmPre :: Event t (DMap k v)
edmPre = (forall (a :: k). Compose (Event t) v a -> Event t (v a))
-> DMap k (Compose (Event t) v) -> Event t (DMap k v)
forall {k} (t :: k) {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
(forall (a :: k). q a -> Event t (v a))
-> DMap k q -> Event t (DMap k v)
forall {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
GCompare k =>
(forall (a :: k). q a -> Event t (v a))
-> DMap k q -> Event t (DMap k v)
mergeG Compose (Event t) v a -> Event t (v a)
forall (a :: k). Compose (Event t) v a -> Event t (v a)
forall {k1} {k2} (f :: k1 -> *) (g :: k2 -> k1) (a :: k2).
Compose f g a -> f (g a)
getCompose (DMap k (Compose (Event t) v) -> Event t (DMap k v))
-> DMap k (Compose (Event t) v) -> Event t (DMap k v)
forall a b. (a -> b) -> a -> b
$ (forall (v :: k). q v -> Compose (Event t) v v)
-> DMap k q -> DMap k (Compose (Event t) v)
forall {k1} (f :: k1 -> *) (g :: k1 -> *) (k2 :: k1 -> *).
(forall (v :: k1). f v -> g v) -> DMap k2 f -> DMap k2 g
DMap.map (Event t (v v) -> Compose (Event t) v v
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Event t (v v) -> Compose (Event t) v v)
-> (q v -> Event t (v v)) -> q v -> Compose (Event t) v v
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Dynamic t (v v) -> Event t (v v)
forall a. Dynamic t a -> Event t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Event t a
updated (Dynamic t (v v) -> Event t (v v))
-> (q v -> Dynamic t (v v)) -> q v -> Event t (v v)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. q v -> Dynamic t (v v)
forall (a :: k). q a -> Dynamic t (v a)
nt) DMap k q
dm
        result :: Dynamic t (DMap k v)
result = PullM t (DMap k v) -> Event t (DMap k v) -> Dynamic t (DMap k v)
forall a. PullM t a -> Event t a -> Dynamic t a
forall {k} (t :: k) a.
Reflex t =>
PullM t a -> Event t a -> Dynamic t a
unsafeBuildDynamic PullM t (DMap k v)
getInitial (Event t (DMap k v) -> Dynamic t (DMap k v))
-> Event t (DMap k v) -> Dynamic t (DMap k v)
forall a b. (a -> b) -> a -> b
$ ((DMap k v -> PushM t (DMap k v))
 -> Event t (DMap k v) -> Event t (DMap k v))
-> Event t (DMap k v)
-> (DMap k v -> PushM t (DMap k v))
-> Event t (DMap k v)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (DMap k v -> PushM t (DMap k v))
-> Event t (DMap k v) -> Event t (DMap k v)
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t b) -> Event t a -> Event t b
pushAlways Event t (DMap k v)
edmPre ((DMap k v -> PushM t (DMap k v)) -> Event t (DMap k v))
-> (DMap k v -> PushM t (DMap k v)) -> Event t (DMap k v)
forall a b. (a -> b) -> a -> b
$ \DMap k v
news -> do
          DMap k v
olds <- Behavior t (DMap k v) -> PushM t (DMap k v)
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t (DMap k v) -> PushM t (DMap k v))
-> Behavior t (DMap k v) -> PushM t (DMap k v)
forall a b. (a -> b) -> a -> b
$ Dynamic t (DMap k v) -> Behavior t (DMap k v)
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t (DMap k v)
result
          DMap k v -> PushM t (DMap k v)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (DMap k v -> PushM t (DMap k v)) -> DMap k v -> PushM t (DMap k v)
forall a b. (a -> b) -> a -> b
$ (forall (v :: k). k v -> v v -> v v -> v v)
-> DMap k v -> DMap k v -> DMap k v
forall {k1} (k2 :: k1 -> *) (f :: k1 -> *).
GCompare k2 =>
(forall (v :: k1). k2 v -> f v -> f v -> f v)
-> DMap k2 f -> DMap k2 f -> DMap k2 f
DMap.unionWithKey (\k v
_ v v
_ v v
new -> v v
new) DMap k v
olds DMap k v
news
    in Dynamic t (DMap k v)
result

-- | Convert a list of 'Dynamic's into a 'Dynamic' list.
distributeListOverDyn :: Reflex t => [Dynamic t a] -> Dynamic t [a]
distributeListOverDyn :: forall {k} (t :: k) a. Reflex t => [Dynamic t a] -> Dynamic t [a]
distributeListOverDyn = ([a] -> [a]) -> [Dynamic t a] -> Dynamic t [a]
forall {k} (t :: k) a b.
Reflex t =>
([a] -> b) -> [Dynamic t a] -> Dynamic t b
distributeListOverDynWith [a] -> [a]
forall a. a -> a
id

-- | Create a new 'Dynamic' by applying a combining function to a list of 'Dynamic's
distributeListOverDynWith :: Reflex t => ([a] -> b) -> [Dynamic t a] -> Dynamic t b
distributeListOverDynWith :: forall {k} (t :: k) a b.
Reflex t =>
([a] -> b) -> [Dynamic t a] -> Dynamic t b
distributeListOverDynWith [a] -> b
f =
  (DMap (Const2 Int a) Identity -> b)
-> Dynamic t (DMap (Const2 Int a) Identity) -> Dynamic t b
forall a b. (a -> b) -> Dynamic t a -> Dynamic t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([a] -> b
f ([a] -> b)
-> (DMap (Const2 Int a) Identity -> [a])
-> DMap (Const2 Int a) Identity
-> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DSum (Const2 Int a) Identity -> a)
-> [DSum (Const2 Int a) Identity] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map DSum (Const2 Int a) Identity -> a
forall a. DSum (Const2 Int a) Identity -> a
fromDSum ([DSum (Const2 Int a) Identity] -> [a])
-> (DMap (Const2 Int a) Identity -> [DSum (Const2 Int a) Identity])
-> DMap (Const2 Int a) Identity
-> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DMap (Const2 Int a) Identity -> [DSum (Const2 Int a) Identity]
forall {k1} (k2 :: k1 -> *) (f :: k1 -> *).
DMap k2 f -> [DSum k2 f]
DMap.toAscList) (Dynamic t (DMap (Const2 Int a) Identity) -> Dynamic t b)
-> ([Dynamic t a] -> Dynamic t (DMap (Const2 Int a) Identity))
-> [Dynamic t a]
-> Dynamic t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  DMap (Const2 Int a) (Dynamic t)
-> Dynamic t (DMap (Const2 Int a) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
DMap k (Dynamic t) -> Dynamic t (DMap k Identity)
distributeDMapOverDynPure (DMap (Const2 Int a) (Dynamic t)
 -> Dynamic t (DMap (Const2 Int a) Identity))
-> ([Dynamic t a] -> DMap (Const2 Int a) (Dynamic t))
-> [Dynamic t a]
-> Dynamic t (DMap (Const2 Int a) Identity)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  [DSum (Const2 Int a) (Dynamic t)]
-> DMap (Const2 Int a) (Dynamic t)
forall {k1} (k2 :: k1 -> *) (f :: k1 -> *).
[DSum k2 f] -> DMap k2 f
DMap.fromDistinctAscList ([DSum (Const2 Int a) (Dynamic t)]
 -> DMap (Const2 Int a) (Dynamic t))
-> ([Dynamic t a] -> [DSum (Const2 Int a) (Dynamic t)])
-> [Dynamic t a]
-> DMap (Const2 Int a) (Dynamic t)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
  (Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t))
-> [Int] -> [Dynamic t a] -> [DSum (Const2 Int a) (Dynamic t)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
forall (f :: * -> *) a b c.
Zip f =>
(a -> b -> c) -> f a -> f b -> f c
zipWith Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
forall {k} (t :: k) a.
Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
toDSum [Int
0..]
  where
    toDSum :: Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
    toDSum :: forall {k} (t :: k) a.
Int -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
toDSum Int
k Dynamic t a
v = Int -> Const2 Int a a
forall {x} a (b :: x). a -> Const2 a b b
Const2 Int
k Const2 Int a a -> Dynamic t a -> DSum (Const2 Int a) (Dynamic t)
forall {k} (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> Dynamic t a
v
    fromDSum :: DSum (Const2 Int a) Identity -> a
    fromDSum :: forall a. DSum (Const2 Int a) Identity -> a
fromDSum (Const2 Int
_ :=> Identity a
v) = a
a
v

-- | Create a new 'Event' that occurs when the first supplied 'Event' occurs
-- unless the second supplied 'Event' occurs simultaneously.
difference :: Reflex t => Event t a -> Event t b -> Event t a
difference :: forall {k} (t :: k) a b.
Reflex t =>
Event t a -> Event t b -> Event t a
difference = (These a b -> Maybe a) -> Event t a -> Event t b -> Event t a
forall {k} (t :: k) a b c.
Reflex t =>
(These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
alignEventWithMaybe ((These a b -> Maybe a) -> Event t a -> Event t b -> Event t a)
-> (These a b -> Maybe a) -> Event t a -> Event t b -> Event t a
forall a b. (a -> b) -> a -> b
$ \case
  This a
a -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
  These a b
_ -> Maybe a
forall a. Maybe a
Nothing

-- | Zips two values by taking the union of their shapes and combining with the provided function.
-- 'Nothing' values are dropped.
alignEventWithMaybe :: Reflex t => (These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
alignEventWithMaybe :: forall {k} (t :: k) a b c.
Reflex t =>
(These a b -> Maybe c) -> Event t a -> Event t b -> Event t c
alignEventWithMaybe These a b -> Maybe c
f Event t a
ea Event t b
eb = (DMap (EitherTag a b) Identity -> Maybe c)
-> Event t (DMap (EitherTag a b) Identity) -> Event t c
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (These a b -> Maybe c
f (These a b -> Maybe c)
-> (DMap (EitherTag a b) Identity -> Maybe (These a b))
-> DMap (EitherTag a b) Identity
-> Maybe c
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< DMap (EitherTag a b) Identity -> Maybe (These a b)
forall a b. DMap (EitherTag a b) Identity -> Maybe (These a b)
dmapToThese) (Event t (DMap (EitherTag a b) Identity) -> Event t c)
-> Event t (DMap (EitherTag a b) Identity) -> Event t c
forall a b. (a -> b) -> a -> b
$
  DMap (EitherTag a b) (Event t)
-> Event t (DMap (EitherTag a b) Identity)
forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
DMap k (Event t) -> Event t (DMap k Identity)
merge (DMap (EitherTag a b) (Event t)
 -> Event t (DMap (EitherTag a b) Identity))
-> DMap (EitherTag a b) (Event t)
-> Event t (DMap (EitherTag a b) Identity)
forall a b. (a -> b) -> a -> b
$ [DSum (EitherTag a b) (Event t)] -> DMap (EitherTag a b) (Event t)
forall {k1} (k2 :: k1 -> *) (f :: k1 -> *).
GCompare k2 =>
[DSum k2 f] -> DMap k2 f
DMap.fromList [EitherTag a b a
forall {k} (l :: k) (r :: k). EitherTag l r l
LeftTag EitherTag a b a -> Event t a -> DSum (EitherTag a b) (Event t)
forall {k} (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> Event t a
ea, EitherTag a b b
forall {k} (l :: k) (r :: k). EitherTag l r r
RightTag EitherTag a b b -> Event t b -> DSum (EitherTag a b) (Event t)
forall {k} (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> Event t b
eb]

-- | Produces an 'Event' that fires only when the input event fires with a 'DSum' key that
-- matches the provided key.
filterEventKey
  :: forall t m k v a.
     ( Reflex t
     , MonadFix m
     , MonadHold t m
     , GEq k
     )
  => k a
  -> Event t (DSum k v)
  -> m (Event t (v a))
filterEventKey :: forall {k} {k} (t :: k) (m :: * -> *) (k :: k -> *) (v :: k -> *)
       (a :: k).
(Reflex t, MonadFix m, MonadHold t m, GEq k) =>
k a -> Event t (DSum k v) -> m (Event t (v a))
filterEventKey k a
k Event t (DSum k v)
kv' = do
  let f :: DSum k v -> Maybe (v a)
      f :: DSum k v -> Maybe (v a)
f (k a
newK :=> v a
newV) = case k a
newK k a -> k a -> Maybe (a :~: a)
forall (a :: k) (b :: k). k a -> k b -> Maybe (a :~: b)
forall k (f :: k -> *) (a :: k) (b :: k).
GEq f =>
f a -> f b -> Maybe (a :~: b)
`geq` k a
k of
        Just a :~: a
Refl -> v a -> Maybe (v a)
forall a. a -> Maybe a
Just v a
v a
newV
        Maybe (a :~: a)
Nothing -> Maybe (v a)
forall a. Maybe a
Nothing
  (DSum k v -> Maybe (v a))
-> Event t (DSum k v) -> m (Event t (v a))
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadFix m, MonadHold t m) =>
(a -> Maybe b) -> Event t a -> m (Event t b)
takeWhileJustE DSum k v -> Maybe (v a)
f Event t (DSum k v)
kv'

-- | "Factor" the input 'DSum' 'Event' to produce an 'Event' which
-- fires when the 'DSum' key changes and contains both the value of the
-- 'DSum' at switchover and an 'Event' of values produced by subsequent
-- firings of the input 'Event' that do not change the 'DSum' key.
factorEvent
  :: forall t m k v a.
     ( Reflex t
     , MonadFix m
     , MonadHold t m
     , GEq k
     )
  => k a
  -> Event t (DSum k v)
  -> m (Event t (v a), Event t (DSum k (Product v (Compose (Event t) v))))
factorEvent :: forall {k} {k1} (t :: k) (m :: * -> *) (k :: k1 -> *)
       (v :: k1 -> *) (a :: k1).
(Reflex t, MonadFix m, MonadHold t m, GEq k) =>
k a
-> Event t (DSum k v)
-> m (Event t (v a),
      Event t (DSum k (Product v (Compose (Event t) v))))
factorEvent k a
k0 Event t (DSum k v)
kv' = do
  Behavior t (Some k)
key :: Behavior t (Some k) <- Some k -> Event t (Some k) -> m (Behavior t (Some k))
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold (k a -> Some k
forall {k} (tag :: k -> *) (a :: k). tag a -> Some tag
Some k a
k0) (Event t (Some k) -> m (Behavior t (Some k)))
-> Event t (Some k) -> m (Behavior t (Some k))
forall a b. (a -> b) -> a -> b
$ (DSum k v -> Some k) -> Event t (DSum k v) -> Event t (Some k)
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (\(k a
k :=> v a
_) -> k a -> Some k
forall {k} (tag :: k -> *) (a :: k). tag a -> Some tag
Some k a
k) Event t (DSum k v)
kv'
  let update :: Event t (DSum k (Product v (Compose (Event t) v)))
update = ((DSum k v
  -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
 -> Event t (DSum k v)
 -> Event t (DSum k (Product v (Compose (Event t) v))))
-> Event t (DSum k v)
-> (DSum k v
    -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
-> Event t (DSum k (Product v (Compose (Event t) v)))
forall a b c. (a -> b -> c) -> b -> a -> c
flip (DSum k v
 -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
-> Event t (DSum k v)
-> Event t (DSum k (Product v (Compose (Event t) v)))
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t (DSum k v)
kv' ((DSum k v
  -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
 -> Event t (DSum k (Product v (Compose (Event t) v))))
-> (DSum k v
    -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
-> Event t (DSum k (Product v (Compose (Event t) v)))
forall a b. (a -> b) -> a -> b
$ \(k a
newKey :=> v a
newVal) -> Behavior t (Some k) -> PushM t (Some k)
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t (Some k)
key PushM t (Some k)
-> (Some k
    -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
-> PushM t (Maybe (DSum k (Product v (Compose (Event t) v))))
forall a b. PushM t a -> (a -> PushM t b) -> PushM t b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Some k a
oldKey -> case k a
newKey k a -> k a -> Maybe (a :~: a)
forall (a :: k1) (b :: k1). k a -> k b -> Maybe (a :~: b)
forall k (f :: k -> *) (a :: k) (b :: k).
GEq f =>
f a -> f b -> Maybe (a :~: b)
`geq` k a
oldKey of
          Just a :~: a
Refl -> Maybe (DSum k (Product v (Compose (Event t) v)))
-> PushM t (Maybe (DSum k (Product v (Compose (Event t) v))))
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (DSum k (Product v (Compose (Event t) v)))
forall a. Maybe a
Nothing
          Maybe (a :~: a)
Nothing -> do
            Event t (v a)
newInner <- k a -> Event t (DSum k v) -> PushM t (Event t (v a))
forall {k} {k} (t :: k) (m :: * -> *) (k :: k -> *) (v :: k -> *)
       (a :: k).
(Reflex t, MonadFix m, MonadHold t m, GEq k) =>
k a -> Event t (DSum k v) -> m (Event t (v a))
filterEventKey k a
newKey Event t (DSum k v)
kv'
            Maybe (DSum k (Product v (Compose (Event t) v)))
-> PushM t (Maybe (DSum k (Product v (Compose (Event t) v))))
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (DSum k (Product v (Compose (Event t) v)))
 -> PushM t (Maybe (DSum k (Product v (Compose (Event t) v)))))
-> Maybe (DSum k (Product v (Compose (Event t) v)))
-> PushM t (Maybe (DSum k (Product v (Compose (Event t) v))))
forall a b. (a -> b) -> a -> b
$ DSum k (Product v (Compose (Event t) v))
-> Maybe (DSum k (Product v (Compose (Event t) v)))
forall a. a -> Maybe a
Just (DSum k (Product v (Compose (Event t) v))
 -> Maybe (DSum k (Product v (Compose (Event t) v))))
-> DSum k (Product v (Compose (Event t) v))
-> Maybe (DSum k (Product v (Compose (Event t) v)))
forall a b. (a -> b) -> a -> b
$ k a
newKey k a
-> Product v (Compose (Event t) v) a
-> DSum k (Product v (Compose (Event t) v))
forall {k} (tag :: k -> *) (f :: k -> *) (a :: k).
tag a -> f a -> DSum tag f
:=> v a -> Compose (Event t) v a -> Product v (Compose (Event t) v) a
forall {k} (f :: k -> *) (g :: k -> *) (a :: k).
f a -> g a -> Product f g a
Pair v a
newVal (Event t (v a) -> Compose (Event t) v a
forall {k} {k1} (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose Event t (v a)
newInner)
  Event t (v a)
eInitial <- k a -> Event t (DSum k v) -> m (Event t (v a))
forall {k} {k} (t :: k) (m :: * -> *) (k :: k -> *) (v :: k -> *)
       (a :: k).
(Reflex t, MonadFix m, MonadHold t m, GEq k) =>
k a -> Event t (DSum k v) -> m (Event t (v a))
filterEventKey k a
k0 Event t (DSum k v)
kv'
  (Event t (v a), Event t (DSum k (Product v (Compose (Event t) v))))
-> m (Event t (v a),
      Event t (DSum k (Product v (Compose (Event t) v))))
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Event t (v a)
eInitial, Event t (DSum k (Product v (Compose (Event t) v)))
update)

--------------------------------------------------------------------------------
-- Accumulator
--------------------------------------------------------------------------------

-- | An 'Accumulator' type can be built by accumulating occurrences of an
-- 'Event'.
#if __GLASGOW_HASKELL__ < 802
{-# WARNING accum "ghc < 8.2.1 doesn't seem to be able to specialize functions in this class, which can lead to poor performance" #-}
{-# WARNING accumM "ghc < 8.2.1 doesn't seem to be able to specialize functions in this class, which can lead to poor performance" #-}
{-# WARNING accumMaybe "ghc < 8.2.1 doesn't seem to be able to specialize functions in this class, which can lead to poor performance" #-}
{-# WARNING accumMaybeM "ghc < 8.2.1 doesn't seem to be able to specialize functions in this class, which can lead to poor performance" #-}
#endif
class Reflex t => Accumulator t f | f -> t where
  accum :: (MonadHold t m, MonadFix m) => (a -> b -> a) -> a -> Event t b -> m (f a)
  accum a -> b -> a
f = (a -> b -> Maybe a) -> a -> Event t b -> m (f a)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> Maybe a) -> a -> Event t b -> m (f a)
forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> Maybe a) -> a -> Event t b -> m (f a)
accumMaybe ((a -> b -> Maybe a) -> a -> Event t b -> m (f a))
-> (a -> b -> Maybe a) -> a -> Event t b -> m (f a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ a -> b -> a
f a
v b
o
  accumM :: (MonadHold t m, MonadFix m) => (a -> b -> PushM t a) -> a -> Event t b -> m (f a)
  accumM a -> b -> PushM t a
f = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
accumMaybeM ((a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a))
-> (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> PushM t a -> PushM t (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> PushM t a
f a
v b
o
  accumMaybe :: (MonadHold t m, MonadFix m) => (a -> b -> Maybe a) -> a -> Event t b -> m (f a)
  accumMaybe a -> b -> Maybe a
f = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
accumMaybeM ((a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a))
-> (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> Maybe a -> PushM t (Maybe a)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> PushM t (Maybe a)) -> Maybe a -> PushM t (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> b -> Maybe a
f a
v b
o
  accumMaybeM :: (MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
  mapAccum :: (MonadHold t m, MonadFix m) => (a -> b -> (a, c)) -> a -> Event t b -> m (f a, Event t c)
  mapAccum a -> b -> (a, c)
f = (a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b c.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
mapAccumMaybe ((a -> b -> (Maybe a, Maybe c))
 -> a -> Event t b -> m (f a, Event t c))
-> (a -> b -> (Maybe a, Maybe c))
-> a
-> Event t b
-> m (f a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (a -> Maybe a) -> (c -> Maybe c) -> (a, c) -> (Maybe a, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> Maybe a
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((a, c) -> (Maybe a, Maybe c)) -> (a, c) -> (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ a -> b -> (a, c)
f a
v b
o
  mapAccumM :: (MonadHold t m, MonadFix m) => (a -> b -> PushM t (a, c)) -> a -> Event t b -> m (f a, Event t c)
  mapAccumM a -> b -> PushM t (a, c)
f = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b c.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
mapAccumMaybeM ((a -> b -> PushM t (Maybe a, Maybe c))
 -> a -> Event t b -> m (f a, Event t c))
-> (a -> b -> PushM t (Maybe a, Maybe c))
-> a
-> Event t b
-> m (f a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (a -> Maybe a) -> (c -> Maybe c) -> (a, c) -> (Maybe a, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> Maybe a
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((a, c) -> (Maybe a, Maybe c))
-> PushM t (a, c) -> PushM t (Maybe a, Maybe c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> PushM t (a, c)
f a
v b
o
  mapAccumMaybe :: (MonadHold t m, MonadFix m) => (a -> b -> (Maybe a, Maybe c)) -> a -> Event t b -> m (f a, Event t c)
  mapAccumMaybe a -> b -> (Maybe a, Maybe c)
f = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b c.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
mapAccumMaybeM ((a -> b -> PushM t (Maybe a, Maybe c))
 -> a -> Event t b -> m (f a, Event t c))
-> (a -> b -> PushM t (Maybe a, Maybe c))
-> a
-> Event t b
-> m (f a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c))
-> (Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ a -> b -> (Maybe a, Maybe c)
f a
v b
o
  mapAccumMaybeM :: (MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a, Maybe c)) -> a -> Event t b -> m (f a, Event t c)

-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
-- with the provided function. See 'foldDyn'.
accumDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> a)
  -> a
  -> Event t b
  -> m (Dynamic t a)
accumDyn :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> a) -> a -> Event t b -> m (Dynamic t a)
accumDyn a -> b -> a
f = (a -> b -> Maybe a) -> a -> Event t b -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> Maybe a) -> a -> Event t b -> m (Dynamic t a)
accumMaybeDyn ((a -> b -> Maybe a) -> a -> Event t b -> m (Dynamic t a))
-> (a -> b -> Maybe a) -> a -> Event t b -> m (Dynamic t a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ a -> b -> a
f a
v b
o

-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
-- with the provided 'PushM' action.
accumMDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> PushM t a)
  -> a
  -> Event t b
  -> m (Dynamic t a)
accumMDyn :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t a) -> a -> Event t b -> m (Dynamic t a)
accumMDyn a -> b -> PushM t a
f = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
accumMaybeMDyn ((a -> b -> PushM t (Maybe a))
 -> a -> Event t b -> m (Dynamic t a))
-> (a -> b -> PushM t (Maybe a))
-> a
-> Event t b
-> m (Dynamic t a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> PushM t a -> PushM t (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> PushM t a
f a
v b
o

-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
-- with the provided function, discarding 'Nothing' results.
accumMaybeDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> Maybe a)
  -> a
  -> Event t b
  -> m (Dynamic t a)
accumMaybeDyn :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> Maybe a) -> a -> Event t b -> m (Dynamic t a)
accumMaybeDyn a -> b -> Maybe a
f = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
accumMaybeMDyn ((a -> b -> PushM t (Maybe a))
 -> a -> Event t b -> m (Dynamic t a))
-> (a -> b -> PushM t (Maybe a))
-> a
-> Event t b
-> m (Dynamic t a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> Maybe a -> PushM t (Maybe a)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> PushM t (Maybe a)) -> Maybe a -> PushM t (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> b -> Maybe a
f a
v b
o

-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
-- with the provided 'PushM' action, discarding 'Nothing' results.
accumMaybeMDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> PushM t (Maybe a))
  -> a
  -> Event t b
  -> m (Dynamic t a)
accumMaybeMDyn :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
accumMaybeMDyn a -> b -> PushM t (Maybe a)
f a
z Event t b
e = do
  rec let e' :: Event t a
e' = ((b -> PushM t (Maybe a)) -> Event t b -> Event t a)
-> Event t b -> (b -> PushM t (Maybe a)) -> Event t a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe a)) -> Event t b -> Event t a
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe a)) -> Event t a)
-> (b -> PushM t (Maybe a)) -> Event t a
forall a b. (a -> b) -> a -> b
$ \b
o -> do
            a
v <- Behavior t a -> PushM t a
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t a -> PushM t a) -> Behavior t a -> PushM t a
forall a b. (a -> b) -> a -> b
$ Dynamic t a -> Behavior t a
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t a
d'
            a -> b -> PushM t (Maybe a)
f a
v b
o
      Dynamic t a
d' <- a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
z Event t a
e'
  Dynamic t a -> m (Dynamic t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Dynamic t a
d'

-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event'
-- with a function that both accumulates and produces a value to fire
-- as an 'Event'. Returns both the accumulated value and an 'Event'.
mapAccumDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> (a, c))
  -> a
  -> Event t b
  -> m (Dynamic t a, Event t c)
mapAccumDyn :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (a, c)) -> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumDyn a -> b -> (a, c)
f = (a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeDyn ((a -> b -> (Maybe a, Maybe c))
 -> a -> Event t b -> m (Dynamic t a, Event t c))
-> (a -> b -> (Maybe a, Maybe c))
-> a
-> Event t b
-> m (Dynamic t a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (a -> Maybe a) -> (c -> Maybe c) -> (a, c) -> (Maybe a, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> Maybe a
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((a, c) -> (Maybe a, Maybe c)) -> (a, c) -> (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ a -> b -> (a, c)
f a
v b
o

-- | Similar to 'mapAccumDyn' except that the combining function is a
-- 'PushM' action.
mapAccumMDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> PushM t (a, c))
  -> a
  -> Event t b
  -> m (Dynamic t a, Event t c)
mapAccumMDyn :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (a, c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMDyn a -> b -> PushM t (a, c)
f = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeMDyn ((a -> b -> PushM t (Maybe a, Maybe c))
 -> a -> Event t b -> m (Dynamic t a, Event t c))
-> (a -> b -> PushM t (Maybe a, Maybe c))
-> a
-> Event t b
-> m (Dynamic t a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (a -> Maybe a) -> (c -> Maybe c) -> (a, c) -> (Maybe a, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> Maybe a
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((a, c) -> (Maybe a, Maybe c))
-> PushM t (a, c) -> PushM t (Maybe a, Maybe c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> PushM t (a, c)
f a
v b
o

-- | Accumulate a 'Dynamic' by folding occurrences of an 'Event' with
-- a function that both optionally accumulates and optionally produces
-- a value to fire as a separate output 'Event'.
-- Note that because 'Nothing's are discarded in both cases, the output
-- 'Event' may fire even though the output 'Dynamic' has not changed, and
-- the output 'Dynamic' may update even when the output 'Event' is not firing.
mapAccumMaybeDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> (Maybe a, Maybe c))
  -> a
  -> Event t b
  -> m (Dynamic t a, Event t c)
mapAccumMaybeDyn :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeDyn a -> b -> (Maybe a, Maybe c)
f = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeMDyn ((a -> b -> PushM t (Maybe a, Maybe c))
 -> a -> Event t b -> m (Dynamic t a, Event t c))
-> (a -> b -> PushM t (Maybe a, Maybe c))
-> a
-> Event t b
-> m (Dynamic t a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c))
-> (Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ a -> b -> (Maybe a, Maybe c)
f a
v b
o

-- | Like 'mapAccumMaybeDyn' except that the combining function is a
-- 'PushM' action.
mapAccumMaybeMDyn
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> PushM t (Maybe a, Maybe c))
  -> a
  -> Event t b
  -> m (Dynamic t a, Event t c)
mapAccumMaybeMDyn :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeMDyn a -> b -> PushM t (Maybe a, Maybe c)
f a
z Event t b
e = do
  rec let e' :: Event t (Maybe a, Maybe c)
e' = ((b -> PushM t (Maybe (Maybe a, Maybe c)))
 -> Event t b -> Event t (Maybe a, Maybe c))
-> Event t b
-> (b -> PushM t (Maybe (Maybe a, Maybe c)))
-> Event t (Maybe a, Maybe c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe (Maybe a, Maybe c)))
-> Event t b -> Event t (Maybe a, Maybe c)
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe (Maybe a, Maybe c)))
 -> Event t (Maybe a, Maybe c))
-> (b -> PushM t (Maybe (Maybe a, Maybe c)))
-> Event t (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ \b
o -> do
            a
v <- Behavior t a -> PushM t a
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample (Behavior t a -> PushM t a) -> Behavior t a -> PushM t a
forall a b. (a -> b) -> a -> b
$ Dynamic t a -> Behavior t a
forall a. Dynamic t a -> Behavior t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Behavior t a
current Dynamic t a
d'
            (Maybe a, Maybe c)
result <- a -> b -> PushM t (Maybe a, Maybe c)
f a
v b
o
            Maybe (Maybe a, Maybe c) -> PushM t (Maybe (Maybe a, Maybe c))
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Maybe a, Maybe c) -> PushM t (Maybe (Maybe a, Maybe c)))
-> Maybe (Maybe a, Maybe c) -> PushM t (Maybe (Maybe a, Maybe c))
forall a b. (a -> b) -> a -> b
$ case (Maybe a, Maybe c)
result of
              (Maybe a
Nothing, Maybe c
Nothing) -> Maybe (Maybe a, Maybe c)
forall a. Maybe a
Nothing
              (Maybe a, Maybe c)
_ -> (Maybe a, Maybe c) -> Maybe (Maybe a, Maybe c)
forall a. a -> Maybe a
Just (Maybe a, Maybe c)
result
      Dynamic t a
d' <- a -> Event t a -> m (Dynamic t a)
forall a. a -> Event t a -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Dynamic t a)
holdDyn a
z (Event t a -> m (Dynamic t a)) -> Event t a -> m (Dynamic t a)
forall a b. (a -> b) -> a -> b
$ ((Maybe a, Maybe c) -> Maybe a)
-> Event t (Maybe a, Maybe c) -> Event t a
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (Maybe a, Maybe c) -> Maybe a
forall a b. (a, b) -> a
fst Event t (Maybe a, Maybe c)
e'
  (Dynamic t a, Event t c) -> m (Dynamic t a, Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Dynamic t a
d', ((Maybe a, Maybe c) -> Maybe c)
-> Event t (Maybe a, Maybe c) -> Event t c
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (Maybe a, Maybe c) -> Maybe c
forall a b. (a, b) -> b
snd Event t (Maybe a, Maybe c)
e')

-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
-- with the provided function.
{-# INLINE accumB #-}
accumB
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> a)
  -> a
  -> Event t b
  -> m (Behavior t a)
accumB :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> a) -> a -> Event t b -> m (Behavior t a)
accumB a -> b -> a
f = (a -> b -> Maybe a) -> a -> Event t b -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> Maybe a) -> a -> Event t b -> m (Behavior t a)
accumMaybeB ((a -> b -> Maybe a) -> a -> Event t b -> m (Behavior t a))
-> (a -> b -> Maybe a) -> a -> Event t b -> m (Behavior t a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ a -> b -> a
f a
v b
o

-- | Like 'accumB' except that the combining function is a 'PushM' action.
{-# INLINE accumMB #-}
accumMB
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> PushM t a)
  -> a
  -> Event t b
  -> m (Behavior t a)
accumMB :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t a) -> a -> Event t b -> m (Behavior t a)
accumMB a -> b -> PushM t a
f = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
accumMaybeMB ((a -> b -> PushM t (Maybe a))
 -> a -> Event t b -> m (Behavior t a))
-> (a -> b -> PushM t (Maybe a))
-> a
-> Event t b
-> m (Behavior t a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> PushM t a -> PushM t (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> PushM t a
f a
v b
o

-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
-- with the provided function, discarding 'Nothing' results.
{-# INLINE accumMaybeB #-}
accumMaybeB
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> Maybe a)
  -> a
  -> Event t b
  -> m (Behavior t a)
accumMaybeB :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> Maybe a) -> a -> Event t b -> m (Behavior t a)
accumMaybeB a -> b -> Maybe a
f = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
accumMaybeMB ((a -> b -> PushM t (Maybe a))
 -> a -> Event t b -> m (Behavior t a))
-> (a -> b -> PushM t (Maybe a))
-> a
-> Event t b
-> m (Behavior t a)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> Maybe a -> PushM t (Maybe a)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> PushM t (Maybe a)) -> Maybe a -> PushM t (Maybe a)
forall a b. (a -> b) -> a -> b
$ a -> b -> Maybe a
f a
v b
o

-- | Like 'accumMaybeB' except that the combining function is a 'PushM' action.
{-# INLINE accumMaybeMB #-}
accumMaybeMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
accumMaybeMB :: forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
accumMaybeMB a -> b -> PushM t (Maybe a)
f a
z Event t b
e = do
  rec let e' :: Event t a
e' = ((b -> PushM t (Maybe a)) -> Event t b -> Event t a)
-> Event t b -> (b -> PushM t (Maybe a)) -> Event t a
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe a)) -> Event t b -> Event t a
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe a)) -> Event t a)
-> (b -> PushM t (Maybe a)) -> Event t a
forall a b. (a -> b) -> a -> b
$ \b
o -> do
            a
v <- Behavior t a -> PushM t a
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
d'
            a -> b -> PushM t (Maybe a)
f a
v b
o
      Behavior t a
d' <- a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
z Event t a
e'
  Behavior t a -> m (Behavior t a)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Behavior t a
d'

-- | Accumulate a 'Behavior' by folding occurrences of an 'Event'
-- with a function that both accumulates and produces a value to fire
-- as an 'Event'. Returns both the accumulated value and an 'Event'.
{-# INLINE mapAccumB #-}
mapAccumB
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> (a, c))
  -> a
  -> Event t b
  -> m (Behavior t a, Event t c)
mapAccumB :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (a, c)) -> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumB a -> b -> (a, c)
f = (a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeB ((a -> b -> (Maybe a, Maybe c))
 -> a -> Event t b -> m (Behavior t a, Event t c))
-> (a -> b -> (Maybe a, Maybe c))
-> a
-> Event t b
-> m (Behavior t a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (a -> Maybe a) -> (c -> Maybe c) -> (a, c) -> (Maybe a, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> Maybe a
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((a, c) -> (Maybe a, Maybe c)) -> (a, c) -> (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ a -> b -> (a, c)
f a
v b
o

-- | Like 'mapAccumB' except that the combining function is a 'PushM' action.
{-# INLINE mapAccumMB #-}
mapAccumMB
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> PushM t (a, c))
  -> a
  -> Event t b
  -> m (Behavior t a, Event t c)
mapAccumMB :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (a, c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMB a -> b -> PushM t (a, c)
f = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeMB ((a -> b -> PushM t (Maybe a, Maybe c))
 -> a -> Event t b -> m (Behavior t a, Event t c))
-> (a -> b -> PushM t (Maybe a, Maybe c))
-> a
-> Event t b
-> m (Behavior t a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (a -> Maybe a) -> (c -> Maybe c) -> (a, c) -> (Maybe a, Maybe c)
forall a b c d. (a -> b) -> (c -> d) -> (a, c) -> (b, d)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap a -> Maybe a
forall a. a -> Maybe a
Just c -> Maybe c
forall a. a -> Maybe a
Just ((a, c) -> (Maybe a, Maybe c))
-> PushM t (a, c) -> PushM t (Maybe a, Maybe c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> PushM t (a, c)
f a
v b
o

-- | Accumulate a 'Behavior' by folding occurrences of an 'Event' with
-- a function that both optionally accumulates and optionally produces
-- a value to fire as a separate output 'Event'. 'Nothing's are discarded.
{-# INLINE mapAccumMaybeB #-}
mapAccumMaybeB
  :: (Reflex t, MonadHold t m, MonadFix m)
  => (a -> b -> (Maybe a, Maybe c))
  -> a
  -> Event t b
  -> m (Behavior t a, Event t c)
mapAccumMaybeB :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeB a -> b -> (Maybe a, Maybe c)
f = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeMB ((a -> b -> PushM t (Maybe a, Maybe c))
 -> a -> Event t b -> m (Behavior t a, Event t c))
-> (a -> b -> PushM t (Maybe a, Maybe c))
-> a
-> Event t b
-> m (Behavior t a, Event t c)
forall a b. (a -> b) -> a -> b
$ \a
v b
o -> (Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c))
-> (Maybe a, Maybe c) -> PushM t (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ a -> b -> (Maybe a, Maybe c)
f a
v b
o

-- | Like 'mapAccumMaybeB' except that the combining function is a 'PushM' action.
{-# INLINE mapAccumMaybeMB #-}
mapAccumMaybeMB :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a, Maybe c)) -> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeMB :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeMB a -> b -> PushM t (Maybe a, Maybe c)
f a
z Event t b
e = do
  rec let e' :: Event t (Maybe a, Maybe c)
e' = ((b -> PushM t (Maybe (Maybe a, Maybe c)))
 -> Event t b -> Event t (Maybe a, Maybe c))
-> Event t b
-> (b -> PushM t (Maybe (Maybe a, Maybe c)))
-> Event t (Maybe a, Maybe c)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (b -> PushM t (Maybe (Maybe a, Maybe c)))
-> Event t b -> Event t (Maybe a, Maybe c)
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push Event t b
e ((b -> PushM t (Maybe (Maybe a, Maybe c)))
 -> Event t (Maybe a, Maybe c))
-> (b -> PushM t (Maybe (Maybe a, Maybe c)))
-> Event t (Maybe a, Maybe c)
forall a b. (a -> b) -> a -> b
$ \b
o -> do
            a
v <- Behavior t a -> PushM t a
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t a
d'
            (Maybe a, Maybe c)
result <- a -> b -> PushM t (Maybe a, Maybe c)
f a
v b
o
            Maybe (Maybe a, Maybe c) -> PushM t (Maybe (Maybe a, Maybe c))
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (Maybe a, Maybe c) -> PushM t (Maybe (Maybe a, Maybe c)))
-> Maybe (Maybe a, Maybe c) -> PushM t (Maybe (Maybe a, Maybe c))
forall a b. (a -> b) -> a -> b
$ case (Maybe a, Maybe c)
result of
              (Maybe a
Nothing, Maybe c
Nothing) -> Maybe (Maybe a, Maybe c)
forall a. Maybe a
Nothing
              (Maybe a, Maybe c)
_ -> (Maybe a, Maybe c) -> Maybe (Maybe a, Maybe c)
forall a. a -> Maybe a
Just (Maybe a, Maybe c)
result
      Behavior t a
d' <- a -> Event t a -> m (Behavior t a)
forall a. a -> Event t a -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a.
MonadHold t m =>
a -> Event t a -> m (Behavior t a)
hold a
z (Event t a -> m (Behavior t a)) -> Event t a -> m (Behavior t a)
forall a b. (a -> b) -> a -> b
$ ((Maybe a, Maybe c) -> Maybe a)
-> Event t (Maybe a, Maybe c) -> Event t a
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (Maybe a, Maybe c) -> Maybe a
forall a b. (a, b) -> a
fst Event t (Maybe a, Maybe c)
e'
  (Behavior t a, Event t c) -> m (Behavior t a, Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (Behavior t a
d', ((Maybe a, Maybe c) -> Maybe c)
-> Event t (Maybe a, Maybe c) -> Event t c
forall a b. (a -> Maybe b) -> Event t a -> Event t b
forall (f :: * -> *) a b.
Filterable f =>
(a -> Maybe b) -> f a -> f b
mapMaybe (Maybe a, Maybe c) -> Maybe c
forall a b. (a, b) -> b
snd Event t (Maybe a, Maybe c)
e')

-- | Accumulate occurrences of an 'Event', producing an output occurrence each
-- time.  Discard the underlying 'Accumulator'.
{-# INLINE mapAccum_ #-}
mapAccum_ :: forall t m a b c. (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> (a, c)) -> a -> Event t b -> m (Event t c)
mapAccum_ :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (a, c)) -> a -> Event t b -> m (Event t c)
mapAccum_ a -> b -> (a, c)
f a
z Event t b
e = do
  (Behavior t a
_, Event t c
result) <- (a -> b -> (a, c)) -> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (a, c)) -> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumB a -> b -> (a, c)
f a
z Event t b
e
  Event t c -> m (Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Event t c
result

-- | Accumulate occurrences of an 'Event', possibly producing an output
-- occurrence each time.  Discard the underlying 'Accumulator'.
{-# INLINE mapAccumMaybe_ #-}
mapAccumMaybe_ :: forall t m a b c. (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> (Maybe a, Maybe c)) -> a -> Event t b -> m (Event t c)
mapAccumMaybe_ :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c)) -> a -> Event t b -> m (Event t c)
mapAccumMaybe_ a -> b -> (Maybe a, Maybe c)
f a
z Event t b
e = do
  (Behavior t a
_, Event t c
result) <- (a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeB a -> b -> (Maybe a, Maybe c)
f a
z Event t b
e
  Event t c -> m (Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Event t c
result

-- | Accumulate occurrences of an 'Event', using a 'PushM' action and producing
-- an output occurrence each time.  Discard the underlying 'Accumulator'.
{-# INLINE mapAccumM_ #-}
mapAccumM_ :: forall t m a b c. (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (a, c)) -> a -> Event t b -> m (Event t c)
mapAccumM_ :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (a, c)) -> a -> Event t b -> m (Event t c)
mapAccumM_ a -> b -> PushM t (a, c)
f a
z Event t b
e = do
  (Behavior t a
_, Event t c
result) <- (a -> b -> PushM t (a, c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (a, c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMB a -> b -> PushM t (a, c)
f a
z Event t b
e
  Event t c -> m (Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Event t c
result

-- | Accumulate occurrences of an 'Event', using a 'PushM' action and possibly
-- producing an output occurrence each time.  Discard the underlying
-- 'Accumulator'.
{-# INLINE mapAccumMaybeM_ #-}
mapAccumMaybeM_ :: forall t m a b c. (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> PushM t (Maybe a, Maybe c)) -> a -> Event t b -> m (Event t c)
mapAccumMaybeM_ :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Event t c)
mapAccumMaybeM_ a -> b -> PushM t (Maybe a, Maybe c)
f a
z Event t b
e = do
  (Behavior t a
_, Event t c
result) <- (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeMB a -> b -> PushM t (Maybe a, Maybe c)
f a
z Event t b
e
  Event t c -> m (Event t c)
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return Event t c
result

instance Reflex t => Accumulator t (Dynamic t) where
  accumMaybeM :: forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
accumMaybeM = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
accumMaybeMDyn
  mapAccumMaybeM :: forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeM = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeMDyn

instance Reflex t => Accumulator t (Behavior t) where
  accumMaybeM :: forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
accumMaybeM = (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
forall {k} (t :: k) (m :: * -> *) a b.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Behavior t a)
accumMaybeMB
  mapAccumMaybeM :: forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeM = (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Behavior t a, Event t c)
mapAccumMaybeMB

instance Reflex t => Accumulator t (Event t) where
  accumMaybeM :: forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Event t a)
accumMaybeM a -> b -> PushM t (Maybe a)
f a
z Event t b
e = Dynamic t a -> Event t a
forall a. Dynamic t a -> Event t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Event t a
updated (Dynamic t a -> Event t a) -> m (Dynamic t a) -> m (Event t a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (f a)
forall (m :: * -> *) a b.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a)) -> a -> Event t b -> m (Dynamic t a)
accumMaybeM a -> b -> PushM t (Maybe a)
f a
z Event t b
e
  mapAccumMaybeM :: forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Event t a, Event t c)
mapAccumMaybeM a -> b -> PushM t (Maybe a, Maybe c)
f a
z Event t b
e = (Dynamic t a -> Event t a)
-> (Dynamic t a, Event t c) -> (Event t a, Event t c)
forall a b c. (a -> b) -> (a, c) -> (b, c)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Dynamic t a -> Event t a
forall a. Dynamic t a -> Event t a
forall {k} (t :: k) a. Reflex t => Dynamic t a -> Event t a
updated ((Dynamic t a, Event t c) -> (Event t a, Event t c))
-> m (Dynamic t a, Event t c) -> m (Event t a, Event t c)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
forall {k} (t :: k) (f :: * -> *) (m :: * -> *) a b c.
(Accumulator t f, MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (f a, Event t c)
forall (m :: * -> *) a b c.
(MonadHold t m, MonadFix m) =>
(a -> b -> PushM t (Maybe a, Maybe c))
-> a -> Event t b -> m (Dynamic t a, Event t c)
mapAccumMaybeM a -> b -> PushM t (Maybe a, Maybe c)
f a
z Event t b
e

-- | Create a new 'Event' by combining each occurrence with the next value of the
-- list using the supplied function. If the list runs out of items, all
-- subsequent 'Event' occurrences will be ignored.
zipListWithEvent :: (Reflex t, MonadHold t m, MonadFix m) => (a -> b -> c) -> [a] -> Event t b -> m (Event t c)
zipListWithEvent :: forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> c) -> [a] -> Event t b -> m (Event t c)
zipListWithEvent a -> b -> c
f [a]
l Event t b
e = do
  let f' :: [a] -> b -> (Maybe [a], Maybe c)
f' [a]
a b
b = case [a]
a of
        a
h:[a]
t -> ([a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
t, c -> Maybe c
forall a. a -> Maybe a
Just (c -> Maybe c) -> c -> Maybe c
forall a b. (a -> b) -> a -> b
$ a -> b -> c
f a
h b
b)
        [a]
_ -> (Maybe [a]
forall a. Maybe a
Nothing, Maybe c
forall a. Maybe a
Nothing) --TODO: Unsubscribe the event?
  ([a] -> b -> (Maybe [a], Maybe c))
-> [a] -> Event t b -> m (Event t c)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (Maybe a, Maybe c)) -> a -> Event t b -> m (Event t c)
mapAccumMaybe_ [a] -> b -> (Maybe [a], Maybe c)
f' [a]
l Event t b
e

-- | Assign a number to each occurrence of the given 'Event', starting from 0
{-# INLINE numberOccurrences #-}
numberOccurrences :: (Reflex t, MonadHold t m, MonadFix m, Num b) => Event t a -> m (Event t (b, a))
numberOccurrences :: forall {k} (t :: k) (m :: * -> *) b a.
(Reflex t, MonadHold t m, MonadFix m, Num b) =>
Event t a -> m (Event t (b, a))
numberOccurrences = b -> Event t a -> m (Event t (b, a))
forall {k} (t :: k) (m :: * -> *) b a.
(Reflex t, MonadHold t m, MonadFix m, Num b) =>
b -> Event t a -> m (Event t (b, a))
numberOccurrencesFrom b
0

-- | Assign a number to each occurrence of the given 'Event'
{-# INLINE numberOccurrencesFrom #-}
numberOccurrencesFrom :: (Reflex t, MonadHold t m, MonadFix m, Num b) => b -> Event t a -> m (Event t (b, a))
numberOccurrencesFrom :: forall {k} (t :: k) (m :: * -> *) b a.
(Reflex t, MonadHold t m, MonadFix m, Num b) =>
b -> Event t a -> m (Event t (b, a))
numberOccurrencesFrom = (b -> a -> (b, (b, a))) -> b -> Event t a -> m (Event t (b, a))
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (a, c)) -> a -> Event t b -> m (Event t c)
mapAccum_ (\b
n a
a -> let !next :: b
next = b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1 in (b
next, (b
n, a
a)))

-- | Assign a number to each occurrence of the given 'Event'; discard the occurrences' values
{-# INLINE numberOccurrencesFrom_ #-}
numberOccurrencesFrom_ :: (Reflex t, MonadHold t m, MonadFix m, Num b) => b -> Event t a -> m (Event t b)
numberOccurrencesFrom_ :: forall {k} (t :: k) (m :: * -> *) b a.
(Reflex t, MonadHold t m, MonadFix m, Num b) =>
b -> Event t a -> m (Event t b)
numberOccurrencesFrom_ = (b -> a -> (b, b)) -> b -> Event t a -> m (Event t b)
forall {k} (t :: k) (m :: * -> *) a b c.
(Reflex t, MonadHold t m, MonadFix m) =>
(a -> b -> (a, c)) -> a -> Event t b -> m (Event t c)
mapAccum_ (\b
n a
_ -> let !next :: b
next = b
n b -> b -> b
forall a. Num a => a -> a -> a
+ b
1 in (b
next, b
n))

-- | This is used to sample the value of a 'Behavior' using an 'Event'.
--
-- The '<@>' operator is intended to be used in conjunction with
-- the 'Applicative' instance for 'Behavior'.
--
-- This is useful when we want to combine the values of one 'Event' and
-- the value of several 'Behavior's at the time the 'Event' is firing.
--
-- If we have:
--
-- > f  :: a -> b -> c -> d
-- > b1 :: Behavior t a
-- > b2 :: Behavior t b
-- > e  :: Event t c
--
-- then we can do:
--
-- > f <$> b1 <*> b2 <@> e :: Event t d
--
-- in order to apply the function 'f' to the relevant values.
--
-- The alternative would be something like:
--
-- > attachWith (\(x1, x2) y -> f x1 x2 y) ((,) <$> b1 <*> b2) e :: Event t d
--
-- or a variation involing a custom data type to hold the combination of
-- 'Behavior's even when that combination might only ever be used by 'f'.
--
-- A more suggestive example might be:
--
-- > handleMouse <$> bPlayerState <*> bMousePosition <@> eMouseClick :: Event t (GameState -> GameState)
--
(<@>) :: Reflex t => Behavior t (a -> b) -> Event t a -> Event t b
<@> :: forall {k} (t :: k) a b.
Reflex t =>
Behavior t (a -> b) -> Event t a -> Event t b
(<@>) Behavior t (a -> b)
b = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
push ((a -> PushM t (Maybe b)) -> Event t a -> Event t b)
-> (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ \a
x -> do
  a -> b
f <- Behavior t (a -> b) -> PushM t (a -> b)
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t (a -> b)
b
  Maybe b -> PushM t (Maybe b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b -> PushM t (Maybe b))
-> (a -> Maybe b) -> a -> PushM t (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> (a -> b) -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f (a -> PushM t (Maybe b)) -> a -> PushM t (Maybe b)
forall a b. (a -> b) -> a -> b
$ a
x
infixl 4 <@>

-- | An version of '<@>' that does not use the value of the 'Event'.
--
-- Alternatively, it is 'tag' in operator form.
--
-- This is useful when we want to combine the values of several
-- 'Behavior's at particular points in time using an 'Applicative'
-- style syntax.
--
-- If we have:
--
-- > g  :: a -> b -> d
-- > b1 :: Behavior t a
-- > b2 :: Behavior t b
-- > e  :: Event t c
--
-- where 'e' is firing at the points in time of interest.
--
-- Then we can use '<@':
--
-- > g <$> b1 <*> b2 <@  e :: Event t d
--
-- to combine the values of 'b1' and 'b2' at each of those points of time,
-- with the function 'g' being used to combine the values.
--
-- This is the same as '<@>' except that the 'Event' is being used only
-- to act as a trigger.
(<@) :: (Reflex t) => Behavior t b -> Event t a -> Event t b
<@ :: forall {k} (t :: k) b a.
Reflex t =>
Behavior t b -> Event t a -> Event t b
(<@) = Behavior t b -> Event t a -> Event t b
forall {k} (t :: k) b a.
Reflex t =>
Behavior t b -> Event t a -> Event t b
tag
infixl 4 <@

------------------
-- Cheap Functions
------------------

-- | A "cheap" version of 'pushAlways'. See the performance note on 'pushCheap'.
{-# INLINE pushAlwaysCheap #-}
pushAlwaysCheap :: Reflex t => (a -> PushM t b) -> Event t a -> Event t b
pushAlwaysCheap :: forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t b) -> Event t a -> Event t b
pushAlwaysCheap a -> PushM t b
f = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
pushCheap ((b -> Maybe b) -> PushM t b -> PushM t (Maybe b)
forall a b. (a -> b) -> PushM t a -> PushM t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap b -> Maybe b
forall a. a -> Maybe a
Just (PushM t b -> PushM t (Maybe b))
-> (a -> PushM t b) -> a -> PushM t (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> PushM t b
f)

-- | A "cheap" version of 'mapMaybe'. See the performance note on 'pushCheap'.
{-# INLINE mapMaybeCheap #-}
mapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
mapMaybeCheap :: forall {k} (t :: k) a b.
Reflex t =>
(a -> Maybe b) -> Event t a -> Event t b
mapMaybeCheap a -> Maybe b
f = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
pushCheap ((a -> PushM t (Maybe b)) -> Event t a -> Event t b)
-> (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ Maybe b -> PushM t (Maybe b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b -> PushM t (Maybe b))
-> (a -> Maybe b) -> a -> PushM t (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe b
f

-- | An alias for 'mapMaybeCheap'
{-# INLINE fmapMaybeCheap #-}
fmapMaybeCheap :: Reflex t => (a -> Maybe b) -> Event t a -> Event t b
fmapMaybeCheap :: forall {k} (t :: k) a b.
Reflex t =>
(a -> Maybe b) -> Event t a -> Event t b
fmapMaybeCheap = (a -> Maybe b) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> Maybe b) -> Event t a -> Event t b
mapMaybeCheap


-- | A "cheap" version of 'fforMaybe'. See the performance note on 'pushCheap'.
{-# INLINE fforMaybeCheap #-}
fforMaybeCheap :: Reflex t => Event t a -> (a -> Maybe b) -> Event t b
fforMaybeCheap :: forall {k} (t :: k) a b.
Reflex t =>
Event t a -> (a -> Maybe b) -> Event t b
fforMaybeCheap = ((a -> Maybe b) -> Event t a -> Event t b)
-> Event t a -> (a -> Maybe b) -> Event t b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> Maybe b) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> Maybe b) -> Event t a -> Event t b
mapMaybeCheap

-- | A "cheap" version of 'ffor'. See the performance note on 'pushCheap'.
{-# INLINE fforCheap #-}
fforCheap :: Reflex t => Event t a -> (a -> b) -> Event t b
fforCheap :: forall {k} (t :: k) a b.
Reflex t =>
Event t a -> (a -> b) -> Event t b
fforCheap = ((a -> b) -> Event t a -> Event t b)
-> Event t a -> (a -> b) -> Event t b
forall a b c. (a -> b -> c) -> b -> a -> c
flip (a -> b) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap

-- | A "cheap" version of 'fmap'. See the performance note on 'pushCheap'.
{-# INLINE fmapCheap #-}
fmapCheap :: Reflex t => (a -> b) -> Event t a -> Event t b
fmapCheap :: forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap a -> b
f = (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> PushM t (Maybe b)) -> Event t a -> Event t b
pushCheap ((a -> PushM t (Maybe b)) -> Event t a -> Event t b)
-> (a -> PushM t (Maybe b)) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ Maybe b -> PushM t (Maybe b)
forall a. a -> PushM t a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe b -> PushM t (Maybe b))
-> (a -> Maybe b) -> a -> PushM t (Maybe b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> Maybe b
forall a. a -> Maybe a
Just (b -> Maybe b) -> (a -> b) -> a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f

-- | A "cheap" version of 'tag'. See the performance note on 'pushCheap'.
{-# INLINE tagCheap #-}
tagCheap :: Reflex t => Behavior t b -> Event t a -> Event t b
tagCheap :: forall {k} (t :: k) b a.
Reflex t =>
Behavior t b -> Event t a -> Event t b
tagCheap Behavior t b
b = (a -> PushM t b) -> Event t a -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> PushM t b) -> Event t a -> Event t b
pushAlwaysCheap ((a -> PushM t b) -> Event t a -> Event t b)
-> (a -> PushM t b) -> Event t a -> Event t b
forall a b. (a -> b) -> a -> b
$ \a
_ -> Behavior t b -> PushM t b
forall a. Behavior t a -> PushM t a
forall {k} (t :: k) (m :: * -> *) a.
MonadSample t m =>
Behavior t a -> m a
sample Behavior t b
b

-- | Merge a collection of events; the resulting 'Event' will only occur if at
-- least one input event is occurring, and will contain all of the input keys
-- that are occurring simultaneously
merge :: (Reflex t, GCompare k) => DMap k (Event t) -> Event t (DMap k Identity)
merge :: forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
DMap k (Event t) -> Event t (DMap k Identity)
merge = (forall a. Event t a -> Event t (Identity a))
-> DMap k (Event t) -> Event t (DMap k Identity)
forall {k} (t :: k) {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
(forall (a :: k). q a -> Event t (v a))
-> DMap k q -> Event t (DMap k v)
forall {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
GCompare k =>
(forall (a :: k). q a -> Event t (v a))
-> DMap k q -> Event t (DMap k v)
mergeG Event t a -> Event t (Identity a)
forall a. Event t a -> Event t (Identity a)
forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Event t a -> Event t b
coerceEvent
{-# INLINE merge #-}

-- | Create a merge whose parents can change over time
mergeIncremental :: (Reflex t, GCompare k)
  => Incremental t (PatchDMap k (Event t)) -> Event t (DMap k Identity)
mergeIncremental :: forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Incremental t (PatchDMap k (Event t)) -> Event t (DMap k Identity)
mergeIncremental = (forall a. Event t a -> Event t (Identity a))
-> Incremental t (PatchDMap k (Event t))
-> Event t (DMap k Identity)
forall {k} (t :: k) {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
(forall (a :: k). q a -> Event t (v a))
-> Incremental t (PatchDMap k q) -> Event t (DMap k v)
forall {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
GCompare k =>
(forall (a :: k). q a -> Event t (v a))
-> Incremental t (PatchDMap k q) -> Event t (DMap k v)
mergeIncrementalG Event t a -> Event t (Identity a)
forall a. Event t a -> Event t (Identity a)
forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Event t a -> Event t b
coerceEvent

-- | Experimental: Create a merge whose parents can change over time; changing the key of an Event is more efficient than with mergeIncremental
mergeIncrementalWithMove :: (Reflex t, GCompare k)
  => Incremental t (PatchDMapWithMove k (Event t)) -> Event t (DMap k Identity)
mergeIncrementalWithMove :: forall {k} (t :: k) (k :: * -> *).
(Reflex t, GCompare k) =>
Incremental t (PatchDMapWithMove k (Event t))
-> Event t (DMap k Identity)
mergeIncrementalWithMove = (forall a. Event t a -> Event t (Identity a))
-> Incremental t (PatchDMapWithMove k (Event t))
-> Event t (DMap k Identity)
forall {k} (t :: k) {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
(Reflex t, GCompare k) =>
(forall (a :: k). q a -> Event t (v a))
-> Incremental t (PatchDMapWithMove k q) -> Event t (DMap k v)
forall {k} (k :: k -> *) (q :: k -> *) (v :: k -> *).
GCompare k =>
(forall (a :: k). q a -> Event t (v a))
-> Incremental t (PatchDMapWithMove k q) -> Event t (DMap k v)
mergeIncrementalWithMoveG Event t a -> Event t (Identity a)
forall a. Event t a -> Event t (Identity a)
forall {k} (t :: k) a b.
(Reflex t, Coercible a b) =>
Event t a -> Event t b
coerceEvent

-- | A "cheap" version of 'mergeWithCheap'. See the performance note on 'pushCheap'.
{-# INLINE mergeWithCheap #-}
mergeWithCheap :: Reflex t => (a -> a -> a) -> [Event t a] -> Event t a
mergeWithCheap :: forall {k} (t :: k) a.
Reflex t =>
(a -> a -> a) -> [Event t a] -> Event t a
mergeWithCheap = (a -> a) -> (a -> a -> a) -> [Event t a] -> Event t a
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
mergeWithCheap' a -> a
forall a. a -> a
id

-- | A "cheap" version of 'mergeWithCheap''. See the performance note on 'pushCheap'.
{-# INLINE mergeWithCheap' #-}
mergeWithCheap' :: Reflex t => (a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
mergeWithCheap' :: forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> (b -> b -> b) -> [Event t a] -> Event t b
mergeWithCheap' a -> b
f b -> b -> b
g = (NonEmpty a -> b) -> [Event t a] -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(NonEmpty a -> b) -> [Event t a] -> Event t b
mergeWithFoldCheap' ((NonEmpty a -> b) -> [Event t a] -> Event t b)
-> (NonEmpty a -> b) -> [Event t a] -> Event t b
forall a b. (a -> b) -> a -> b
$ (b -> b -> b) -> NonEmpty b -> b
forall a. (a -> a -> a) -> NonEmpty a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldl1 b -> b -> b
g (NonEmpty b -> b) -> (NonEmpty a -> NonEmpty b) -> NonEmpty a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> NonEmpty a -> NonEmpty b
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f

-- | A "cheap" version of 'mergeWithFoldCheap''. See the performance note on 'pushCheap'.
{-# INLINE mergeWithFoldCheap' #-}
mergeWithFoldCheap' :: Reflex t => (NonEmpty a -> b) -> [Event t a] -> Event t b
mergeWithFoldCheap' :: forall {k} (t :: k) a b.
Reflex t =>
(NonEmpty a -> b) -> [Event t a] -> Event t b
mergeWithFoldCheap' NonEmpty a -> b
f [Event t a]
es =
  (IntMap a -> b) -> Event t (IntMap a) -> Event t b
forall {k} (t :: k) a b.
Reflex t =>
(a -> b) -> Event t a -> Event t b
fmapCheap (NonEmpty a -> b
f (NonEmpty a -> b) -> (IntMap a -> NonEmpty a) -> IntMap a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\(a
h : [a]
t) -> a
h a -> [a] -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
:| [a]
t) ([a] -> NonEmpty a) -> (IntMap a -> [a]) -> IntMap a -> NonEmpty a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap a -> [a]
forall a. IntMap a -> [a]
IntMap.elems)
  (Event t (IntMap a) -> Event t b)
-> ([(Int, Event t a)] -> Event t (IntMap a))
-> [(Int, Event t a)]
-> Event t b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IntMap (Event t a) -> Event t (IntMap a)
forall {k} (t :: k) a.
Reflex t =>
IntMap (Event t a) -> Event t (IntMap a)
mergeInt
  (IntMap (Event t a) -> Event t (IntMap a))
-> ([(Int, Event t a)] -> IntMap (Event t a))
-> [(Int, Event t a)]
-> Event t (IntMap a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, Event t a)] -> IntMap (Event t a)
forall a. [(Int, a)] -> IntMap a
IntMap.fromDistinctAscList
  ([(Int, Event t a)] -> Event t b)
-> [(Int, Event t a)] -> Event t b
forall a b. (a -> b) -> a -> b
$ [Int] -> [Event t a] -> [(Int, Event t a)]
forall a b. [a] -> [b] -> [(a, b)]
forall (f :: * -> *) a b. Zip f => f a -> f b -> f (a, b)
zip [Int
0 :: Int ..] [Event t a]
es

--------------------------------------------------------------------------------
-- Deprecated functions
--------------------------------------------------------------------------------

{-# DEPRECATED switchPromptly "Use 'switchHoldPromptly' instead. The 'switchHold*' naming convention was chosen because those functions are more closely related to each other than they are to 'switch'. " #-}
-- | See 'switchHoldPromptly'
switchPromptly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
switchPromptly :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchPromptly = Event t a -> Event t (Event t a) -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptly
{-# DEPRECATED switchPromptOnly "Use 'switchHoldPromptOnly' instead. The 'switchHold*' naming convention was chosen because those functions are more closely related to each other than they are to 'switch'. " #-}
-- | See 'switchHoldPromptOnly'
switchPromptOnly :: (Reflex t, MonadHold t m) => Event t a -> Event t (Event t a) -> m (Event t a)
switchPromptOnly :: forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchPromptOnly = Event t a -> Event t (Event t a) -> m (Event t a)
forall {k} (t :: k) (m :: * -> *) a.
(Reflex t, MonadHold t m) =>
Event t a -> Event t (Event t a) -> m (Event t a)
switchHoldPromptOnly