on master help sign in
Merge pull request #574 from shlok:seedless-unfold
72b6a6dHarendra Kumar
src/Streamly/Internal/Prelude.hs+9
@@ -57,6 +57,7 @@ module Streamly.Internal.Prelude
57 , unfoldr
57 , unfoldr
58 , unfoldrM
58 , unfoldrM
59 , unfold
59 , unfold
60+ , unfold0
60 , iterate
61 , iterate
61 , iterateM
62 , iterateM
62 , fromIndices
63 , fromIndices
@@ -536,6 +537,7 @@ import Data.Kind (Type)
536#endif
537#endif
537import Data.Heap (Entry(..))
538import Data.Heap (Entry(..))
538import Data.Maybe (isJust, fromJust, isNothing)
539import Data.Maybe (isJust, fromJust, isNothing)
540+import Data.Void (Void)
539import Foreign.Storable (Storable)
541import Foreign.Storable (Storable)
540import Prelude
542import Prelude
541 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
543 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
@@ -706,6 +708,13 @@ unfoldrMZipSerial = Serial.unfoldrM
706unfold :: (IsStream t, Monad m) => Unfold m a b -> a -> t m b
708unfold :: (IsStream t, Monad m) => Unfold m a b -> a -> t m b
707unfold unf x = fromStreamD $ D.unfold unf x
709unfold unf x = fromStreamD $ D.unfold unf x
708
710
711+-- | Convert an 'Unfold' with a closed input end into a stream.
712+--
713+-- /Internal/
714+{-# INLINE unfold0 #-}
715+unfold0 :: (IsStream t, Monad m) => Unfold m Void b -> t m b
716+unfold0 unf = unfold unf (error "unfold0: unexpected void evaluation")
717+
709------------------------------------------------------------------------------
718------------------------------------------------------------------------------
710-- Specialized Generation
719-- Specialized Generation
711------------------------------------------------------------------------------
720------------------------------------------------------------------------------
test/Streamly/Test/Internal/Prelude.hs+30−4
@@ -15,9 +15,13 @@ import Control.Concurrent (threadDelay)
15import Control.Monad (when)
15import Control.Monad (when)
16
16
17import Test.Hspec as H
17import Test.Hspec as H
18+import Test.Hspec.QuickCheck (prop)
19+import Test.QuickCheck (Property, choose)
20+import Test.QuickCheck.Monadic (monadicIO, pick)
18
21
19import qualified Streamly.Prelude as S
22import qualified Streamly.Prelude as S
20import qualified Streamly.Internal.Data.Fold as FL
23import qualified Streamly.Internal.Data.Fold as FL
24+import qualified Streamly.Internal.Data.Unfold as UF
21import qualified Streamly.Internal.Prelude as SI
25import qualified Streamly.Internal.Prelude as SI
22
26
23import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
27import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
@@ -25,6 +29,9 @@ import Streamly.Internal.Data.Time.Units
25 (AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
29 (AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
26import Data.Int (Int64)
30import Data.Int (Int64)
27
31
32+max_length :: Int
33+max_length = 1000
34+
28tenPow8 :: Int64
35tenPow8 :: Int64
29tenPow8 = 10^(8 :: Int)
36tenPow8 = 10^(8 :: Int)
30
37
@@ -70,9 +77,28 @@ testDropByTime = do
70 $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
77 $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
71 checkTakeDropTime (Just t0, mt1)
78 checkTakeDropTime (Just t0, mt1)
72
79
80+unfold :: Property
81+unfold = monadicIO $ do
82+ a <- pick $ choose (0, max_length `div` 2)
83+ b <- pick $ choose (0, max_length)
84+ let unf = UF.enumerateFromToIntegral b
85+ ls <- S.toList $ S.unfold unf a
86+ return $ ls == [a..b]
87+
88+unfold0 :: Property
89+unfold0 = monadicIO $ do
90+ a <- pick $ choose (0, max_length `div` 2)
91+ b <- pick $ choose (0, max_length)
92+ let unf = UF.supply (UF.enumerateFromToIntegral b) a
93+ ls <- S.toList $ SI.unfold0 unf
94+ return $ ls == [a..b]
95+
73main :: IO ()
96main :: IO ()
74main =
97main =
75− hspec $
98+ hspec $ do
76− describe "Filtering" $ do
99+ describe "Filtering" $ do
77− it "takeByTime" (testTakeByTime `shouldReturn` True)
100+ it "takeByTime" (testTakeByTime `shouldReturn` True)
78− it "dropByTime" (testDropByTime `shouldReturn` True)
101+ it "dropByTime" (testDropByTime `shouldReturn` True)
102+ describe "From Generators" $ do
103+ prop "unfold" unfold
104+ prop "unfold0" unfold0