Vectors
Filtering a Vector
Section titled “Filtering a Vector”Filter odd elements:
Prelude Data.Vector> Data.Vector.filter odd yfromList [1,3,5,7,9,11] :: Data.Vector.VectorMapping (map) and Reducing (fold) a Vector
Section titled “Mapping (map) and Reducing (fold) a Vector”Vectors can be map’d and fold'd,filter'd andzip`‘d:
Prelude Data.Vector> Data.Vector.map (^2) yfromList [0,1,4,9,16,25,36,49,64,81,100,121] :: Data.Vector.VectorReduce to a single value:
Prelude Data.Vector> Data.Vector.foldl (+) 0 y66Working on Multiple Vectors
Section titled “Working on Multiple Vectors”Zip two arrays into an array of pairs:
Prelude Data.Vector> Data.Vector.zip y yfromList [(0,0),(1,1),(2,2),(3,3),(4,4),(5,5),(6,6),(7,7),(8,8),(9,9),(10,10),(11,11)] :: Data.Vector.VectorThe Data.Vector Module
Section titled “The Data.Vector Module”The Data.Vector module provided by the vector is a high performance library for working with arrays.
Once you’ve imported Data.Vector, it’s easy to start using a Vector:
You can even have a multi-dimensional array:
Remarks
Section titled “Remarks”It [Data.Vector] has an emphasis on very high performance through loop fusion, whilst retaining a rich interface. The main data types are boxed and unboxed arrays, and arrays may be immutable (pure), or mutable. Arrays may hold Storable elements, suitable for passing to and from C, and you can convert between the array types. Arrays are indexed by non-negative Int values.
The Haskell Wiki has these recommendations:
In general:
-
- End users should use Data.Vector.Unboxed for most cases
- If you need to store more complex structures, use Data.Vector
- If you need to pass to C, use Data.Vector.Storable
-
- Use the generic interface, to ensure your library is maximally flexible: Data.Vector.Generic