Copyright | (c) Laurent P. René de Cotret |
---|---|
License | MIT |
Maintainer | laurent.decotret@outlook.com |
Portability | portable |
Safe Haskell | Safe-Inferred |
Language | GHC2021 |
Data.Series.Index.Internal
Description
WARNING
This module is considered internal. It contains functions
which may be unsafe to use in general, for example requiring
the data to be pre-sorted like fromDistinctAscList
.
The Package Versioning Policy still applies.
Synopsis
- newtype Index k = MkIndex (Set k)
- fromAscList :: Eq k => [k] -> Index k
- fromDistinctAscList :: [k] -> Index k
- fromAscVector :: (Vector v k, Ord k) => v k -> Index k
- fromDistinctAscVector :: Vector v k => v k -> Index k
- mapMonotonic :: (k -> g) -> Index k -> Index g
- elemAt :: HasCallStack => Int -> Index k -> k
- findIndex :: HasCallStack => Ord k => k -> Index k -> Int
Documentation
Representation of the index of a series.
An index is a sequence of sorted elements. All elements are unique, much like a Set
.
You can construct an Index
from a set (fromSet
), from a list (fromList
), or from a vector (fromVector
). You can
also make use of the OverloadedLists
extension:
>>>
:set -XOverloadedLists
>>>
let (ix :: Index Int) = [1, 2, 3]
>>>
ix
Index [1,2,3]
Since keys in an Index
are always sorted and unique, Index
is not a Functor
. To map a function
over an Index
, use map
.
Instances
Foldable Index Source # | |
Defined in Data.Series.Index.Definition Methods fold :: Monoid m => Index m -> m # foldMap :: Monoid m => (a -> m) -> Index a -> m # foldMap' :: Monoid m => (a -> m) -> Index a -> m # foldr :: (a -> b -> b) -> b -> Index a -> b # foldr' :: (a -> b -> b) -> b -> Index a -> b # foldl :: (b -> a -> b) -> b -> Index a -> b # foldl' :: (b -> a -> b) -> b -> Index a -> b # foldr1 :: (a -> a -> a) -> Index a -> a # foldl1 :: (a -> a -> a) -> Index a -> a # elem :: Eq a => a -> Index a -> Bool # maximum :: Ord a => Index a -> a # minimum :: Ord a => Index a -> a # | |
Selection Index Source # | |
Ord k => Monoid (Index k) Source # | |
Ord k => Semigroup (Index k) Source # | |
Ord k => IsList (Index k) Source # | |
Show k => Show (Index k) Source # | |
NFData k => NFData (Index k) Source # | |
Defined in Data.Series.Index.Definition | |
Eq k => Eq (Index k) Source # | |
Ord k => Ord (Index k) Source # | |
Defined in Data.Series.Index.Definition | |
type Item (Index k) Source # | |
Defined in Data.Series.Index.Definition |
Unsafe construction
fromAscList :: Eq k => [k] -> Index k Source #
fromDistinctAscList :: [k] -> Index k Source #
\(O(n)\) Build an Index
from a list of distinct elements in ascending order. The precondition
that elements be unique and sorted is not checked.
fromAscVector :: (Vector v k, Ord k) => v k -> Index k Source #
\(O(n \log n)\) Build an Index
from a Vector
of elements in ascending order. The precondition
that elements already be sorted is not checked.
Note that since an Index
is composed of unique elements,
the length of the index may not be the same as the length of the input vector:
>>>
import Data.Vector as V
>>>
fromAscVector $ V.fromList ['a', 'b', 'b', 'c']
Index "abc"
fromDistinctAscVector :: Vector v k => v k -> Index k Source #
Unsafe functions with pre-conditions
mapMonotonic :: (k -> g) -> Index k -> Index g Source #
\(O(n)\) Map a monotonic function over keys in the index. Monotonic means that if a < b
, then f a < f b
.
Using mapMonononic
can be much faster than using map
for a large Index
.
>>>
mapMonotonic (+1) $ fromList [0::Int,1,2,3,4,5]
Index [1,2,3,4,5,6]
The precondiction that the function be monotonic is not checked. Using mapMonotonic
with a non-monotonic function will not result in an exception; subtle inconsistencies will make you
question your own sanity.
Unsafe indexing
elemAt :: HasCallStack => Int -> Index k -> k Source #
\(O(\log n)\) Returns the element at some integer index.
This function raises an exception if the integer index is out-of-bounds.
Consider using lookupIndex
instead.
findIndex :: HasCallStack => Ord k => k -> Index k -> Int Source #
\(O(\log n)\). Returns the integer index of a key. This function raises an exception
if the key is not in the Index
; see lookupIndex
for a safe version.
>>>
findIndex 'b' $ fromList ['a', 'b', 'c']
1