packages feed

papa-0.7.0: src/Papa/Base/Data/List.hs

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

-- | Lists, with total versions of the partial functions of "Data.List".
--
-- @uncons@ and @unsnoc@ are not re-exported from "Data.List": the
-- generalised ones of "Control.Lens" are exported by "Papa".
module Papa.Base.Data.List (
  module P,

  -- * Removing duplicates
  -- $nub
  nub,
  nubBy,
  cycle,
  lookup,
  (!?),
  (++),
) where

import Control.Category ((.))
import Control.Lens (Ixed (ix), filtered, folded, has, only, preview, _1, _2)
import Data.Eq (Eq)
import Data.Foldable (Foldable)
import Data.Functor (fmap)
import Data.Int (Int)
import Data.List (nub, nubBy)
import Data.List as P (
  all,
  and,
  any,
  break,
  delete,
  deleteBy,
  deleteFirstsBy,
  drop,
  dropWhile,
  dropWhileEnd,
  elem,
  find,
  foldl,
  foldl',
  foldr,
  genericDrop,
  genericLength,
  genericReplicate,
  genericSplitAt,
  genericTake,
  insertBy,
  intercalate,
  intersect,
  intersectBy,
  intersperse,
  isInfixOf,
  isPrefixOf,
  isSubsequenceOf,
  isSuffixOf,
  length,
  lines,
  mapAccumL,
  mapAccumR,
  notElem,
  null,
  or,
  partition,
  permutations,
  product,
  replicate,
  reverse,
  singleton,
  sort,
  sortBy,
  sortOn,
  span,
  splitAt,
  stripPrefix,
  subsequences,
  sum,
  take,
  takeWhile,
  transpose,
  union,
  unionBy,
  unlines,
  unwords,
  unzip,
  words,
  (\\),
 )
import qualified Data.List.NonEmpty as NE
import Data.Maybe (Maybe)
import Data.Semigroup (Semigroup ((<>)))

-- $nub
-- 'nub' and 'nubBy' take /O(n²)/ time: each element is compared with every
-- element kept before it. Where the elements are 'Data.Ord.Ord',
-- 'Papa.Containers.Data.Containers.ListUtils.nubOrd' and
-- 'Papa.Containers.Data.Containers.ListUtils.nubOrdOn' do the same in
-- /O(n log n)/, and at 'Int', 'Papa.Containers.Data.Containers.ListUtils.nubInt'
-- and 'Papa.Containers.Data.Containers.ListUtils.nubIntOn' faster still.

-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genInts = Gen.list (Range.linear 0 20) (Gen.int (Range.linear (-100) 100))

-- | Total @cycle@: the infinite repetition of a list. 'Data.Maybe.Nothing' for the
-- empty list, where "Data.List"'s @cycle@ throws.
--
-- ==== __Examples__
--
-- >>> fmap (take 7) (cycle [1, 2, 3 :: Int])
-- Just [1,2,3,1,2,3,1]
--
-- >>> cycle ([] :: [Int])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; fmap (take (length xs)) (cycle xs) === (xs <$ preview _head xs)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
cycle :: [a] -> Maybe [a]
cycle =
  fmap (NE.toList . NE.cycle) . NE.nonEmpty
{-# INLINE cycle #-}

-- | The value of the first pair whose key is equal to the given key, in any
-- 'Foldable' of pairs.
--
-- ==== __Examples__
--
-- >>> lookup 'b' [('a', 1), ('b', 2 :: Int)]
-- Just 2
--
-- >>> lookup 'z' [('a', 1 :: Int)]
-- Nothing
--
-- >>> lookup 'a' (Just ('a', "x"))
-- Just "x"
--
-- >>> check $ withTests 100 $ property $ do k <- forAll (Gen.int (Range.linear 0 5)); kvs <- forAll (Gen.list (Range.linear 0 10) ((,) <$> Gen.int (Range.linear 0 5) <*> Gen.alpha)); lookup k kvs === fmap snd (find ((== k) . fst) kvs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
lookup :: (Eq a, Foldable f) => a -> f (a, b) -> Maybe b
lookup a =
  preview (folded . filtered (has (_1 . only a)) . _2)
{-# INLINE lookup #-}

-- | Total list indexing. 'Data.Maybe.Nothing' for an index out of range.
--
-- ==== __Examples__
--
-- >>> [10, 20, 30 :: Int] !? 1
-- Just 20
--
-- >>> [10, 20, 30 :: Int] !? 3
-- Nothing
--
-- >>> [10, 20, 30 :: Int] !? (-1)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; i <- forAll (Gen.int (Range.linear (-5) 25)); (xs !? i) === preview (ix i) xs
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
(!?) :: [a] -> Int -> Maybe a
xs !? n =
  preview (ix n) xs
{-# INLINE (!?) #-}

infixl 9 !?

-- | Append, generalised from lists to any 'Semigroup'.
--
-- ==== __Examples__
--
-- >>> "abc" ++ "def"
-- "abcdef"
--
-- >>> Just [1 :: Int] ++ Nothing ++ Just [2]
-- Just [1,2]
--
-- Right-associative at precedence 5, as in "Prelude":
--
-- >>> 1 `elem` [2] ++ [1 :: Int]
-- True
--
-- >>> check $ withTests 100 $ property $ do xs <- forAll genInts; ys <- forAll genInts; zs <- forAll genInts; (xs ++ ys) ++ zs === xs ++ (ys ++ zs)
-- ...passed 100 tests.
-- True
--
-- @since 0.1.0
(++) :: (Semigroup a) => a -> a -> a
(++) =
  (<>)
{-# INLINE (++) #-}

infixr 5 ++