on master help sign in
Implement scanl

--HG-- extra : convert_revision : c1faf4d20e8fdf07bd4db3990486b4d4ed799a9c

commitd793b2125523ccae1414a99b067f26e540cad709 authorBryan O'Sullivan <bos@serpentine.com> dateTue 27 Jan 2009, 08:16 (18 years ago) parentfa5c09f
2 files changed+25
Data/Text.hs+9 Data/Text/Fusion.hs+16
Data/Text.hs+9
@@ -72,6 +72,11 @@ module Data.Text
7272 , minimum
7373
7474 -- * Construction
75+
76+ -- ** Scans
77+ , scanl
78+
79+ -- ** Generation and unfolding
7580 , replicate
7681 , unfoldr
7782 , unfoldrN
@@ -423,6 +428,10 @@ minimum t = S.minimum (stream t)
423428-- -----------------------------------------------------------------------------
424429-- * Building 'Text's
425430
431+scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
432+scanl f z t = unstream (S.scanl f z (stream t))
433+{-# INLINE scanl #-}
434+
426435-- -----------------------------------------------------------------------------
427436-- ** Generating and unfolding 'Text's
428437
Data/Text/Fusion.hs+16
@@ -60,6 +60,10 @@ module Data.Text.Fusion
6060 , minimum
6161
6262 -- * Construction
63+ -- ** Scans
64+ , scanl
65+
66+ -- ** Generation and unfolding
6367 , replicate
6468 , unfoldr
6569 , unfoldrN
@@ -540,6 +544,18 @@ minimum (Stream next0 s0 _len) = loop0_minimum s0
540544-- -----------------------------------------------------------------------------
541545-- * Building streams
542546
547+scanl :: (Char -> Char -> Char) -> Char -> Stream Char -> Stream Char
548+scanl f z0 (Stream next0 s0 len) = Stream next (S1 :!: z0 :!: s0) (len+1)
549+ where
550+ {-# INLINE next #-}
551+ next (S1 :!: z :!: s) = Yield z (S2 :!: z :!: s)
552+ next (S2 :!: z :!: s) = case next0 s of
553+ Yield x s' -> let !x' = f z x
554+ in Yield x' (S2 :!: x' :!: s')
555+ Skip s' -> Skip (S2 :!: z :!: s')
556+ Done -> Done
557+{-# INLINE [0] scanl #-}
558+
543559-- -----------------------------------------------------------------------------
544560-- ** Generating and unfolding streams
545561