on master help sign in
add API to convert a callback to AsyncT
commit5c34cc6d78bc3afaaf2a41c963505778bcc431a3 authorHarendra Kumar <harendra.kumar@gmail.com> dateTue 29 Aug 2017, 11:19 (9 years ago) parentc81684d
1 file changed+12−34
src/Asyncly/AsyncT.hs+12−34
src/Asyncly/AsyncT.hs+12−34
@@ -23,13 +23,14 @@ module Asyncly.AsyncT
2323 , toList
2424 , interleave
2525 , (<=>)
26+ , parAhead
27+ , (<>>)
2628 , parLeft
2729 , (|>)
2830 , foldWith
2931 , foldMapWith
3032 , forEachWith
31−-- , async
32−-- , makeAsync
33+ , fromCallback
3334 )
3435where
3536
@@ -490,36 +491,6 @@ instance MonadRecorder m => MonadRecorder (AsyncT m) where
490491 putJournal = lift . putJournal
491492 play = lift . play
492493
493−------------------------------------------------------------------------------
494−-- Async primitives
495−------------------------------------------------------------------------------
496−--
497−-- Only those actions that are marked with 'async' are guaranteed to be
498−-- asynchronous. Asyncly is free to run other actions synchronously or
499−-- asynchronously and it should not matter to the semantics of the program, if
500−-- it does then use async to force.
501−--
502−-- Why not make async as default and ask the programmer to use a 'sync'
503−-- primitive to force an action to run synchronously? But then we would not
504−-- have the freedom to convert the async actions to sync dynamically. Note that
505−-- it is safe to convert a sync action to async but vice-versa is not true.
506−-- Converting an async to sync can cause change in semantics if the async
507−-- action was an infinite loop for example.
508−--
509−-- | In an 'Alternative' composition, force the action to run asynchronously.
510−-- The @\<|\>@ operator implies "can be parallel", whereas 'async' implies
511−-- "must be parallel". Note that outside an 'Alternative' composition 'async'
512−-- is not useful and should not be used. Even in an 'Alternative' composition
513−-- 'async' is not useful as the last action as the last action always runs in
514−-- the current thread.
515−{-
516−async :: Monad m => AsyncT m a -> AsyncT m a
517−async action = AsyncT $ runAsyncTask True (runAsyncT action)
518−
519−makeAsync :: Monad m => ((a -> m ()) -> m ()) -> AsyncT m a
520−makeAsync = AsyncT . makeCont
521−-}
522−
523494------------------------------------------------------------------------------
524495-- Running the monad
525496------------------------------------------------------------------------------
@@ -570,11 +541,18 @@ foldWith f = foldr f mempty
570541{-# INLINABLE foldMapWith #-}
571542foldMapWith :: (Monoid b, Foldable t) =>
572543 (b1 -> b -> b) -> (a -> b1) -> t a -> b
573−foldMapWith ff mf = foldr (ff . mf) mempty
544+foldMapWith f g = foldr (f . g) mempty
574545
575546-- | Fold a 'Foldable' container using a function that is a compostioin of the
576547-- first and the third argument.
577548{-# INLINABLE forEachWith #-}
578549forEachWith :: (Monoid b, Foldable t) =>
579550 (b1 -> b -> b) -> t a -> (a -> b1) -> b
580−forEachWith ff xs mf = foldr (ff . mf) mempty xs
551+forEachWith f xs g = foldr (f . g) mempty xs
552+
553+------------------------------------------------------------------------------
554+-- Convert a callback into an 'AsyncT' computation
555+------------------------------------------------------------------------------
556+
557+fromCallback :: Monad m => (forall r. (a -> m r) -> m r) -> AsyncT m a
558+fromCallback k = AsyncT $ \ctx _ yld -> k (\a -> yld a ctx Nothing)