packages feed

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

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

{- HLINT ignore "Avoid restricted function" -}
-- Reason: this module defines Papa's total head, tail, last and init (lenses
-- into a NonEmpty), whose names HLint restricts whatever module they come
-- from.

-- | Non-empty lists, with lenses for the parts that "Data.List" can only
-- give partially.
--
-- 'head', 'tail', 'last' and 'init' are lenses, not functions: read them with
-- 'Papa.Lens.Optic.the', and write them with 'Control.Lens.set' and
-- 'Control.Lens.over'. On a list, use 'Control.Lens._head',
-- 'Control.Lens._tail', 'Control.Lens._last' and 'Control.Lens._init',
-- which are traversals of at most one target.
module Papa.Base.Data.List.NonEmpty (
  module P,
  head,
  tail,
  last,
  init,
) where

import Control.Lens (Lens')
import Data.Functor (fmap)
import Data.List.NonEmpty as P (
  NonEmpty ((:|)),
  group,
  group1,
  groupAllWith,
  groupAllWith1,
  groupBy,
  groupBy1,
  groupWith,
  groupWith1,
  inits,
  insert,
  iterate,
  nonEmpty,
  repeat,
  scanl,
  scanl1,
  scanr,
  scanr1,
  some1,
  tails,
  unfold,
  xor,
 )
import qualified Data.List.NonEmpty as NE
import Papa.Lens.Optic (the)

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

-- | The first element of a 'NonEmpty'.
--
-- ==== __Examples__
--
-- >>> the head (1 :| [2, 3 :: Int])
-- 1
--
-- >>> set head 9 (1 :| [2, 3 :: Int])
-- 9 :| [2,3]
--
-- Lens laws:
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; set head (the head s) s === s
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInt; the head (set head a s) === a
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInt; b <- forAll genInt; set head b (set head a s) === set head b s
-- ...passed 100 tests.
-- True
--
-- @since 0.4.0
head :: Lens' (NonEmpty a) a
head f (h :| t) =
  fmap (:| t) (f h)
{-# INLINE head #-}

-- | Every element of a 'NonEmpty' after the first.
--
-- ==== __Examples__
--
-- >>> the tail (1 :| [2, 3 :: Int])
-- [2,3]
--
-- >>> set tail [] (1 :| [2, 3 :: Int])
-- 1 :| []
--
-- Lens laws:
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; set tail (the tail s) s === s
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInts; the tail (set tail a s) === a
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInts; b <- forAll genInts; set tail b (set tail a s) === set tail b s
-- ...passed 100 tests.
-- True
--
-- @since 0.4.0
tail :: Lens' (NonEmpty a) [a]
tail f (h :| t) =
  fmap (h :|) (f t)
{-# INLINE tail #-}

-- | The last element of a 'NonEmpty'.
--
-- ==== __Examples__
--
-- >>> the last (1 :| [2, 3 :: Int])
-- 3
--
-- >>> set last 9 (1 :| [2, 3 :: Int])
-- 1 :| [2,9]
--
-- Lens laws:
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; set last (the last s) s === s
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInt; the last (set last a s) === a
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInt; b <- forAll genInt; set last b (set last a s) === set last b s
-- ...passed 100 tests.
-- True
--
-- @since 0.4.0
last :: Lens' (NonEmpty a) a
last f (h :| t) =
  case t of
    [] -> fmap (:| []) (f h)
    x : xs -> fmap (h NE.<|) (last f (x :| xs))
{-# INLINEABLE last #-}

-- | Every element of a 'NonEmpty' before the last.
--
-- ==== __Examples__
--
-- >>> the init (1 :| [2, 3 :: Int])
-- [1,2]
--
-- >>> set init [] (1 :| [2, 3 :: Int])
-- 3 :| []
--
-- >>> set init [7, 8, 9] (1 :| [2, 3 :: Int])
-- 7 :| [8,9,3]
--
-- Lens laws:
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; set init (the init s) s === s
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInts; the init (set init a s) === a
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; a <- forAll genInts; b <- forAll genInts; set init b (set init a s) === set init b s
-- ...passed 100 tests.
-- True
--
-- @since 0.4.0
init :: Lens' (NonEmpty a) [a]
init f ne =
  fmap (\i -> NE.prependList i (the last ne :| [])) (f (initList ne))
{-# INLINE init #-}

-- | Every element of a 'NonEmpty' before the last, as a list.
--
-- >>> initList (1 :| [2, 3 :: Int])
-- [1,2]
--
-- >>> check $ withTests 100 $ property $ do s <- forAll genNonEmpty; initList s === the init s
-- ...passed 100 tests.
-- True
initList :: NonEmpty a -> [a]
initList (h :| t) =
  go h t
 where
  go _ [] = []
  go x (y : ys) = x : go y ys
{-# INLINEABLE initList #-}