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 -> Typedata 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

Instances

#singleton Source

singleton :: forall f a. Plus f => a -> NonEmpty f a

Create a non-empty structure with a single value.

import Prelude

singleton 1 == 1 :| []
singleton 1 == 1 :| Nil

#(:|) 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

#foldl1 Source

foldl1 :: forall f a. Foldable f => (a -> a -> a) -> NonEmpty f a -> a

Fold a non-empty structure, collecting results using a binary operation.

foldl1 (+) (1 :| [2, 3]) == 6

#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]

#head Source

head :: forall f a. NonEmpty f a -> a

Get the 'first' element of a non-empty container.

head (1 :| [2, 3]) == 1

#tail Source

tail :: forall f a. NonEmpty f a -> f a

Get everything but the 'first' element of a non-empty container.

tail (1 :| [2, 3]) == [2, 3]
Modules
Data.NonEmpty