on master help sign in
Implement scanl
d793b21Bryan O'Sullivan
Data/Text.hs+9
@@ -72,6 +72,11 @@ module Data.Text
72 , minimum
72 , minimum
73
73
74 -- * Construction
74 -- * Construction
75+
76+ -- ** Scans
77+ , scanl
78+
79+ -- ** Generation and unfolding
75 , replicate
80 , replicate
76 , unfoldr
81 , unfoldr
77 , unfoldrN
82 , unfoldrN
@@ -423,6 +428,10 @@ minimum t = S.minimum (stream t)
423-- -----------------------------------------------------------------------------
428-- -----------------------------------------------------------------------------
424-- * Building 'Text's
429-- * Building 'Text's
425
430
431+scanl :: (Char -> Char -> Char) -> Char -> Text -> Text
432+scanl f z t = unstream (S.scanl f z (stream t))
433+{-# INLINE scanl #-}
434+
426-- -----------------------------------------------------------------------------
435-- -----------------------------------------------------------------------------
427-- ** Generating and unfolding 'Text's
436-- ** Generating and unfolding 'Text's
428
437
Data/Text/Fusion.hs+16
@@ -60,6 +60,10 @@ module Data.Text.Fusion
60 , minimum
60 , minimum
61
61
62 -- * Construction
62 -- * Construction
63+ -- ** Scans
64+ , scanl
65+
66+ -- ** Generation and unfolding
63 , replicate
67 , replicate
64 , unfoldr
68 , unfoldr
65 , unfoldrN
69 , unfoldrN
@@ -540,6 +544,18 @@ minimum (Stream next0 s0 _len) = loop0_minimum s0
540-- -----------------------------------------------------------------------------
544-- -----------------------------------------------------------------------------
541-- * Building streams
545-- * Building streams
542
546
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+
543-- -----------------------------------------------------------------------------
559-- -----------------------------------------------------------------------------
544-- ** Generating and unfolding streams
560-- ** Generating and unfolding streams
545
561