on master help sign in
Optimize fmap by making a custom functor instance
9435cdcHarendra Kumar
src/Asyncly/Streams.hs+22−7
@@ -87,7 +87,7 @@ module Asyncly.Streams
87where
87where
88
88
89import Control.Applicative (Alternative (..), liftA2)
89import Control.Applicative (Alternative (..), liftA2)
90−import Control.Monad (MonadPlus(..), ap, liftM)
90+import Control.Monad (MonadPlus(..), ap)
91import Control.Monad.Base (MonadBase (..))
91import Control.Monad.Base (MonadBase (..))
92import Control.Monad.Catch (MonadThrow)
92import Control.Monad.Catch (MonadThrow)
93import Control.Monad.Error.Class (MonadError(..))
93import Control.Monad.Error.Class (MonadError(..))
@@ -285,7 +285,11 @@ instance Monad m => Applicative (StreamT m) where
285------------------------------------------------------------------------------
285------------------------------------------------------------------------------
286
286
287instance Monad m => Functor (StreamT m) where
287instance Monad m => Functor (StreamT m) where
288− fmap = liftM
288+ fmap f (StreamT (Stream m)) = StreamT $ Stream $ \_ stp yld ->
289+ let yield a Nothing = yld (f a) Nothing
290+ yield a (Just r) = yld (f a)
291+ (Just (getStreamT (fmap f (StreamT r))))
292+ in m Nothing stp yield
289
293
290------------------------------------------------------------------------------
294------------------------------------------------------------------------------
291-- Num
295-- Num
@@ -379,7 +383,11 @@ instance Monad m => Applicative (InterleavedT m) where
379------------------------------------------------------------------------------
383------------------------------------------------------------------------------
380
384
381instance Monad m => Functor (InterleavedT m) where
385instance Monad m => Functor (InterleavedT m) where
382− fmap = liftM
386+ fmap f (InterleavedT (Stream m)) = InterleavedT $ Stream $ \_ stp yld ->
387+ let yield a Nothing = yld (f a) Nothing
388+ yield a (Just r) =
389+ yld (f a) (Just (getInterleavedT (fmap f (InterleavedT r))))
390+ in m Nothing stp yield
383
391
384------------------------------------------------------------------------------
392------------------------------------------------------------------------------
385-- Num
393-- Num
@@ -489,8 +497,11 @@ instance MonadAsync m => Applicative (AsyncT m) where
489-- Functor
497-- Functor
490------------------------------------------------------------------------------
498------------------------------------------------------------------------------
491
499
492−instance MonadAsync m => Functor (AsyncT m) where
500+instance Monad m => Functor (AsyncT m) where
493− fmap = liftM
501+ fmap f (AsyncT (Stream m)) = AsyncT $ Stream $ \_ stp yld ->
502+ let yield a Nothing = yld (f a) Nothing
503+ yield a (Just r) = yld (f a) (Just (getAsyncT (fmap f (AsyncT r))))
504+ in m Nothing stp yield
494
505
495------------------------------------------------------------------------------
506------------------------------------------------------------------------------
496-- Num
507-- Num
@@ -585,8 +596,12 @@ instance MonadAsync m => Applicative (ParallelT m) where
585-- Functor
596-- Functor
586------------------------------------------------------------------------------
597------------------------------------------------------------------------------
587
598
588−instance MonadAsync m => Functor (ParallelT m) where
599+instance Monad m => Functor (ParallelT m) where
589− fmap = liftM
600+ fmap f (ParallelT (Stream m)) = ParallelT $ Stream $ \_ stp yld ->
601+ let yield a Nothing = yld (f a) Nothing
602+ yield a (Just r) = yld (f a)
603+ (Just (getParallelT (fmap f (ParallelT r))))
604+ in m Nothing stp yield
590
605
591------------------------------------------------------------------------------
606------------------------------------------------------------------------------
592-- Num
607-- Num