on master help sign in
Implement and test lazy null
8396390Bryan O'Sullivan
Data/Text/Lazy.hs+15−1
@@ -48,7 +48,7 @@ module Data.Text.Lazy
48 , last
48 , last
49 , tail
49 , tail
50 , init
50 , init
51− -- , null
51+ , null
52 -- , length
52 -- , length
53
53
54 -- * Transformations
54 -- * Transformations
@@ -279,6 +279,20 @@ init Empty = emptyError "init"
279 unstream (S.init (stream t)) = init t
279 unstream (S.init (stream t)) = init t
280 #-}
280 #-}
281
281
282+-- | /O(1)/ Tests whether a 'Text' is empty or not. Subject to array
283+-- fusion.
284+null :: Text -> Bool
285+null Empty = True
286+null _ = False
287+{-# INLINE [1] null #-}
288+
289+{-# RULES
290+"TEXT null -> fused" [~1] forall t.
291+ null t = S.null (stream t)
292+"TEXT null -> unfused" [1] forall t.
293+ S.null (stream t) = null t
294+ #-}
295+
282-- | /O(1)/ Returns the last character of a 'Text', which must be
296-- | /O(1)/ Returns the last character of a 'Text', which must be
283-- non-empty. Subject to array fusion.
297-- non-empty. Subject to array fusion.
284last :: Text -> Char
298last :: Text -> Char
tests/Properties.hs+4
@@ -133,7 +133,9 @@ prop_TL_tail = tail `eqEP` (unpackT . TL.tail)
133prop_S_init = init `eqEP` (unpackT . S.init)
133prop_S_init = init `eqEP` (unpackT . S.init)
134prop_T_init = init `eqEP` (unpackT . T.init)
134prop_T_init = init `eqEP` (unpackT . T.init)
135prop_TL_init = init `eqEP` (unpackT . TL.init)
135prop_TL_init = init `eqEP` (unpackT . TL.init)
136+prop_S_null = null `eqP` S.null
136prop_T_null = null `eqP` T.null
137prop_T_null = null `eqP` T.null
138+prop_TL_null = null `eqP` TL.null
137prop_T_length = length `eqP` T.length
139prop_T_length = length `eqP` T.length
138prop_T_map f = map f `eqP` (unpackT . T.map f)
140prop_T_map f = map f `eqP` (unpackT . T.map f)
139prop_T_intercalate c = L.intercalate c `eq` (unpackT . T.intercalate (T.pack c) . map T.pack)
141prop_T_intercalate c = L.intercalate c `eq` (unpackT . T.intercalate (T.pack c) . map T.pack)
@@ -312,7 +314,9 @@ tests = [
312 ("prop_S_init", mytest prop_S_init),
314 ("prop_S_init", mytest prop_S_init),
313 ("prop_T_init", mytest prop_T_init),
315 ("prop_T_init", mytest prop_T_init),
314 ("prop_TL_init", mytest prop_TL_init),
316 ("prop_TL_init", mytest prop_TL_init),
317+ ("prop_S_null", mytest prop_S_null),
315 ("prop_T_null", mytest prop_T_null),
318 ("prop_T_null", mytest prop_T_null),
319+ ("prop_TL_null", mytest prop_TL_null),
316 ("prop_T_length", mytest prop_T_length),
320 ("prop_T_length", mytest prop_T_length),
317
321
318 ("prop_T_map", mytest prop_T_map),
322 ("prop_T_map", mytest prop_T_map),