on master help sign in
Fix formatting and docs

* Use let clauses to break bigger expressions * Use `even` as predicate

commitcd3ce3e503ef51c227a7886fa49b6e6dcf7c79b6 authorHarendra Kumar <harendra@composewell.com> dateSat 14 Nov 2020, 00:53 (6 years ago) parent60d13da
3 files changed+20−24
benchmark/Streamly/Benchmark/Data/Fold.hs+7−17 src/Streamly/Internal/Data/Fold.hs+9−5 test/Streamly/Test/Data/Fold.hs+4−2
benchmark/Streamly/Benchmark/Data/Fold.hs+7−17
@@ -11,22 +11,16 @@ module Main (main) where
1111
1212import Control.DeepSeq (NFData(..))
1313import Data.Monoid (Last(..), Sum(..))
14−
1514import 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
2515
2616import qualified Streamly.Data.Array.Storable.Foreign as A
17+import qualified Streamly.Data.Fold as FL
2718import qualified Streamly.Internal.Data.Array.Storable.Foreign as IA
2819import 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
2922import qualified Streamly.Internal.Data.Stream.IsStream as IP
23+import qualified Streamly.Prelude as S
3024
3125import Gauge
3226import Streamly.Benchmark.Common
@@ -159,13 +153,9 @@ o_1_space_serial_transformation value =
159153 [ bgroup "serially"
160154 [ bgroup "transformation"
161155 [ benchIOSink value "lmap" (S.fold (IFL.lmap (+ 1) FL.drain))
162− , benchIOSink
163− value
164− "mapMaybe"
165− (S.fold
166− (IFL.mapMaybe
167− (\x -> case x of 1 -> Just x ; _ -> Nothing)
168− FL.drain))
156+ , let f x = if even x then Just x else Nothing
157+ fld = IFL.mapMaybe f FL.drain
158+ in benchIOSink value "mapMaybe" (S.fold fld)
169159 , benchIOSink
170160 value
171161 "sequence"
src/Streamly/Internal/Data/Fold.hs+9−5
@@ -315,11 +315,15 @@ sequence (Fold step initial extract) = Fold step initial extract'
315315mapM :: Monad m => (b -> m c) -> Fold m a b -> Fold m a c
316316mapM f = sequence . fmap f
317317
318−-- | @(mapMaybe f fold)@ maps a 'Maybe' returning function @f@ on the input of the fold,
319−-- filter out the 'Nothing' elements, and return a values extracted from 'Just'.
320−-- >>> S.fold (FL.mapMaybe (\x -> case x of 5 -> Just x ; _ -> Nothing) FL.sum)
321−-- (S.enumerateFromTo 1 10)
322−-- 5
318+-- | @mapMaybe f fold@ maps a 'Maybe' returning function @f@ on the input of
319+-- the fold, filters out 'Nothing' elements, and return the values extracted
320+-- from 'Just'.
321+--
322+-- >>> f x = if even x then Just x else Nothing
323+-- >>> fld = Fold.mapMaybe f Fold.toList
324+-- >>> Stream.fold fld (Stream.enumerateFromTo 1 10)
325+-- [2,4,6,8]
326+--
323327-- /Internal/
324328{-# INLINE mapMaybe #-}
325329mapMaybe :: (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
9797last ls = S.fold FL.last (S.fromList ls) `shouldReturn` safeLast ls
9898
9999mapMaybe :: [Int] -> Expectation
100−mapMaybe ls = S.fold (F.mapMaybe (\x -> if even x then Just x else Nothing) FL.toList)
101− (S.fromList ls) `shouldReturn` filter even ls
100+mapMaybe ls =
101+ (let f = F.mapMaybe (\x -> if even x then Just x else Nothing) FL.toList
102+ in S.fold f (S.fromList ls))
103+ `shouldReturn` filter even ls
102104
103105nth :: Int -> [a] -> Maybe a
104106nth idx (x:xs) = if idx == 0