@@ -1,8 +1,7 @@
1module Main (main) where
1module Main (main) where
4− (maximum, minimum, elem, notElem, null, product, sum, head, last)
5import Data.Semigroup (Sum(..), getSum)
3import Data.Semigroup (Sum(..), getSum)
4+import Streamly.Test.Common (checkListEqual)
6import Test.QuickCheck
5import Test.QuickCheck
@@ -15,15 +14,15 @@ import Test.QuickCheck
15 , vectorOf
14 , vectorOf
16 , withMaxSuccess
15 , withMaxSuccess
19− (maximum, minimum, elem, notElem, null, product, sum, head, last)
20−import Streamly.Test.Common (checkListEqual)
21import Test.QuickCheck.Monadic (monadicIO, assert, run)
17import Test.QuickCheck.Monadic (monadicIO, assert, run)
19+import qualified Prelude
23import qualified Streamly.Internal.Data.Fold as F
20import qualified Streamly.Internal.Data.Fold as F
24import qualified Streamly.Prelude as S
21import qualified Streamly.Prelude as S
25import qualified Streamly.Data.Fold as FL
22import qualified Streamly.Data.Fold as FL
25+ (maximum, minimum, elem, notElem, null, product, sum, head, last)
27import Test.Hspec as H
26import Test.Hspec as H
28import Test.Hspec.QuickCheck
27import Test.Hspec.QuickCheck
@@ -70,10 +69,11 @@ length :: [Int] -> Expectation
70length ls = S.fold FL.length (S.fromList ls) `shouldReturn` Prelude.length ls
69length ls = S.fold FL.length (S.fromList ls) `shouldReturn` Prelude.length ls
72sum :: [Int] -> Expectation
71sum :: [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
75product :: [Int] -> Expectation
74product :: [Int] -> Expectation
76−product ls = S.fold FL.product (S.fromList ls) `shouldReturn` foldl (*) 1 ls
76+ S.fold FL.product (S.fromList ls) `shouldReturn` Prelude.product ls
78lesser :: (a -> a -> Ordering) -> a -> a -> a
78lesser :: (a -> a -> Ordering) -> a -> a -> a
79lesser f x y = if f x y == LT then x else y
79lesser f x y = if f x y == LT then x else y
@@ -90,8 +90,7 @@ foldMaybe f acc ls =
90maximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
90maximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
91maximumBy genmin f ls =
91maximumBy genmin f ls =
92 S.fold (FL.maximumBy f) (S.fromList ls)
92 S.fold (FL.maximumBy f) (S.fromList ls)
94− foldMaybe (greater f) genmin ls
93+ `shouldReturn` foldMaybe (greater f) genmin ls
96maximum :: (Show a, Ord a) => a -> [a] -> Expectation
95maximum :: (Show a, Ord a) => a -> [a] -> Expectation
97maximum genmin ls =
96maximum genmin ls =
@@ -129,16 +128,16 @@ mapMaybe ls =
129 in S.fold f (S.fromList ls) `shouldReturn` filter even ls
128 in S.fold f (S.fromList ls) `shouldReturn` filter even ls
131nth :: Int -> [a] -> Maybe a
130nth :: Int -> [a] -> Maybe a
132−nth idx (x:xs) = if idx == 0
131+nth idx (x : xs)
133− then Just x
132+ | idx == 0 = Just x
135− then Nothing
133+ | idx < 0 = Nothing
136− else nth (idx - 1) xs
134+ | otherwise = nth (idx - 1) xs
137nth _ [] = Nothing
135nth _ [] = Nothing
139index :: Int -> [String] -> Expectation
137index :: Int -> [String] -> Expectation
140−index idx ls = let x = S.fold (FL.index idx) (S.fromList ls)
139+ let x = S.fold (FL.index idx) (S.fromList ls)
141− in x `shouldReturn` (nth idx ls)
140+ in x `shouldReturn` nth idx ls
143find :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
142find :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
144find f ls = do
143find f ls = do
@@ -152,7 +151,7 @@ find f ls = do
152 in fld `shouldReturn` False
151 in fld `shouldReturn` False
154neg :: (a -> Bool) -> a -> Bool
153neg :: (a -> Bool) -> a -> Bool
155−neg f x = if f x == True then False else True
154+neg f x = not (f x)
157findIndex :: (a -> Bool) -> [a] -> Expectation
156findIndex :: (a -> Bool) -> [a] -> Expectation
158findIndex f ls = do
157findIndex f ls = do
@@ -161,16 +160,16 @@ findIndex f ls = do
161 Nothing ->
160 Nothing ->
162 let fld = S.fold (FL.all $ neg f) (S.fromList ls)
161 let fld = S.fold (FL.all $ neg f) (S.fromList ls)
163 in fld `shouldReturn` True
162 in fld `shouldReturn` True
164− Just idx -> if idx == 0
163+ Just idx ->
166− S.fold (FL.all f) (S.fromList []) `shouldReturn` True
166+ S.fold (FL.all f) (S.fromList []) `shouldReturn` True
168− S.fold (FL.all f) (S.fromList $ (take idx ls))
168+ S.fold (FL.all f) (S.fromList $ take idx ls)
169− `shouldReturn`
169+ `shouldReturn` False
172predicate :: Int -> Bool
171predicate :: Int -> Bool
173−predicate x = if x * x < 100 then True else False
172+predicate x = x * x < 100
175elemIndex :: Int -> [Int] -> Expectation
174elemIndex :: Int -> [Int] -> Expectation
176elemIndex elm ls = do
175elemIndex elm ls = do
@@ -180,13 +179,17 @@ elemIndex elm ls = do
180 let fld = S.fold (FL.any (== elm)) (S.fromList ls)
179 let fld = S.fold (FL.any (== elm)) (S.fromList ls)
181 in fld `shouldReturn` False
180 in fld `shouldReturn` False
182 Just idx ->
181 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)
184 in fld `shouldReturn` False
183 in fld `shouldReturn` False
186null :: [Int] -> Expectation
185null :: [Int] -> Expectation
187−null ls = S.fold FL.null (S.fromList ls) `shouldReturn` case ls of
187+ S.fold FL.null (S.fromList ls)
188− [] -> True
190+ [] -> True
189− _ -> False
191+ _ -> False
190elem :: Int -> [Int] -> Expectation
193elem :: Int -> [Int] -> Expectation
191elem elm ls = do
194elem elm ls = do
192 y <- S.fold (FL.elem elm) (S.fromList ls)
195 y <- S.fold (FL.elem elm) (S.fromList ls)
@@ -201,7 +204,7 @@ notElem elm ls = do
202all :: (a -> Bool) -> [a] -> Expectation
205all :: (a -> Bool) -> [a] -> Expectation
203all f ls =
206all 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
206any :: (a -> Bool) -> [a] -> Expectation
209any :: (a -> Bool) -> [a] -> Expectation
207any f ls = S.fold (FL.any f) (S.fromList ls) `shouldReturn` Prelude.any f ls
210any f ls = S.fold (FL.any f) (S.fromList ls) `shouldReturn` Prelude.any f ls
@@ -257,7 +260,7 @@ mean =
258 action ls = do
261 action ls = do
259 v1 <- run $ S.fold FL.mean (S.fromList ls)
262 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)
261 assert (abs (v1 - v2) < 0.0001)
264 assert (abs (v1 - v2) < 0.0001)
263stdDev :: Property
266stdDev :: Property
@@ -269,8 +272,8 @@ stdDev =
270 action ls = do
273 action ls = do
271 v1 <- run $ S.fold FL.stdDev (S.fromList ls)
274 v1 <- run $ S.fold FL.stdDev (S.fromList ls)
272− let avg = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
275+ let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
273− se = (foldl (+) 0 (map (\x -> (x - avg) * (x - avg)) ls))
276+ se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
274 sd = sqrt $ se / fromIntegral (Prelude.length ls)
277 sd = sqrt $ se / fromIntegral (Prelude.length ls)
275 assert (abs (v1 - sd) < 0.0001 )
278 assert (abs (v1 - sd) < 0.0001 )
@@ -283,8 +286,8 @@ variance =
284 action ls = do
287 action ls = do
285 v1 <- run $ S.fold FL.variance (S.fromList ls)
288 v1 <- run $ S.fold FL.variance (S.fromList ls)
286− let avg = (foldl (+) 0 ls) / fromIntegral (Prelude.length ls)
289+ let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
287− se = (foldl (+) 0 (map (\x -> (x - avg) * (x - avg)) ls))
290+ se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
288 vr = se / fromIntegral (Prelude.length ls)
291 vr = se / fromIntegral (Prelude.length ls)
289 assert (abs (v1 - vr) < 0.01 )
292 assert (abs (v1 - vr) < 0.01 )
@@ -297,7 +300,7 @@ mconcat =
298 action ls = do
301 action ls = do
299 v1 <- run $ S.fold FL.mconcat (S.map Sum $ S.fromList ls)
302 v1 <- run $ S.fold FL.mconcat (S.map Sum $ S.fromList ls)
300− let v2 = (foldl (+) 0 ls)
303+ let v2 = Prelude.sum ls
301 assert (getSum v1 == v2)
304 assert (getSum v1 == v2)
303foldMap :: Property
306foldMap :: Property
@@ -309,7 +312,7 @@ foldMap =
310 action ls = do
313 action ls = do
311 v1 <- run $ S.fold (FL.foldMap Sum) $ S.fromList ls
314 v1 <- run $ S.fold (FL.foldMap Sum) $ S.fromList ls
312− let v2 = (foldl (+) 0 ls)
315+ let v2 = Prelude.sum ls
313 assert (getSum v1 == v2)
316 assert (getSum v1 == v2)
315foldMapM :: Property
318foldMapM :: Property
@@ -321,7 +324,7 @@ foldMapM =
322 action ls = do
325 action ls = do
323 v1 <- run $ S.fold (FL.foldMapM (return . Sum)) $ S.fromList ls
326 v1 <- run $ S.fold (FL.foldMapM (return . Sum)) $ S.fromList ls
324− let v2 = (foldl (+) 0 ls)
327+ let v2 = Prelude.sum ls
325 assert (getSum v1 == v2)
328 assert (getSum v1 == v2)
327lookup :: Property
330lookup :: Property
@@ -363,7 +366,7 @@ teeWithLength =
364 action ls = do
367 action ls = do
365 v1 <- run $ S.fold (FL.tee FL.sum FL.length) $ S.fromList ls
368 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
367 v3 = Prelude.length ls
370 v3 = Prelude.length ls
368 assert (v1 == (v2, v3))
371 assert (v1 == (v2, v3))
@@ -376,7 +379,7 @@ teeWithMax =
377 action ls = do
380 action ls = do
378 v1 <- run $ S.fold (FL.tee FL.sum FL.maximum) $ S.fromList ls
381 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
380 v3 = foldMaybe (greater compare) intMin ls
383 v3 = foldMaybe (greater compare) intMin ls
381 assert (v1 == (v2, v3))
384 assert (v1 == (v2, v3))
@@ -389,7 +392,7 @@ distribute =
390 action ls = do
393 action ls = do
391 v1 <- run $ S.fold (FL.distribute [FL.sum, FL.length]) $ S.fromList ls
394 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
393 v3 = Prelude.length ls
396 v3 = Prelude.length ls
394 assert (v1 == [v2, v3])
397 assert (v1 == [v2, v3])
@@ -418,7 +421,7 @@ main :: IO ()
418main = hspec $
421main = hspec $
419 describe "Fold s" $ do
422 describe "Fold s" $ do
420 prop "RollingHashFirstN" rollingHashFirstN
423 prop "RollingHashFirstN" rollingHashFirstN
421− prop "Index" $ index
424+ prop "Index" index
422 prop "Head" head
425 prop "Head" head
423 prop "Last" last
426 prop "Last" last
424 prop "Length" Main.length
427 prop "Length" Main.length