-- |
-- Module      : Basement.NonEmpty
-- License     : BSD-style
-- Maintainer  : Foundation
-- Stability   : experimental
-- Portability : portable
--
-- A newtype wrapper around a non-empty Collection.

module Basement.NonEmpty
    ( NonEmpty(..)
    ) where

import           Basement.Exception
import           Basement.Compat.Base

-- | NonEmpty property for any Collection
newtype NonEmpty a = NonEmpty { forall a. NonEmpty a -> a
getNonEmpty :: a }
    deriving (Int -> NonEmpty a -> ShowS
forall a. Show a => Int -> NonEmpty a -> ShowS
forall a. Show a => [NonEmpty a] -> ShowS
forall a. Show a => NonEmpty a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [NonEmpty a] -> ShowS
$cshowList :: forall a. Show a => [NonEmpty a] -> ShowS
show :: NonEmpty a -> String
$cshow :: forall a. Show a => NonEmpty a -> String
showsPrec :: Int -> NonEmpty a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> NonEmpty a -> ShowS
Show,NonEmpty a -> NonEmpty a -> Bool
forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NonEmpty a -> NonEmpty a -> Bool
$c/= :: forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
== :: NonEmpty a -> NonEmpty a -> Bool
$c== :: forall a. Eq a => NonEmpty a -> NonEmpty a -> Bool
Eq)

instance IsList c => IsList (NonEmpty c) where
    type Item (NonEmpty c) = Item c
    toList :: NonEmpty c -> [Item (NonEmpty c)]
toList      = forall l. IsList l => l -> [Item l]
toList forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall a. NonEmpty a -> a
getNonEmpty
    fromList :: [Item (NonEmpty c)] -> NonEmpty c
fromList [] = forall a e. Exception e => e -> a
throw NonEmptyCollectionIsEmpty
NonEmptyCollectionIsEmpty
    fromList [Item (NonEmpty c)]
l  = forall a. a -> NonEmpty a
NonEmpty forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall l. IsList l => [Item l] -> l
fromList forall a b. (a -> b) -> a -> b
$ [Item (NonEmpty c)]
l