@@ -23,13 +23,14 @@ module Asyncly.AsyncT
24 , interleave
24 , interleave
28 , foldWith
30 , foldWith
29 , foldMapWith
31 , foldMapWith
30 , forEachWith
32 , forEachWith
31−-- , async
33+ , fromCallback
@@ -490,36 +491,6 @@ instance MonadRecorder m => MonadRecorder (AsyncT m) where
490 putJournal = lift . putJournal
491 putJournal = lift . putJournal
491 play = lift . play
492 play = lift . play
493−------------------------------------------------------------------------------
495−------------------------------------------------------------------------------
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.
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.
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.
516−async :: Monad m => AsyncT m a -> AsyncT m a
517−async action = AsyncT $ runAsyncTask True (runAsyncT action)
519−makeAsync :: Monad m => ((a -> m ()) -> m ()) -> AsyncT m a
520−makeAsync = AsyncT . makeCont
523------------------------------------------------------------------------------
494------------------------------------------------------------------------------
524-- Running the monad
495-- Running the monad
525------------------------------------------------------------------------------
496------------------------------------------------------------------------------
@@ -570,11 +541,18 @@ foldWith f = foldr f mempty
570{-# INLINABLE foldMapWith #-}
541{-# INLINABLE foldMapWith #-}
571foldMapWith :: (Monoid b, Foldable t) =>
542foldMapWith :: (Monoid b, Foldable t) =>
572 (b1 -> b -> b) -> (a -> b1) -> t a -> b
543 (b1 -> b -> b) -> (a -> b1) -> t a -> b
573−foldMapWith ff mf = foldr (ff . mf) mempty
544+foldMapWith f g = foldr (f . g) mempty
575-- | Fold a 'Foldable' container using a function that is a compostioin of the
546-- | Fold a 'Foldable' container using a function that is a compostioin of the
576-- first and the third argument.
547-- first and the third argument.
577{-# INLINABLE forEachWith #-}
548{-# INLINABLE forEachWith #-}
578forEachWith :: (Monoid b, Foldable t) =>
549forEachWith :: (Monoid b, Foldable t) =>
579 (b1 -> b -> b) -> t a -> (a -> b1) -> b
550 (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
553+------------------------------------------------------------------------------
554+-- Convert a callback into an 'AsyncT' computation
555+------------------------------------------------------------------------------
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)