nonempty-alternative-0.4.0: NonEmpty for Alternative types

Safe HaskellSafe
LanguageHaskell2010

Data.NonEmpty

Contents

Description

This package extends NonEmpty from semigroups to arbitrary Alternative types. The method is the same as for lists, by separating an element from the rest.

There are two natural ways to merge an element x to the rest of the structure xs. The first gives rise to NonEmptyL:

flattenL :: NonEmptyL f a -> f a
flattenL (x :<: xs) = pure x <|> xs

The second gives rise to NonEmptyR:

flattenR :: NonEmptyR f a -> f a
flattenR (xs :>: x) = xs <|> pure x

The instances are made so that flattenL gives a type class morphism between NonEmptyL List and List, and flattenR gives the same for NonEmptyR RList and RList from the package rlist.

Synopsis

Left Non-Empty Alternatives

data NonEmptyL f a Source

The type NonEmptyL is well suited for cons structures.

Constructors

a :<: (f a) infixr 5 

Instances

Basic functions for NonEmptyL

headL :: NonEmptyL f a -> a Source

Extracts the structure's singular element. This function is total and equivalent to extract from Comonad.

tailL :: NonEmptyL f a -> f a Source

Extracts the structure's remaining data. This function is total.

flattenL :: Alternative f => NonEmptyL f a -> f a Source

Flattens the structure to its base type from the left.

joinL :: (Alternative f, Monad f) => NonEmptyL f (NonEmptyL f a) -> NonEmptyL f a Source

This is equivalent to join for Monad.

budgeL :: (Alternative f, Alternative g) => NonEmptyL f (g a) -> NonEmptyL f (g a) Source

Budge the head into the remaining structure from the left, adding an empty head.

Right Non-Empty Alternatives

data NonEmptyR f a Source

The type NonEmptyR is well suited for snoc structures.

Constructors

(f a) :>: a infixl 5 

Instances

Basic functions for NonEmptyR

lastR :: NonEmptyR f a -> a Source

Extracts the structure's singular element. This function is total and equivalent to extract from Comonad.

initR :: NonEmptyR f a -> f a Source

Extracts the structure's remaining data. This function is total.

flattenR :: Alternative f => NonEmptyR f a -> f a Source

Flattens the structure to its base type from the right.

joinR :: (Alternative f, Monad f) => NonEmptyR f (NonEmptyR f a) -> NonEmptyR f a Source

This is equivalent to join for Monad.

budgeR :: (Alternative f, Alternative g) => NonEmptyR f (g a) -> NonEmptyR f (g a) Source

Budge the head into the remaining structure from the right, adding an empty head.