on master help sign in
Merge pull request #574 from shlok:seedless-unfold
commit72b6a6db06df8cc0b983b07d28848193cd88c978 authorHarendra Kumar <harendra@composewell.com> dateFri 3 Jul 2020, 16:10 (6 years ago) parent98514249225c35
2 files changed+39−4
src/Streamly/Internal/Prelude.hs+9 test/Streamly/Test/Internal/Prelude.hs+30−4
src/Streamly/Internal/Prelude.hs+9
@@ -57,6 +57,7 @@ module Streamly.Internal.Prelude
5757 , unfoldr
5858 , unfoldrM
5959 , unfold
60+ , unfold0
6061 , iterate
6162 , iterateM
6263 , fromIndices
@@ -536,6 +537,7 @@ import Data.Kind (Type)
536537#endif
537538import Data.Heap (Entry(..))
538539import Data.Maybe (isJust, fromJust, isNothing)
540+import Data.Void (Void)
539541import Foreign.Storable (Storable)
540542import Prelude
541543 hiding (filter, drop, dropWhile, take, takeWhile, zipWith, foldr,
@@ -706,6 +708,13 @@ unfoldrMZipSerial = Serial.unfoldrM
706708unfold :: (IsStream t, Monad m) => Unfold m a b -> a -> t m b
707709unfold unf x = fromStreamD $ D.unfold unf x
708710
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+
709718------------------------------------------------------------------------------
710719-- Specialized Generation
711720------------------------------------------------------------------------------
test/Streamly/Test/Internal/Prelude.hs+30−4
@@ -15,9 +15,13 @@ import Control.Concurrent (threadDelay)
1515import Control.Monad (when)
1616
1717import Test.Hspec as H
18+import Test.Hspec.QuickCheck (prop)
19+import Test.QuickCheck (Property, choose)
20+import Test.QuickCheck.Monadic (monadicIO, pick)
1821
1922import qualified Streamly.Prelude as S
2023import qualified Streamly.Internal.Data.Fold as FL
24+import qualified Streamly.Internal.Data.Unfold as UF
2125import qualified Streamly.Internal.Prelude as SI
2226
2327import Streamly.Internal.Data.Time.Clock (Clock(Monotonic), getTime)
@@ -25,6 +29,9 @@ import Streamly.Internal.Data.Time.Units
2529 (AbsTime, NanoSecond64(..), toRelTime64, diffAbsTime64)
2630import Data.Int (Int64)
2731
32+max_length :: Int
33+max_length = 1000
34+
2835tenPow8 :: Int64
2936tenPow8 = 10^(8 :: Int)
3037
@@ -70,9 +77,28 @@ testDropByTime = do
7077 $ S.repeatM (threadDelay 1000 >> getTime Monotonic)
7178 checkTakeDropTime (Just t0, mt1)
7279
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+
7396main :: IO ()
7497main =
75− hspec $
76− describe "Filtering" $ do
77− it "takeByTime" (testTakeByTime `shouldReturn` True)
78− it "dropByTime" (testDropByTime `shouldReturn` True)
98+ hspec $ do
99+ describe "Filtering" $ do
100+ it "takeByTime" (testTakeByTime `shouldReturn` True)
101+ it "dropByTime" (testDropByTime `shouldReturn` True)
102+ describe "From Generators" $ do
103+ prop "unfold" unfold
104+ prop "unfold0" unfold0