HORIZON HASKELLDocslts/ghc-9.14.x45dd5372026-10-11Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · 45dd537 · 2026-10-11

Modulenonempty-containers-0.3.6.0Haskell2010

Data.Sequence.NonEmpty.Internal

Unsafe internal-use functions used in the implementation of Data.Sequence.NonEmpty. These functions can potentially be used to break the abstraction of NESeq and produce unsound sequences, so be wary!

  • 1 type
  • 18 values
datadata NESeq a
#

A general-purpose non-empty (by construction) finite sequence type.

Non-emptiness means that:

  • Functions that take an NESeq can safely operate on it with the assumption that it has at least value.

  • Functions that return an NESeq provide an assurance that the result has at least one value.

Data.Sequence.NonEmpty re-exports the API of Data.Sequence, faithfully reproducing asymptotics, typeclass constraints, and semantics. Functions that ensure that input and output maps are both non-empty (like <|) return NESeq, but functions that might potentially return an empty map (like tail) return a Seq instead.

You can directly construct an NESeq with the API from Data.Sequence.NonEmpty; it's more or less the same as constructing a normal Seq, except you don't have access to Data.Seq.empty. There are also a few ways to construct an NESeq from a Seq:

  1. The nonEmptySeq smart constructor will convert a Seq a into a Maybe (NESeq a), returning Nothing if the original Seq was empty.

  2. You can use :<||, :||>, and insertSeqAt to insert a value into a Seq to create a guaranteed NESeq.

  3. You can use the IsNonEmpty and IsEmpty patterns to "pattern match" on a Seq to reveal it as either containing a NESeq or an empty sequence.

  4. withNonEmpty offers a continuation-based interface for deconstructing a Seq and treating it as if it were an NESeq.

You can convert an NESeq into a Seq with toSeq or IsNonEmpty, essentially "obscuring" the non-empty property from the type.

Constructors

Instances34Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
  • Monad NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Functor NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • MonadFix NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Applicative NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Foldable NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal

    foldr1, foldl1, maximum, and minimum are all total, unlike for Seq.

  • Traversable NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • MonadZip NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
    mzipWith = zipWith
    munzip = unzip
  • Foldable1 NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Eq1 NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Ord1 NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Read1 NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Show1 NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Comonad NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Alt NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Apply NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Bind NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Extend NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Traversable1 NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Invariant NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • FoldableWithIndex Int NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • FunctorWithIndex Int NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • TraversableWithIndex Int NESeqDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • IsList (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Eq a => Eq (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Data a => Data (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Ord a => Ord (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Read a => Read (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Show a => Show (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • Semigroup (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • NFData a => NFData (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • FromJSON a => FromJSON (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • ToJSON a => ToJSON (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
  • IsNonEmptyList (NESeq a)Defined in nonempty-containers-0.3.6.0 · Data.Containers.NonEmpty.List
  • type Item (NESeq a) = aDefined in nonempty-containers-0.3.6.0 · Data.Sequence.NonEmpty.Internal
patternpattern (:<||) :: a -> Seq a -> NESeq a
#

O(1). An abstract constructor for an NESeq that consists of a "head" a and a "tail" Seq a. Similar to :| for NonEmpty.

Can be used to match on the head and tail of an NESeq, and also used to construct an NESeq by consing an item to the beginnong of a Seq, ensuring that the result is non-empty.

patternpattern (:||>) :: Seq a -> a -> NESeq a
#

O(1). An abstract constructor for an NESeq that consists of a "init" Seq a and a "last" a. Similar to :| for NonEmpty, but at the end of the list instead of at the beginning.

Can be used to match on the init and last of an NESeq, and also used to construct an NESeq by snocing an item to the end of a Seq, ensuring that the result is non-empty.

valuetoSeq :: NESeq a -> Seq a
#

O(1). Convert a non-empty sequence back into a normal possibly-empty sequence, for usage with functions that expect Seq.

Can be thought of as "obscuring" the non-emptiness of the map in its type. See the Data.Sequence.NonEmpty.IsNotEmpty pattern.

nonEmptySeq and maybe Data.Seq.empty toSeq form an isomorphism: they are perfect structure-preserving inverses of eachother.

valuelength :: NESeq a -> Int
#

O(1) . The number of elements in the sequence.

valuefromFunction :: Int -> (Int -> a) -> NESeq a
#

O(n) . Convert a given sequence length and a function representing that sequence into a sequence.

valuereplicate :: Int -> a -> NESeq a
#

O(\log n) . replicate n x is a sequence consisting of n copies of x. Is only defined when n is positive.

valueindex :: NESeq a -> Int -> a
#

O(\log(\min(i,n-i))) . The element at the specified position, counting from 0. The argument should thus be a non-negative integer less than the size of the sequence. If the position is out of range, index fails with an error.

Property
xs `index` i = toList xs !! i

Caution: index necessarily delays retrieving the requested element until the result is forced. It can therefore lead to a space leak if the result is stored, unforced, in another structure. To retrieve an element immediately without forcing it, use lookup or (!?).

value(<|) :: a -> NESeq a -> NESeq a
#

O(1) . Add an element to the left end of a non-empty sequence. Mnemonic: a triangle with the single element at the pointy end.

value(><) :: NESeq a -> NESeq a -> NESeq a
#

O(\log(\min(n_1,n_2))) . Concatenate two non-empty sequences.

value(|><) :: NESeq a -> Seq a -> NESeq a
#

O(\log(\min(n_1,n_2))) . Concatenate a non-empty sequence with a potentially empty sequence (Seq), to produce a guaranteed non-empty sequence. Mnemonic: like ><, but a pipe for the guarunteed non-empty side.

valuemap :: (a -> b) -> NESeq a -> NESeq b
#

Defined here but hidden; intended for use with RULES pragma.

valuetails :: NESeq a -> NESeq (NESeq a)
#

O(n) . Returns a sequence of all non-empty suffixes of this sequence, longest first. For example,

tails (fromList (1:|[2,3])) = fromList (fromList (1:|[2,3]) :| [fromList (2:|[3]), fromList (3:|[])])

Evaluating the i th suffix takes O(\log(\min(i, n-i))) , but evaluating every suffix in the sequence takes O(n) due to sharing.

valuezip :: NESeq a -> NESeq b -> NESeq (a, b)
#

O(\min(n_1,n_2)) . zip takes two sequences and returns a sequence of corresponding pairs. If one input is short, excess elements are discarded from the right end of the longer sequence.

valuezipWith :: (a -> b -> c) -> NESeq a -> NESeq b -> NESeq c
#

O(\min(n_1,n_2)) . zipWith generalizes zip by zipping with the function given as the first argument, instead of a tupling function. For example, zipWith (+) is applied to two sequences to take the sequence of corresponding sums.

valueunzip :: NESeq (a, b) -> (NESeq a, NESeq b)
#

Unzip a sequence of pairs.

unzip ps = ps `seq` (fmap fst ps) (fmap snd ps)

Example:

unzip $ fromList ((1,"a") :| [(2,"b"), (3,"c")]) =
  (fromList (1:|[2,3]), fromList ("a":|["b","c"]))

See the note about efficiency at unzipWith.