@@ -75,6 +75,7 @@ module Data.Text.Fusion
75 , replicate
75 , replicate
77 , unfoldrN
77 , unfoldrN
79 -- * Substrings
80 -- * Substrings
80 -- ** Breaking strings
81 -- ** Breaking strings
@@ -104,8 +105,8 @@ module Data.Text.Fusion
104 , zipWith
105 , zipWith
107−import Prelude (Bool(..), Char, Either(..), Eq(..), Maybe(..), Monad(..),
108+import Prelude (Bool(..), Char, Either(..), Eq(..), Integral, Maybe(..),
108− Num(..), Ord(..), String, ($), (++), (.), (&&),
109+ Monad(..), Num(..), Ord(..), String, ($), (++), (.), (&&),
109 fromIntegral, otherwise)
110 fromIntegral, otherwise)
110import qualified Data.List as L
111import qualified Data.List as L
111import GHC.Exts (Int(..), (+#))
112import GHC.Exts (Int(..), (+#))
@@ -641,15 +642,33 @@ unfoldr f s0 = Stream next s0 1
641-- value. However, the length of the result is limited by the
642-- value. However, the length of the result is limited by the
642-- first argument to 'unfoldrN'. This function is more efficient than
643-- first argument to 'unfoldrN'. This function is more efficient than
643-- 'unfoldr' when the length of the result is known.
644-- 'unfoldr' when the length of the result is known.
644−unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Stream Char
645+unfoldrI :: Integral a => a -> (b -> Maybe (Char,b)) -> b -> Stream Char
645−unfoldrN n f s0 | n < 0 = empty
646+unfoldrI n f s0 | n < 0 = empty
646− | otherwise = Stream next (0 :!: s0) (n*2)
647+ | otherwise = Stream next (0 :!: s0) (fromIntegral (n*2))
648 {-# INLINE next #-}
649 {-# INLINE next #-}
649 next (z :!: s) = case f s of
650 next (z :!: s) = case f s of
650 Nothing -> Done
651 Nothing -> Done
651 Just (w, s') | z >= n -> Done
652 Just (w, s') | z >= n -> Done
652 | otherwise -> Yield w ((z + 1) :!: s')
653 | otherwise -> Yield w ((z + 1) :!: s')
654+{-# INLINE unfoldrI #-}
656+-- | /O(n)/ Like 'unfoldr', 'unfoldrN' builds a stream from a seed
657+-- value. However, the length of the result is limited by the
658+-- first argument to 'unfoldrN'. This function is more efficient than
659+-- 'unfoldr' when the length of the result is known.
660+unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Stream Char
661+unfoldrN n = unfoldrI (fromIntegral n :: Int)
662+{-# INLINE [0] unfoldrN #-}
664+-- | /O(n)/ Like 'unfoldr', 'unfoldrN64' builds a stream from a seed
665+-- value. However, the length of the result is limited by the
666+-- first argument to 'unfoldrN64'. This function is more efficient than
667+-- 'unfoldr' when the length of the result is known.
668+unfoldrN64 :: Int64 -> (a -> Maybe (Char,a)) -> a -> Stream Char
669+unfoldrN64 n = unfoldrI (fromIntegral n :: Int64)
670+{-# INLINE [0] unfoldrN64 #-}
653-------------------------------------------------------------------------------
672-------------------------------------------------------------------------------
654-- * Substreams
673-- * Substreams