diff --git a/docs/Data/NonEmpty.md b/docs/Data/NonEmpty.md index a942576..a22504c 100644 --- a/docs/Data/NonEmpty.md +++ b/docs/Data/NonEmpty.md @@ -21,12 +21,12 @@ nonEmptyList = 0 :| empty ##### Instances ``` purescript -instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) -instance eqNonEmpty :: (Eq a, Eq (f a)) => Eq (NonEmpty f a) -instance ordNonEmpty :: (Ord a, Ord (f a)) => Ord (NonEmpty f a) -instance functorNonEmpty :: (Functor f) => Functor (NonEmpty f) -instance foldableNonEmpty :: (Foldable f) => Foldable (NonEmpty f) -instance traversableNonEmpty :: (Traversable f) => Traversable (NonEmpty f) +(Show a, Show (f a)) => Show (NonEmpty f a) +(Eq a, Eq (f a)) => Eq (NonEmpty f a) +(Ord a, Ord (f a)) => Ord (NonEmpty f a) +(Functor f) => Functor (NonEmpty f) +(Foldable f) => Foldable (NonEmpty f) +(Traversable f) => Traversable (NonEmpty f) ``` #### `singleton` @@ -50,7 +50,7 @@ An infix synonym for `NonEmpty`. #### `foldl1` ``` purescript -foldl1 :: forall f a s. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a +foldl1 :: forall f a. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a ``` Fold a non-empty structure, collecting results using a binary operation. @@ -83,4 +83,20 @@ fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a ``` +#### `head` + +``` purescript +head :: forall f a. NonEmpty f a -> a +``` + +Get the 'first' element of a non-empty container. + +#### `tail` + +``` purescript +tail :: forall f a. NonEmpty f a -> f a +``` + +Get everything but the 'first' element of a non-empty container. + diff --git a/src/Data/NonEmpty.purs b/src/Data/NonEmpty.purs index cdb6281..d305d3a 100644 --- a/src/Data/NonEmpty.purs +++ b/src/Data/NonEmpty.purs @@ -10,6 +10,8 @@ module Data.NonEmpty , fold1 , fromNonEmpty , oneOf + , head + , tail ) where import Prelude @@ -59,6 +61,14 @@ fromNonEmpty f (NonEmpty a fa) = a `f` fa oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a oneOf (NonEmpty a fa) = pure a <|> fa +-- | Get the 'first' element of a non-empty container. +head :: forall f a. NonEmpty f a -> a +head (NonEmpty x _) = x + +-- | Get everything but the 'first' element of a non-empty container. +tail :: forall f a. NonEmpty f a -> f a +tail (NonEmpty _ xs) = xs + instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) where show (NonEmpty a fa) = "(NonEmpty " ++ show a ++ " " ++ show fa ++ ")"