packages feed

papa-0.7.0: src/Papa/Internal/Order.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

-- | Checks of the order of a list, for the preconditions of the
-- containers functions that build from an ordered list.
--
-- Internal to papa: not exported.
module Papa.Internal.Order (
  pairwise,
) where

import Data.Bool (Bool (True), (&&))

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Papa.Internal.Order
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range

-- | Whether a relation holds between every element of a list and the next.
-- 'Data.Bool.True' for a list of fewer than two elements.
--
-- ==== __Examples__
--
-- >>> pairwise (<=) [1, 2, 2, 3 :: Int]
-- True
--
-- >>> pairwise (<) [1, 2, 2, 3 :: Int]
-- False
--
-- >>> pairwise (<) ([] :: [Int])
-- True
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll (Gen.list (Range.linear 0 20) (Gen.int (Range.linear 0 9))); pairwise (<=) xs === (sort xs == xs)
-- ...passed 100 tests.
-- True
pairwise :: (a -> a -> Bool) -> [a] -> Bool
pairwise r =
  go
 where
  go (x : xs@(y : _)) =
    r x y && go xs
  go _ =
    True
{-# INLINE pairwise #-}