on master help sign in
Fix formatting and docs
cd3ce3eHarendra Kumar
benchmark/Streamly/Benchmark/Data/Fold.hs+7−17
@@ -11,22 +11,16 @@ module Main (main) where
11
11
12import Control.DeepSeq (NFData(..))
12import Control.DeepSeq (NFData(..))
13import Data.Monoid (Last(..), Sum(..))
13import Data.Monoid (Last(..), Sum(..))
14−
15import System.Random (randomRIO)
14import System.Random (randomRIO)
16−import Prelude (IO, Int, Double, String, (>), (<*>), (<$>), (+), ($),
17− (<=), Monad(..), (==), Maybe(..), (.), fromIntegral,
18− compare, (>=), concat, seq, Either(..), odd)
19−
20−import qualified Streamly.Prelude as S
21−import qualified Streamly.Internal.Data.Fold as FL
22−import qualified Streamly.Internal.Data.Pipe as Pipe
23−
24−import qualified Streamly.Internal.Data.Sink as Sink
25
15
26import qualified Streamly.Data.Array.Storable.Foreign as A
16import qualified Streamly.Data.Array.Storable.Foreign as A
17+import qualified Streamly.Data.Fold as FL
27import qualified Streamly.Internal.Data.Array.Storable.Foreign as IA
18import qualified Streamly.Internal.Data.Array.Storable.Foreign as IA
28import qualified Streamly.Internal.Data.Fold as IFL
19import qualified Streamly.Internal.Data.Fold as IFL
20+import qualified Streamly.Internal.Data.Pipe as Pipe
21+import qualified Streamly.Internal.Data.Sink as Sink
29import qualified Streamly.Internal.Data.Stream.IsStream as IP
22import qualified Streamly.Internal.Data.Stream.IsStream as IP
23+import qualified Streamly.Prelude as S
30
24
31import Gauge
25import Gauge
32import Streamly.Benchmark.Common
26import Streamly.Benchmark.Common
@@ -159,13 +153,9 @@ o_1_space_serial_transformation value =
159 [ bgroup "serially"
153 [ bgroup "serially"
160 [ bgroup "transformation"
154 [ bgroup "transformation"
161 [ benchIOSink value "lmap" (S.fold (IFL.lmap (+ 1) FL.drain))
155 [ benchIOSink value "lmap" (S.fold (IFL.lmap (+ 1) FL.drain))
156+ , let f x = if even x then Just x else Nothing
157+ fld = IFL.mapMaybe f FL.drain
162− , benchIOSink
158+ in benchIOSink value "mapMaybe" (S.fold fld)
163− value
164− "mapMaybe"
165− (S.fold
166− (IFL.mapMaybe
167− (\x -> case x of 1 -> Just x ; _ -> Nothing)
168− FL.drain))
169 , benchIOSink
159 , benchIOSink
170 value
160 value
171 "sequence"
161 "sequence"
src/Streamly/Internal/Data/Fold.hs+9−5
@@ -315,11 +315,15 @@ sequence (Fold step initial extract) = Fold step initial extract'
315mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
315mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
316mapM f = sequence . fmap f
316mapM f = sequence . fmap f
317
317
318−-- | @(mapMaybe f fold)@ maps a 'Maybe' returning function @f@ on the input of the fold,
318+-- | @mapMaybe f fold@ maps a 'Maybe' returning function @f@ on the input of
319−-- filter out the 'Nothing' elements, and return a values extracted from 'Just'.
319+-- the fold, filters out 'Nothing' elements, and return the values extracted
320−-- >>> S.fold (FL.mapMaybe (\x -> case x of 5 -> Just x ; _ -> Nothing) FL.sum)
320+-- from 'Just'.
321+--
322+-- >>> f x = if even x then Just x else Nothing
323+-- >>> fld = Fold.mapMaybe f Fold.toList
321−-- (S.enumerateFromTo 1 10)
324+-- >>> Stream.fold fld (Stream.enumerateFromTo 1 10)
322−-- 5
325+-- [2,4,6,8]
326+--
323-- /Internal/
327-- /Internal/
324{-# INLINE mapMaybe #-}
328{-# INLINE mapMaybe #-}
325mapMaybe :: (Monad m) => (a -> Maybe b) -> Fold m b r -> Fold m a r
329mapMaybe :: (Monad m) => (a -> Maybe b) -> Fold m b r -> Fold m a r
test/Streamly/Test/Data/Fold.hs+4−2
@@ -97,8 +97,10 @@ last :: [String] -> Expectation
97last ls = S.fold FL.last (S.fromList ls) `shouldReturn` safeLast ls
97last ls = S.fold FL.last (S.fromList ls) `shouldReturn` safeLast ls
98
98
99mapMaybe :: [Int] -> Expectation
99mapMaybe :: [Int] -> Expectation
100+mapMaybe ls =
100−mapMaybe ls = S.fold (F.mapMaybe (\x -> if even x then Just x else Nothing) FL.toList)
101+ (let f = F.mapMaybe (\x -> if even x then Just x else Nothing) FL.toList
102+ in S.fold f (S.fromList ls))
101− (S.fromList ls) `shouldReturn` filter even ls
103+ `shouldReturn` filter even ls
102
104
103nth :: Int -> [a] -> Maybe a
105nth :: Int -> [a] -> Maybe a
104nth idx (x:xs) = if idx == 0
106nth idx (x:xs) = if idx == 0