-- |
-- 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 { NonEmpty a -> a
getNonEmpty :: a }
    deriving (Int -> NonEmpty a -> ShowS
[NonEmpty a] -> ShowS
NonEmpty a -> String
(Int -> NonEmpty a -> ShowS)
-> (NonEmpty a -> String)
-> ([NonEmpty a] -> ShowS)
-> Show (NonEmpty a)
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
(NonEmpty a -> NonEmpty a -> Bool)
-> (NonEmpty a -> NonEmpty a -> Bool) -> Eq (NonEmpty a)
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      = c -> [Item c]
forall l. IsList l => l -> [Item l]
toList (c -> [Item c]) -> (NonEmpty c -> c) -> NonEmpty c -> [Item c]
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NonEmpty c -> c
forall a. NonEmpty a -> a
getNonEmpty
    fromList :: [Item (NonEmpty c)] -> NonEmpty c
fromList [] = NonEmptyCollectionIsEmpty -> NonEmpty c
forall a e. Exception e => e -> a
throw NonEmptyCollectionIsEmpty
NonEmptyCollectionIsEmpty
    fromList [Item (NonEmpty c)]
l  = c -> NonEmpty c
forall a. a -> NonEmpty a
NonEmpty (c -> NonEmpty c) -> ([Item c] -> c) -> [Item c] -> NonEmpty c
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Item c] -> c
forall l. IsList l => [Item l] -> l
fromList ([Item c] -> NonEmpty c) -> [Item c] -> NonEmpty c
forall a b. (a -> b) -> a -> b
$ [Item c]
[Item (NonEmpty c)]
l