on master help sign in
Optimize fmap by making a custom functor instance
commit9435cdcd1f765abaf183c6c1790529a0adbd3835 authorHarendra Kumar <harendra.kumar@gmail.com> dateThu 2 Nov 2017, 11:55 (9 years ago) parent62336fb
1 file changed+22−7
src/Asyncly/Streams.hs+22−7
src/Asyncly/Streams.hs+22−7
@@ -87,7 +87,7 @@ module Asyncly.Streams
8787where
8888
8989import Control.Applicative (Alternative (..), liftA2)
90−import Control.Monad (MonadPlus(..), ap, liftM)
90+import Control.Monad (MonadPlus(..), ap)
9191import Control.Monad.Base (MonadBase (..))
9292import Control.Monad.Catch (MonadThrow)
9393import Control.Monad.Error.Class (MonadError(..))
@@ -285,7 +285,11 @@ instance Monad m => Applicative (StreamT m) where
285285------------------------------------------------------------------------------
286286
287287instance 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
289293
290294------------------------------------------------------------------------------
291295-- Num
@@ -379,7 +383,11 @@ instance Monad m => Applicative (InterleavedT m) where
379383------------------------------------------------------------------------------
380384
381385instance 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
383391
384392------------------------------------------------------------------------------
385393-- Num
@@ -489,8 +497,11 @@ instance MonadAsync m => Applicative (AsyncT m) where
489497-- Functor
490498------------------------------------------------------------------------------
491499
492−instance MonadAsync m => Functor (AsyncT m) where
493− fmap = liftM
500+instance Monad m => Functor (AsyncT m) where
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
494505
495506------------------------------------------------------------------------------
496507-- Num
@@ -585,8 +596,12 @@ instance MonadAsync m => Applicative (ParallelT m) where
585596-- Functor
586597------------------------------------------------------------------------------
587598
588−instance MonadAsync m => Functor (ParallelT m) where
589− fmap = liftM
599+instance Monad m => Functor (ParallelT m) where
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
590605
591606------------------------------------------------------------------------------
592607-- Num