@@ -1,8 +1,7 @@
11module Main (main) where
22
3−import Prelude hiding
4− (maximum, minimum, elem, notElem, null, product, sum, head, last)
53import Data.Semigroup (Sum(..), getSum)
4+import Streamly.Test.Common (checkListEqual)
65import Test.QuickCheck
76 ( Gen
87 , Property
@@ -15,15 +14,15 @@ import Test.QuickCheck
1514 , vectorOf
1615 , withMaxSuccess
1716 )
18−import Prelude hiding
19− (maximum, minimum, elem, notElem, null, product, sum, head, last)
20−import Streamly.Test.Common (checkListEqual)
2117import Test.QuickCheck.Monadic (monadicIO, assert, run)
2218
19+import qualified Prelude
2320import qualified Streamly.Internal.Data.Fold as F
2421import qualified Streamly.Prelude as S
2522import qualified Streamly.Data.Fold as FL
2623
24+import Prelude hiding
25+ (maximum, minimum, elem, notElem, null, product, sum, head, last)
2726import Test.Hspec as H
2827import Test.Hspec.QuickCheck
2928
@@ -70,10 +69,11 @@ length :: [Int] -> Expectation
7069length ls = S.fold FL.length (S.fromList ls) `shouldReturn` Prelude.length ls
7170
7271sum :: [Int] -> Expectation
73−sum ls = S.fold FL.sum (S.fromList ls) `shouldReturn` foldl (+) 0 ls
72+sum ls = S.fold FL.sum (S.fromList ls) `shouldReturn` Prelude.sum ls
7473
7574product :: [Int] -> Expectation
76−product ls = S.fold FL.product (S.fromList ls) `shouldReturn` foldl (*) 1 ls
75+product ls =
76+ S.fold FL.product (S.fromList ls) `shouldReturn` Prelude.product ls
7777
7878lesser :: (a -> a -> Ordering) -> a -> a -> a
7979lesser f x y = if f x y == LT then x else y
@@ -90,8 +90,7 @@ foldMaybe f acc ls =
9090maximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
9191maximumBy genmin f ls =
9292 S.fold (FL.maximumBy f) (S.fromList ls)
93− `shouldReturn`
94− foldMaybe (greater f) genmin ls
93+ `shouldReturn` foldMaybe (greater f) genmin ls
9594
9695maximum :: (Show a, Ord a) => a -> [a] -> Expectation
9796maximum genmin ls =
@@ -129,16 +128,16 @@ mapMaybe ls =
129128 in S.fold f (S.fromList ls) `shouldReturn` filter even ls
130129
131130nth :: Int -> [a] -> Maybe a
132−nth idx (x:xs) = if idx == 0
133− then Just x
134− else if idx < 0
135− then Nothing
136− else nth (idx - 1) xs
131+nth idx (x : xs)
132+ | idx == 0 = Just x
133+ | idx < 0 = Nothing
134+ | otherwise = nth (idx - 1) xs
137135nth _ [] = Nothing
138136
139137index :: Int -> [String] -> Expectation
140−index idx ls = let x = S.fold (FL.index idx) (S.fromList ls)
141− in x `shouldReturn` (nth idx ls)
138+index idx ls =
139+ let x = S.fold (FL.index idx) (S.fromList ls)
140+ in x `shouldReturn` nth idx ls
142141
143142find :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
144143find f ls = do
@@ -152,7 +151,7 @@ find f ls = do
152151 in fld `shouldReturn` False
153152
154153neg :: (a -> Bool) -> a -> Bool
155−neg f x = if f x == True then False else True
154+neg f x = not (f x)
156155
157156findIndex :: (a -> Bool) -> [a] -> Expectation
158157findIndex f ls = do
@@ -161,16 +160,16 @@ findIndex f ls = do
161160 Nothing ->
162161 let fld = S.fold (FL.all $ neg f) (S.fromList ls)
163162 in fld `shouldReturn` True
164− Just idx -> if idx == 0
165− then
166− S.fold (FL.all f) (S.fromList []) `shouldReturn` True
167− else
168− S.fold (FL.all f) (S.fromList $ (take idx ls))
169− `shouldReturn`
170− False
163+ Just idx ->
164+ if idx == 0
165+ then
166+ S.fold (FL.all f) (S.fromList []) `shouldReturn` True
167+ else
168+ S.fold (FL.all f) (S.fromList $ take idx ls)
169+ `shouldReturn` False
171170
172171predicate :: Int -> Bool
173−predicate x = if x * x < 100 then True else False
172+predicate x = x * x < 100
174173
175174elemIndex :: Int -> [Int] -> Expectation
176175elemIndex elm ls = do
@@ -180,13 +179,17 @@ elemIndex elm ls = do
180179 let fld = S.fold (FL.any (== elm)) (S.fromList ls)
181180 in fld `shouldReturn` False
182181 Just idx ->
183− let fld = S.fold (FL.any (== elm)) (S.fromList $ (take idx ls))
182+ let fld = S.fold (FL.any (== elm)) (S.fromList $ take idx ls)
184183 in fld `shouldReturn` False
185184
186185null :: [Int] -> Expectation
187−null ls = S.fold FL.null (S.fromList ls) `shouldReturn` case ls of
188− [] -> True
189− _ -> False
186+null ls =
187+ S.fold FL.null (S.fromList ls)
188+ `shouldReturn`
189+ case ls of
190+ [] -> True
191+ _ -> False
192+
190193elem :: Int -> [Int] -> Expectation
191194elem elm ls = do
192195 y <- S.fold (FL.elem elm) (S.fromList ls)
@@ -201,7 +204,7 @@ notElem elm ls = do
201204
202205all :: (a -> Bool) -> [a] -> Expectation
203206all f ls =
204− S.fold (FL.all f) (S.fromList ls) `shouldReturn` Prelude.and (map f ls)
207+ S.fold (FL.all f) (S.fromList ls) `shouldReturn` Prelude.all f ls
205208
206209any :: (a -> Bool) -> [a] -> Expectation
207210any f ls = S.fold (FL.any f) (S.fromList ls) `shouldReturn` Prelude.any f ls
@@ -257,7 +260,7 @@ mean =
257260
258261 action ls = do
259262 v1 <- run $ S.fold FL.mean (S.fromList ls)
260− let v2 = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
263+ let v2 = Prelude.sum ls / fromIntegral (Prelude.length ls)
261264 assert (abs (v1 - v2) < 0.0001)
262265
263266stdDev :: Property
@@ -269,8 +272,8 @@ stdDev =
269272
270273 action ls = do
271274 v1 <- run $ S.fold FL.stdDev (S.fromList ls)
272− let avg = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
273− se = (foldl (+) 0 (map (\x -> (x - avg) * (x - avg)) ls))
275+ let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
276+ se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
274277 sd = sqrt $ se / fromIntegral (Prelude.length ls)
275278 assert (abs (v1 - sd) < 0.0001 )
276279
@@ -283,8 +286,8 @@ variance =
283286
284287 action ls = do
285288 v1 <- run $ S.fold FL.variance (S.fromList ls)
286− let avg = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
287− se = (foldl (+) 0 (map (\x -> (x - avg) * (x - avg)) ls))
289+ let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
290+ se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
288291 vr = se / fromIntegral (Prelude.length ls)
289292 assert (abs (v1 - vr) < 0.01 )
290293
@@ -297,7 +300,7 @@ mconcat =
297300
298301 action ls = do
299302 v1 <- run $ S.fold FL.mconcat (S.map Sum $ S.fromList ls)
300− let v2 = (foldl (+) 0 ls)
303+ let v2 = Prelude.sum ls
301304 assert (getSum v1 == v2)
302305
303306foldMap :: Property
@@ -309,7 +312,7 @@ foldMap =
309312
310313 action ls = do
311314 v1 <- run $ S.fold (FL.foldMap Sum) $ S.fromList ls
312− let v2 = (foldl (+) 0 ls)
315+ let v2 = Prelude.sum ls
313316 assert (getSum v1 == v2)
314317
315318foldMapM :: Property
@@ -321,7 +324,7 @@ foldMapM =
321324
322325 action ls = do
323326 v1 <- run $ S.fold (FL.foldMapM (return . Sum)) $ S.fromList ls
324− let v2 = (foldl (+) 0 ls)
327+ let v2 = Prelude.sum ls
325328 assert (getSum v1 == v2)
326329
327330lookup :: Property
@@ -363,7 +366,7 @@ teeWithLength =
363366
364367 action ls = do
365368 v1 <- run $ S.fold (FL.tee FL.sum FL.length) $ S.fromList ls
366− let v2 = foldl (+) 0 ls
369+ let v2 = Prelude.sum ls
367370 v3 = Prelude.length ls
368371 assert (v1 == (v2, v3))
369372
@@ -376,7 +379,7 @@ teeWithMax =
376379
377380 action ls = do
378381 v1 <- run $ S.fold (FL.tee FL.sum FL.maximum) $ S.fromList ls
379− let v2 = foldl (+) 0 ls
382+ let v2 = Prelude.sum ls
380383 v3 = foldMaybe (greater compare) intMin ls
381384 assert (v1 == (v2, v3))
382385
@@ -389,7 +392,7 @@ distribute =
389392
390393 action ls = do
391394 v1 <- run $ S.fold (FL.distribute [FL.sum, FL.length]) $ S.fromList ls
392− let v2 = foldl (+) 0 ls
395+ let v2 = Prelude.sum ls
393396 v3 = Prelude.length ls
394397 assert (v1 == [v2, v3])
395398
@@ -418,7 +421,7 @@ main :: IO ()
418421main = hspec $
419422 describe "Fold s" $ do
420423 prop "RollingHashFirstN" rollingHashFirstN
421− prop "Index" $ index
424+ prop "Index" index
422425 prop "Head" head
423426 prop "Last" last
424427 prop "Length" Main.length