Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added ReactiveUtils
- Loading branch information
Andreas-Christoph Bernstein
committed
Jul 26, 2012
1 parent
349fcb4
commit 258e17a
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
{-# LANGUAGE ExistentialQuantification | ||
, ScopedTypeVariables | ||
, MultiParamTypeClasses | ||
, FlexibleInstances | ||
, TypeFamilies | ||
#-} | ||
|
||
----------------------------------------------------------------------------- | ||
-- | | ||
-- Module : ReactiveUtils | ||
-- Copyright : (c) 2012 Andreas-C. Bernstein | ||
-- License : BSD-style (see LICENSE) | ||
-- Maintainer : andreas.bernstein@gmail.com | ||
-- | ||
-- Reactive banana utilities. Most of them very similar to the ones from | ||
-- the Reactive library. | ||
-- | ||
----------------------------------------------------------------------------- | ||
|
||
module ReactiveUtils | ||
( | ||
integral | ||
, sumB | ||
, withPrevE | ||
, withPrevEWith | ||
, diffE | ||
, snapshotWith | ||
, snapshot | ||
, snapshot_ | ||
, unique | ||
, once | ||
) where | ||
|
||
import Reactive.Banana | ||
import Data.VectorSpace | ||
import Data.AffineSpace | ||
import Data.Active (fromDuration, Time) | ||
|
||
integral :: (VectorSpace v, Scalar v ~ Double) => Event t Time -> Behavior t v -> Behavior t v | ||
integral t b = sumB (snapshotWith (*^) b (fromDuration <$> diffE t)) | ||
|
||
sumB :: AdditiveGroup a => Event t a -> Behavior t a | ||
sumB = accumB zeroV . fmap (^+^) | ||
|
||
withPrevE :: Event t a -> Event t (a,a) | ||
withPrevE = filterJust . fmap f . accumE (Nothing,Nothing) . fmap ((\new (prev,_) -> (new,prev)).Just) | ||
where | ||
f :: (Maybe a, Maybe b) -> Maybe (a,b) | ||
f = uncurry (liftA2 (,)) | ||
|
||
withPrevEWith :: (a -> a -> b) -> Event t a -> Event t b | ||
withPrevEWith f e = fmap (uncurry f) (withPrevE e) | ||
|
||
diffE :: AffineSpace a => Event t a -> Event t (Diff a) | ||
diffE = withPrevEWith (.-.) | ||
|
||
snapshotWith :: (a -> b -> c) -> Behavior t b -> Event t a -> Event t c | ||
snapshotWith h b e = (flip h <$> b) `apply` e | ||
|
||
snapshot :: Behavior t b -> Event t a -> Event t (a,b) | ||
snapshot = snapshotWith (,) | ||
|
||
snapshot_ :: Behavior t b -> Event t a -> Event t b | ||
snapshot_ = snapshotWith (flip const) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
unique :: Eq a => Event t a -> Event t a | ||
unique = filterJust . accumE Nothing . fmap (\a acc -> if Just a == acc then Nothing else Just a) | ||
|
||
once :: Event t a -> Event t a | ||
once e = whenE (True `stepper` (False <$ e)) e |
The infix functions
<@
and<@>
offered by reactive-banana are probably an improvement over thesnapshot
family of functions.