utility-ht-0.0.17.1: Various small helper functions for Lists, Maybes, Tuples, Functions
Safe HaskellSafe-Inferred
LanguageHaskell98

Data.List.Reverse.StrictElement

Description

The functions in this module process the list formally from the end. Actually they traverse the list from the start and check every element. This way they are strict in the elements and lazy in the list spline. Thus you can apply them to infinite lists. Use these functions if the list is long or the test is cheap.

Synopsis

Documentation

>>> import Test.Utility (forAllPredicates, defined)
>>> import qualified Data.List.Reverse.StrictElement as Rev
>>> import qualified Data.List.Match as Match
>>> import qualified Data.List as List
>>> import Data.Tuple.HT (mapPair, swap)
>>> 
>>> _suppressUnusedImportWarning :: (a -> Bool) -> [a] -> [a]
>>> _suppressUnusedImportWarning = Data.List.Reverse.StrictElement.dropWhile

dropWhile :: (a -> Bool) -> [a] -> [a] Source #

Remove the longest suffix of elements satisfying p. In contrast to reverse . dropWhile p . reverse this works for infinite lists, too.

forAllPredicates $ \p xs -> Rev.dropWhile p xs == reverse (List.dropWhile p (reverse xs))
\x xs pad -> defined $ Match.take (pad::[()]) $ Rev.dropWhile ((x::Char)/=) $ cycle $ x:xs

takeWhile :: (a -> Bool) -> [a] -> [a] Source #

Alternative version of reverse . takeWhile p . reverse.

forAllPredicates $ \p xs -> Rev.takeWhile p xs == reverse (List.takeWhile p (reverse xs))

span :: (a -> Bool) -> [a] -> ([a], [a]) Source #

forAllPredicates $ \p xs -> Rev.span p xs == swap (mapPair (reverse, reverse) (List.span p (reverse xs)))
forAllPredicates $ \p xs -> Rev.span p xs == (Rev.dropWhile p xs, Rev.takeWhile p xs)
\x xs pad -> defined $ Match.take (pad::[()]) $ fst $ Rev.span ((x::Char)/=) $ cycle $ x:xs