on master help sign in
Implement and test lazy init

--HG-- extra : convert_revision : 29c50bdf943f52ec8b0ddff552c7a9a8f93dcc25

commit95b326b84f6ae808f7b241644c83e51c62a8ad25 authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Mar 2009, 00:16 (18 years ago) parent751bf24
2 files changed+21−3
Data/Text/Lazy.hs+17−1 tests/Properties.hs+4−2
Data/Text/Lazy.hs+17−1
@@ -47,7 +47,7 @@ module Data.Text.Lazy
4747 , head
4848 , last
4949 , tail
50− -- , init
50+ , init
5151 -- , null
5252 -- , length
5353
@@ -251,6 +251,8 @@ head :: Text -> Char
251251head t = S.head (stream t)
252252{-# INLINE head #-}
253253
254+-- | /O(1)/ Returns all characters after the head of a 'Text', which
255+-- must be non-empty. Subject to array fusion.
254256tail :: Text -> Text
255257tail (Chunk t ts) = chunk (T.tail t) ts
256258tail Empty = emptyError "tail"
@@ -263,6 +265,20 @@ tail Empty = emptyError "tail"
263265 unstream (S.tail (stream t)) = tail t
264266 #-}
265267
268+init :: Text -> Text
269+init (Chunk t0 ts0) = go t0 ts0
270+ where go t (Chunk t' ts) = Chunk t (go t' ts)
271+ go t Empty = chunk (T.init t) Empty
272+init Empty = emptyError "init"
273+{-# INLINE [1] init #-}
274+
275+{-# RULES
276+"LAZY TEXT init -> fused" [~1] forall t.
277+ init t = unstream (S.init (stream t))
278+"LAZY TEXT init -> unfused" [1] forall t.
279+ unstream (S.init (stream t)) = init t
280+ #-}
281+
266282-- | /O(1)/ Returns the last character of a 'Text', which must be
267283-- non-empty. Subject to array fusion.
268284last :: Text -> Char
tests/Properties.hs+4−2
@@ -130,8 +130,9 @@ prop_TL_last = last `eqEP` TL.last
130130prop_S_tail = tail `eqEP` (unpackT . S.tail)
131131prop_T_tail = tail `eqEP` (unpackT . T.tail)
132132prop_TL_tail = tail `eqEP` (unpackT . TL.tail)
133+prop_S_init = init `eqEP` (unpackT . S.init)
133134prop_T_init = init `eqEP` (unpackT . T.init)
134−prop_T_initS = init `eqEP` (unpackT . S.unstream . S.init . S.stream)
135+prop_TL_init = init `eqEP` (unpackT . TL.init)
135136prop_T_null = null `eqP` T.null
136137prop_T_length = length `eqP` T.length
137138prop_T_map f = map f `eqP` (unpackT . T.map f)
@@ -308,8 +309,9 @@ tests = [
308309 ("prop_S_tail", mytest prop_S_tail),
309310 ("prop_T_tail", mytest prop_T_tail),
310311 ("prop_TL_tail", mytest prop_TL_tail),
312+ ("prop_S_init", mytest prop_S_init),
311313 ("prop_T_init", mytest prop_T_init),
312− ("prop_T_initS", mytest prop_T_initS),
314+ ("prop_TL_init", mytest prop_TL_init),
313315 ("prop_T_null", mytest prop_T_null),
314316 ("prop_T_length", mytest prop_T_length),
315317