@@ -126,6 +126,15 @@ sourceUnfoldr n = S.unfoldr step n
126126 then Nothing
127127 else Just (cnt, cnt + 1)
128128
129+{-# INLINE sourceUnfoldrN #-}
130+sourceUnfoldrN :: (Monad m, S.IsStream t) => Int -> Int -> t m Int
131+sourceUnfoldrN upto start = S.unfoldr step start
132+ where
133+ step cnt =
134+ if cnt > start + upto
135+ then Nothing
136+ else Just (cnt, cnt + 1)
137+
129138{-# INLINE sourceUnfoldrM #-}
130139sourceUnfoldrM :: (S.IsStream t, S.MonadAsync m) => Int -> t m Int
131140sourceUnfoldrM n = S.unfoldrM step n
@@ -137,10 +146,10 @@ sourceUnfoldrM n = S.unfoldrM step n
137146
138147{-# INLINE sourceUnfoldrMN #-}
139148sourceUnfoldrMN :: (S.IsStream t, S.MonadAsync m) => Int -> Int -> t m Int
140−sourceUnfoldrMN m n = S.unfoldrM step n
149+sourceUnfoldrMN upto start = S.unfoldrM step start
141150 where
142151 step cnt =
143− if cnt > n + m
152+ if cnt > start + upto
144153 then return Nothing
145154 else return (Just (cnt, cnt + 1))
146155
@@ -533,16 +542,29 @@ eqBy src = S.eqBy (==) src src
533542cmpBy :: (Monad m, P.Ord a) => Stream m a -> m P.Ordering
534543cmpBy src = S.cmpBy P.compare src src
535544
536−concatStreamLen, maxNested :: Int
537−-- concatStreamLen = value2
538−-- maxNested = value2
539−concatStreamLen = 1
540−maxNested = 100000
541−
545+-- The worst case for concatMap, concat a 1 element stream n times.
542546{-# INLINE concatMap #-}
543547concatMap :: S.MonadAsync m => Int -> Stream m Int
544−concatMap n = S.concatMap (\_ -> sourceUnfoldrMN maxNested n)
545− (sourceUnfoldrMN concatStreamLen n)
548+concatMap n = S.concatMap (\_ -> sourceUnfoldrMN 1 n)
549+ (sourceUnfoldrMN value n)
550+
551+{-# INLINE concatMapPure1xN #-}
552+concatMapPure1xN :: S.MonadAsync m => Int -> Stream m Int
553+concatMapPure1xN n = S.concatMap (\_ -> sourceUnfoldrN 1 n)
554+ (sourceUnfoldrN value n)
555+
556+-- We use a (sqrt n) element stream as source and then generate and concat a
557+-- (sqrt n) element stream for each element of the source to produce an n
558+-- element stream.
559+{-# INLINE concatMapNxN #-}
560+concatMapNxN :: S.MonadAsync m => Int -> Stream m Int
561+concatMapNxN n = S.concatMap (\_ -> sourceUnfoldrMN value2 n)
562+ (sourceUnfoldrMN value2 n)
563+
564+{-# INLINE concatMapRepl4xN #-}
565+concatMapRepl4xN :: S.MonadAsync m => Int -> Stream m Int
566+concatMapRepl4xN n = S.concatMap (S.replicate 4)
567+ (sourceUnfoldrMN (value `div` 4) n)
546568
547569-------------------------------------------------------------------------------
548570-- Mixed Composition