vector-extras-0.2.8: Utilities for the "vector" library
Safe HaskellSafe-Inferred
LanguageHaskell2010

VectorExtras.Accumulator

Contents

Description

Utility for construction of vectors when the size is not known ahead.

Synopsis

Documentation

data Accumulator a Source #

Constructor of vectors optimised for appending elements one by one, providing for \(\mathcal{O}(n)\) complexity of the whole construction process.

Very useful as accumulator in folds.

Under the hood it is the size counter and a reverse list of elements. When executed, a vector of the according size gets allocated and gets populated with the elements in reverse order (starting from the last element).

init :: Accumulator a Source #

Create an empty accumulator.

add :: a -> Accumulator a -> Accumulator a Source #

Add an element to the accumulator.

addList :: [a] -> Accumulator a -> Accumulator a Source #

Add a list of elements to the accumulator.

addFoldable :: Foldable f => f a -> Accumulator a -> Accumulator a Source #

Add a foldable of elements to the accumulator.

Execution

toVector :: Vector v a => Accumulator a -> v a Source #

Finalise the accumulator as vector.

toReverseVector :: Vector v a => Accumulator a -> v a Source #

Finalise the accumulator as vector in reverse order.