Module

# Data.NonEmpty

- Package
- purescript-nonempty
- Repository
- purescript/purescript-nonempty

This module defines a generic non-empty data structure, which adds an additional element to any container type.

### #NonEmpty Source

`data NonEmpty :: (Type -> Type) -> Type -> Type`

`data NonEmpty f a`

A non-empty container of elements of type a.

```
import Data.NonEmpty
nonEmptyArray :: NonEmpty Array Int
nonEmptyArray = NonEmpty 1 [2,3]
import Data.List(List(..), (:))
nonEmptyList :: NonEmpty List Int
nonEmptyList = NonEmpty 1 (2 : 3 : Nil)
```

#### Constructors

`NonEmpty a (f a)`

#### Instances

`(Show a, Show (f a)) => Show (NonEmpty f a)`

`(Eq1 f, Eq a) => Eq (NonEmpty f a)`

`(Eq1 f) => Eq1 (NonEmpty f)`

`(Ord1 f, Ord a) => Ord (NonEmpty f a)`

`(Ord1 f) => Ord1 (NonEmpty f)`

`(Functor f) => Functor (NonEmpty f)`

`(FunctorWithIndex i f) => FunctorWithIndex (Maybe i) (NonEmpty f)`

`(Foldable f) => Foldable (NonEmpty f)`

`(FoldableWithIndex i f) => FoldableWithIndex (Maybe i) (NonEmpty f)`

`(Traversable f) => Traversable (NonEmpty f)`

`(TraversableWithIndex i f) => TraversableWithIndex (Maybe i) (NonEmpty f)`

`(Foldable f) => Foldable1 (NonEmpty f)`

`(Unfoldable f) => Unfoldable1 (NonEmpty f)`

`(Applicative f, Semigroup (f a)) => Semigroup (NonEmpty f a)`

### #(:|) Source

Operator alias for Data.NonEmpty.NonEmpty *(right-associative / precedence 5)*

An infix synonym for `NonEmpty`

.

```
nonEmptyArray :: NonEmpty Array Int
nonEmptyArray = 1 :| [2,3]
nonEmptyList :: NonEmpty List Int
nonEmptyList = 1 :| 2 : 3 : Nil
```

### #fromNonEmpty Source

`fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r`

Apply a function that takes the `first`

element and remaining elements
as arguments to a non-empty container.

For example, return the remaining elements multiplied by the first element:

```
fromNonEmpty (\x xs -> map (_ * x) xs) (3 :| [2, 1]) == [6, 3]
```

### #oneOf Source

`oneOf :: forall f a. Alternative f => NonEmpty f a -> f a`

Returns the `alt`

(`<|>`

) result of:

- The first element lifted to the container of the remaining elements.
- The remaining elements.

```
import Data.Maybe(Maybe(..))
oneOf (1 :| Nothing) == Just 1
oneOf (1 :| Just 2) == Just 1
oneOf (1 :| [2, 3]) == [1,2,3]
```

- Modules
- Data.
NonEmpty

This is a lawful

`Semigroup`

instance that will behave sensibly for common nonempty containers like lists and arrays. However, it's not guaranteed that`pure`

will behave sensibly alongside`<>`

for all types, as we don't have any laws which govern their behavior.