You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Applicative and Alternative instances for Async #90
I think it might be useful for some people to have both of them fit those instances, as some people (me included) prefer to use Async sometimes and Concurrent other times.
I was thinking about a potential way to implement those instances for Async, without modifying much of the previous code, could be:
Move the current definition of Async and related functions to an Internal package, then I think it could be done, keeping the same semantics, with something like:
module Control.Concurrent.Async
( ...
) where
import Control.Concurrent.Async.Internal as Internal
data Async :: * -> * where
BasicAsync :: Internal.Async a -> Async a
ConcurrentAsync :: Async (a -> b) -> Async a -> Async b
AlternativeAsync :: Async a -> Async a -> Async a
EmptyAsync :: -> Async a
PureAsync :: a -> Async a
instance Applicative Async where
(<*>) = ConcurrentAsync
pure = PureAsync
instance Alternative Async where
(<|>) = AlternativeAsync
empty = EmptyAsync
async :: IO a -> IO (Async a)
async = fmap BasicAsync . Internal.async
cancel :: Async a -> IO ()
cancel = \case
BasicAsync x -> Internal.cancel x
ConcurrentAsync x y -> Internal.cancel x >> Internal.cancel y
AlternativeAsync x y -> Internal.cancel x >> Internal.cancel y
EmptyAsync -> return ()
PureAsync _ -> return ()
.... and so on with the rest of the methods
One thing I like about an Alternative instance for Async over an Alternative instance for Concurrently, is the fact that with Async<*> distributes over <|>. IMO nice to have both.
Activity
cohei commentedon Oct 25, 2018
Concurrentlyis expected to use for those instances, but I'm not sure.vwwv commentedon Oct 25, 2018
I think it might be useful for some people to have both of them fit those instances, as some people (me included) prefer to use
Asyncsometimes andConcurrentother times.I was thinking about a potential way to implement those instances for
Async, without modifying much of the previous code, could be:Asyncand related functions to anInternalpackage, then I think it could be done, keeping the same semantics, with something like:What do you think?
vwwv commentedon Oct 25, 2018
I'll send an actual PR so you can check if it is ok.
simonmar commentedon Oct 26, 2018
I'm not sure I understand the motivation for this, and it adds a lot of complexity. Is there somem reason you can't use
Concurrently?LukaJCB commentedon Nov 6, 2018
One thing I like about an
Alternativeinstance forAsyncover anAlternativeinstance forConcurrently, is the fact that withAsync<*>distributes over<|>. IMO nice to have both.