mirror of
https://github.com/composewell/streamly.git
synced 2024-11-10 12:47:22 +03:00
Add mapM_ benchmark, add build/augment
This commit is contained in:
parent
a30ef3eef9
commit
9c8a7504fa
@ -165,6 +165,7 @@ main =
|
||||
]
|
||||
, bgroup "elimination"
|
||||
[ benchIO "toNull" K.toNull K.sourceUnfoldrM
|
||||
, benchIO "mapM_" K.mapM_ K.sourceUnfoldrM
|
||||
, benchIO "uncons" K.uncons K.sourceUnfoldrM
|
||||
, benchFold "init" K.init K.sourceUnfoldrM
|
||||
, benchFold "tail" K.tail K.sourceUnfoldrM
|
||||
|
@ -3,7 +3,9 @@
|
||||
-- at GHC generated code for optimizing specific problematic cases.
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
{-# LANGUAGE FlexibleContexts #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
import Streamly.SVar (MonadAsync)
|
||||
import qualified Streamly.Streams.StreamK as S
|
||||
import Gauge
|
||||
import System.Random
|
||||
@ -12,7 +14,7 @@ maxValue :: Int
|
||||
maxValue = 100000
|
||||
|
||||
{-# INLINE sourceUnfoldrM #-}
|
||||
sourceUnfoldrM :: Monad m => S.Stream m Int
|
||||
sourceUnfoldrM :: MonadAsync m => S.Stream m Int
|
||||
sourceUnfoldrM = S.unfoldrM step 0
|
||||
where
|
||||
step cnt =
|
||||
@ -21,7 +23,7 @@ sourceUnfoldrM = S.unfoldrM step 0
|
||||
else return (Just (cnt, cnt + 1))
|
||||
|
||||
{-# INLINE sourceUnfoldrMN #-}
|
||||
sourceUnfoldrMN :: Monad m => Int -> S.Stream m Int
|
||||
sourceUnfoldrMN :: MonadAsync m => Int -> S.Stream m Int
|
||||
sourceUnfoldrMN n = S.unfoldrM step n
|
||||
where
|
||||
step cnt =
|
||||
@ -74,7 +76,7 @@ dropWhileFalseX4 = S.runStream
|
||||
|
||||
{-# INLINE iterateSource #-}
|
||||
iterateSource
|
||||
:: Monad m
|
||||
:: MonadAsync m
|
||||
=> (S.Stream m Int -> S.Stream m Int) -> Int -> Int -> S.Stream m Int
|
||||
iterateSource g i n = f i (sourceUnfoldrMN n)
|
||||
where
|
||||
|
@ -14,7 +14,7 @@ import Control.Monad (when)
|
||||
import Data.Maybe (isJust)
|
||||
import Prelude
|
||||
(Monad, Int, (+), ($), (.), return, even, (>), (<=), div,
|
||||
subtract, undefined, Maybe(..), not, mapM_, (>>=),
|
||||
subtract, undefined, Maybe(..), not, (>>=),
|
||||
maxBound)
|
||||
import qualified Prelude as P
|
||||
|
||||
@ -116,6 +116,10 @@ source = sourceUnfoldrM
|
||||
runStream :: Monad m => Stream m a -> m ()
|
||||
runStream = S.runStream
|
||||
|
||||
{-# INLINE mapM_ #-}
|
||||
mapM_ :: Monad m => Stream m a -> m ()
|
||||
mapM_ = S.mapM_ (\_ -> return ())
|
||||
|
||||
toNull = runStream
|
||||
uncons s = do
|
||||
r <- S.uncons s
|
||||
@ -127,19 +131,19 @@ uncons s = do
|
||||
init :: (Monad m, S.IsStream t) => t m a -> m ()
|
||||
init s = do
|
||||
t <- S.init s
|
||||
mapM_ S.runStream t
|
||||
P.mapM_ S.runStream t
|
||||
|
||||
{-# INLINE tail #-}
|
||||
tail :: (Monad m, S.IsStream t) => t m a -> m ()
|
||||
tail s = S.tail s >>= mapM_ tail
|
||||
tail s = S.tail s >>= P.mapM_ tail
|
||||
|
||||
nullTail s = do
|
||||
r <- S.null s
|
||||
when (not r) $ S.tail s >>= mapM_ nullTail
|
||||
when (not r) $ S.tail s >>= P.mapM_ nullTail
|
||||
|
||||
headTail s = do
|
||||
h <- S.head s
|
||||
when (isJust h) $ S.tail s >>= mapM_ headTail
|
||||
when (isJust h) $ S.tail s >>= P.mapM_ headTail
|
||||
|
||||
toList = S.toList
|
||||
foldl = S.foldl' (+) 0
|
||||
|
@ -9,6 +9,8 @@
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
{-# LANGUAGE UndecidableInstances #-} -- XXX
|
||||
|
||||
#include "inline.hs"
|
||||
|
||||
-- |
|
||||
-- Module : Streamly.Streams.StreamK
|
||||
-- Copyright : (c) 2017 Harendra Kumar
|
||||
@ -227,14 +229,30 @@ uncons m =
|
||||
-- Generation
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
{-# INLINE_NORMAL build #-}
|
||||
build :: IsStream t => forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a
|
||||
build g = g cons nil
|
||||
|
||||
{-# INLINE_NORMAL _augment #-}
|
||||
_augment
|
||||
:: IsStream t
|
||||
=> forall a. (forall b. (a -> b -> b) -> b -> b) -> t m a -> t m a
|
||||
_augment g xs = g cons xs
|
||||
|
||||
{-# INLINE_NORMAL _buildM #-}
|
||||
_buildM
|
||||
:: (IsStream t, MonadAsync m)
|
||||
=> forall a. ((m a -> t m a -> t m a) -> t m a -> t m a) -> t m a
|
||||
_buildM g = g consM nil
|
||||
|
||||
{-# INLINE unfoldr #-}
|
||||
unfoldr :: IsStream t => (b -> Maybe (a, b)) -> b -> t m a
|
||||
unfoldr step = go
|
||||
where
|
||||
go s = mkStream $ \_ yld _ stp ->
|
||||
case step s of
|
||||
Nothing -> stp
|
||||
Just (a, b) -> yld a (go b)
|
||||
unfoldr step b0 = build $ \cns nl ->
|
||||
let go s =
|
||||
case step s of
|
||||
Just (a, b) -> a `cns` go b
|
||||
Nothing -> nl
|
||||
in go b0
|
||||
|
||||
{-# INLINE unfoldrM #-}
|
||||
unfoldrM :: (IsStream t, MonadAsync m) => (b -> m (Maybe (a, b))) -> b -> t m a
|
||||
@ -405,6 +423,9 @@ foldlM' step begin = foldxM step (return begin) return
|
||||
-- Specialized folds
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- |
|
||||
-- > runStream = foldl' (\_ _ -> ()) ()
|
||||
-- > runStream = mapM_ (\_ -> return ())
|
||||
{-# INLINE runStream #-}
|
||||
runStream :: (Monad m, IsStream t) => t m a -> m ()
|
||||
runStream = go
|
||||
@ -642,6 +663,7 @@ findIndices p = go 0
|
||||
|
||||
-- | Apply a monadic action to each element of the stream and discard the
|
||||
-- output of the action.
|
||||
{-# INLINE mapM_ #-}
|
||||
mapM_ :: (IsStream t, Monad m) => (a -> m b) -> t m a -> m ()
|
||||
mapM_ f m = go m
|
||||
where
|
||||
|
Loading…
Reference in New Issue
Block a user