The Haskell language provides the list type ([a]
) for values that correspond to a finite sequence of values of the same type (a
). Other packages export other types that do the same: containers
exports Seq a
and vector
exports Vector a
from module Data.Vector
and other types from other modules. The values of the different types have different underlying representations and have different performance for similar operations.
I wanted to convert efficiently from a Vector a
value to a Seq a
value and back again, in part because the package hgeometry
makes use of Seq a
in the representation of polygonal lines.
IsList
Seq a
and Vector a
each provide instances of typeclass IsList
, which provides functions toList
and fromList
. However, converting via a list using fromList . toList
was unlikely to be efficient.
fromFunction
Module Data.Sequence
exports fromFunction :: Int -> (Int -> a) -> Seq a
and indexing is fast for values of type Vector a
. So, I had:
1 2 3 4 |
import qualified Data.Vector as V fromVector :: V.Vector a -> Seq a fromVector v = fromFunction (V.length v) (v V.!) |
vector-builder package
Seq
provides an instance of typeclass Foldable
and package vector-builder
exports two functions:
build :: Data.Vector.Generic.Vector v a => Builder a -> v a
foldable :: Foldable f => f a -> Builder a
So, I had:
1 2 3 4 5 6 |
import qualified Data.Vector as V import VectorBuilder.Builder (build) import VectorBuilder.Vector (foldable) toVector :: Seq a -> V.Vector a toVector = build . foldable |