on master help sign in
Implement and test lazy head
99344b7Bryan O'Sullivan
Data/Text/Lazy.hs+7−1
@@ -44,7 +44,7 @@ module Data.Text.Lazy
44 , snoc
44 , snoc
45 , append
45 , append
46 , uncons
46 , uncons
47− -- , head
47+ , head
48 -- , last
48 -- , last
49 -- , tail
49 -- , tail
50 -- , init
50 -- , init
@@ -244,6 +244,12 @@ uncons (Chunk t ts) =
244 if T.length t == 1 then ts else Chunk (T.unsafeTail t) ts)
244 if T.length t == 1 then ts else Chunk (T.unsafeTail t) ts)
245{-# INLINE uncons #-}
245{-# INLINE uncons #-}
246
246
247+-- | /O(1)/ Returns the first character of a 'Text', which must be
248+-- non-empty. Subject to array fusion.
249+head :: Text -> Char
250+head t = S.head (stream t)
251+{-# INLINE head #-}
252+
247-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
253-- | /O(n)/ 'splitAt' @n t@ returns a pair whose first element is a
248-- prefix of @t@ of length @n@, and whose second is the remainder of
254-- prefix of @t@ of length @n@, and whose second is the remainder of
249-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
255-- the string. It is equivalent to @('take' n t, 'drop' n t)@.
tests/Properties.hs+4
@@ -121,7 +121,9 @@ prop_T_uncons s = uncons `eqP` (fmap (second unpackT) . T.uncons)
121 where types = s :: String
121 where types = s :: String
122prop_TL_uncons s = uncons `eqP` (fmap (second unpackT) . TL.uncons)
122prop_TL_uncons s = uncons `eqP` (fmap (second unpackT) . TL.uncons)
123 where types = s :: String
123 where types = s :: String
124+prop_S_head = head `eqEP` S.head
124prop_T_head = head `eqEP` T.head
125prop_T_head = head `eqEP` T.head
126+prop_TL_head = head `eqEP` TL.head
125prop_T_last = last `eqEP` T.last
127prop_T_last = last `eqEP` T.last
126prop_T_lastS = last `eqEP` (S.last . S.stream)
128prop_T_lastS = last `eqEP` (S.last . S.stream)
127prop_T_tail = tail `eqEP` (unpackT . T.tail)
129prop_T_tail = tail `eqEP` (unpackT . T.tail)
@@ -295,7 +297,9 @@ tests = [
295 ("prop_T_appendS", mytest prop_T_appendS),
297 ("prop_T_appendS", mytest prop_T_appendS),
296 ("prop_T_uncons", mytest prop_T_uncons),
298 ("prop_T_uncons", mytest prop_T_uncons),
297 ("prop_TL_uncons", mytest prop_TL_uncons),
299 ("prop_TL_uncons", mytest prop_TL_uncons),
300+ ("prop_S_head", mytest prop_S_head),
298 ("prop_T_head", mytest prop_T_head),
301 ("prop_T_head", mytest prop_T_head),
302+ ("prop_TL_head", mytest prop_TL_head),
299 ("prop_T_last", mytest prop_T_last),
303 ("prop_T_last", mytest prop_T_last),
300 ("prop_T_lastS", mytest prop_T_lastS),
304 ("prop_T_lastS", mytest prop_T_lastS),
301 ("prop_T_tail", mytest prop_T_tail),
305 ("prop_T_tail", mytest prop_T_tail),