on master help sign in
Implement and test scanl1
7541131Bryan O'Sullivan
Data/Text.hs+10−1
@@ -75,7 +75,7 @@ module Data.Text
75
75
76 -- ** Scans
76 -- ** Scans
77 , scanl
77 , scanl
78− -- , scanl1
78+ , scanl1
79 -- , scanr
79 -- , scanr
80 -- , scanr1
80 -- , scanr1
81
81
@@ -495,6 +495,15 @@ scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
495scanl f z t = unstream (S.scanl f z (stream t))
495scanl f z t = unstream (S.scanl f z (stream t))
496{-# INLINE scanl #-}
496{-# INLINE scanl #-}
497
497
498+-- | /O(n)/ 'scanl1' is a variant of 'scanl' that has no starting
499+-- value argument. This function is subject to array fusion.
500+--
501+-- > scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]
502+scanl1 :: (Char -> Char -> Char) -> Text -> Text
503+scanl1 f t | null t = empty
504+ | otherwise = scanl f (head t) (tail t)
505+{-# INLINE scanl1 #-}
506+
498-- -----------------------------------------------------------------------------
507-- -----------------------------------------------------------------------------
499-- ** Generating and unfolding 'Text's
508-- ** Generating and unfolding 'Text's
500
509
tests/Properties.hs+2
@@ -72,6 +72,7 @@ prop_maximum = L.maximum `eqEP` T.maximum
72prop_minimum = L.minimum `eqEP` T.minimum
72prop_minimum = L.minimum `eqEP` T.minimum
73
73
74prop_scanl f z = L.scanl f z `eqP` (unpack . T.scanl f z)
74prop_scanl f z = L.scanl f z `eqP` (unpack . T.scanl f z)
75+prop_scanl1 f = L.scanl1 f `eqP` (unpack . T.scanl1 f)
75
76
76prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
77prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
77prop_unfoldr n = L.unfoldr f `eq` (unpack . T.unfoldr f)
78prop_unfoldr n = L.unfoldr f `eq` (unpack . T.unfoldr f)
@@ -159,6 +160,7 @@ tests = [
159 ("prop_minimum", mytest prop_minimum),
160 ("prop_minimum", mytest prop_minimum),
160
161
161 ("prop_scanl", mytest prop_scanl),
162 ("prop_scanl", mytest prop_scanl),
163+ ("prop_scanl1", mytest prop_scanl1),
162
164
163 ("prop_replicate", mytest prop_replicate),
165 ("prop_replicate", mytest prop_replicate),
164 ("prop_unfoldr", mytest prop_unfoldr),
166 ("prop_unfoldr", mytest prop_unfoldr),