packages feed

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

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Avoid restricted function" -}

module Papa.Lens.Data.List (
  (!!),
) where

import Control.Lens (Index, IxValue, Ixed, ix, (^?))
import Data.Int (Int)
import Data.Maybe (Maybe)

-- | Safe list indexing. Returns 'Nothing' if the index is out of bounds.
--
-- >>> import Data.Maybe (Maybe(..))
-- >>> [1,2,3,4] !! 0
-- Just 1
-- >>> [1,2,3,4] !! 2
-- Just 3
-- >>> [1,2,3,4] !! 10
-- Nothing
{-# INLINE (!!) #-}
{-# SPECIALIZE (!!) ::
  [a] ->
  Int ->
  Maybe a
  #-}
(!!) ::
  (Ixed s) =>
  s ->
  Index s ->
  Maybe (IxValue s)
q !! n =
  q ^? ix n

infixl 9 !!