event 0.1.2.1 → 0.1.3
raw patch · 3 files changed
+32/−3 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Control.Concurrent.Event: filterE :: (a -> Bool) -> Event a -> Event a
+ Control.Concurrent.Event: foldrE :: (b -> a -> b) -> b -> Event a -> Event b
Files
- CHANGELOG.md +5/−0
- event.cabal +1/−1
- src/Control/Concurrent/Event.hs +26/−2
CHANGELOG.md view
@@ -1,3 +1,8 @@+### 0.1.3++- Added foldrE.+- Added filterE.+ ### 0.1.2.1 - Added support for semigroups-0.18.
event.cabal view
@@ -1,5 +1,5 @@ name: event-version: 0.1.2.1+version: 0.1.3 synopsis: Monoidal, monadic and first-class events description: This package can be used to represent events as first-class objects instead of deepening callbacks and
src/Control/Concurrent/Event.hs view
@@ -56,9 +56,12 @@ -- * Triggering events , Trigger(..) , trigger+ -- * Event combinators+ , filterE+ , foldrE ) where -import Control.Monad ( ap )+import Control.Monad ( ap, when ) import Control.Monad.IO.Class ( MonadIO(..) ) import Data.Foldable ( traverse_ ) import Data.IntMap as M@@ -72,7 +75,7 @@ -- -- 'Event's can be triggered with the 'trigger' function and the associated -- type 'Trigger'.-newtype Event a = Event ((a -> IO ()) -> IO Detach)+newtype Event a = Event { unEvent :: (a -> IO ()) -> IO Detach } instance Applicative Event where pure x = Event $ \k -> k x >> pure mempty@@ -139,3 +142,24 @@ writeIORef hRef (succ h) pure . Detach . modifyIORef callbacksRef $ delete h register ref = Trigger $ \a -> liftIO $ readIORef ref >>= traverse_ ($ a)++-- |Filter an 'Event' with a predicate.+filterE :: (a -> Bool) -> Event a -> Event a+filterE predicate e = Event $ \k -> do+ (filtered,trig) <- newEvent+ _ <- on e $ \a -> when (predicate a) (trigger trig a)+ unEvent filtered k++-- |Right fold an 'Event'. Each time an event occur, the function folding function is applied and+-- the result is passed to the future 'Event'.+foldrE :: (b -> a -> b) -> b -> Event a -> Event b+foldrE f b e = Event $ \k -> do+ (folded,trig) <- newEvent+ ref <- newIORef b+ _ <- on e $ \a -> do+ acc <- readIORef ref+ let x = f acc a+ writeIORef ref x+ trigger trig x+ unEvent folded k+