packages feed

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

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

module Papa.Base.Data.List (
  module P,
  lookup,
  (++),
) where

import Data.List as P (
  all,
  and,
  any,
  break,
  cycle,
  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,
  nub,
  nubBy,
  null,
  or,
  partition,
  permutations,
  product,
  replicate,
  reverse,
  sort,
  sortBy,
  sortOn,
  span,
  splitAt,
  stripPrefix,
  subsequences,
  sum,
  take,
  takeWhile,
  transpose,
  union,
  unionBy,
  unlines,
  unwords,
  words,
  (\\),
#if MIN_VERSION_base(4,19,0)
  -- Added in base 4.19 (GHC 9.8)
  singleton,
  uncons,
  unsnoc,
  (!?),
#endif
 )

import Data.Eq (Eq ((==)))
import Data.Foldable (Foldable)
import Data.Maybe (Maybe (Just, Nothing))
import Data.Semigroup (Semigroup ((<>)))

-- | Look up a key in an association list.
--
-- >>> import Data.Maybe (Maybe(..))
{-# INLINE lookup #-}
-- >>> lookup 2 [(1,'a'), (2,'b'), (3,'c')]
-- Just 'b'
-- >>> lookup 4 [(1,'a'), (2,'b'), (3,'c')]
-- Nothing
lookup ::
  (Eq a, Foldable f) =>
  a ->
  f (a, b) ->
  Maybe b
lookup a =
  foldr (\(x, y) b -> if x == a then Just y else b) Nothing

-- | Append two semigroups.
--
-- >>> [1,2] ++ [3,4]
-- [1,2,3,4]
-- >>> "hello" ++ " world"
-- "hello world"
{-# SPECIALIZE (++) ::
  [a] ->
  [a] ->
  [a]
  #-}
(++) ::
  (Semigroup a) =>
  a ->
  a ->
  a
(++) =
  (<>)