-- |
-- 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
[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
$cshowsPrec :: forall a. Show a => Int -> NonEmpty a -> ShowS
showsPrec :: Int -> NonEmpty a -> ShowS
$cshow :: forall a. Show a => NonEmpty a -> String
show :: NonEmpty a -> String
$cshowList :: forall a. Show a => [NonEmpty a] -> ShowS
showList :: [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
$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
/= :: 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 b c a. (b -> c) -> (a -> b) -> a -> 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 (NonEmpty c)] -> c) -> [Item (NonEmpty c)] -> NonEmpty c
forall b c a. (b -> c) -> (a -> b) -> a -> 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
[Item (NonEmpty c)] -> c
forall l. IsList l => [Item l] -> l
fromList ([Item (NonEmpty c)] -> NonEmpty c)
-> [Item (NonEmpty c)] -> NonEmpty c
forall a b. (a -> b) -> a -> b
$ [Item (NonEmpty c)]
l