{-# LANGUAGE DerivingStrategies    #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RecordWildCards       #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE UndecidableInstances  #-}

module Data.Series.Generic.Definition ( 
    Series(..),

    convert,

    -- * Basic interface
    singleton,
    headM, lastM, map, mapWithKey, mapIndex, concatMap, fold, foldM, 
    foldWithKey, foldMWithKey, foldMap, bifoldMap, foldMapWithKey, 
    length, null, take, takeWhile, drop, dropWhile,
    mapWithKeyM, mapWithKeyM_, forWithKeyM, forWithKeyM_,
    traverseWithKey,

    fromIndex,
    -- * Conversion to/from Series
    IsSeries(..),
    -- ** Conversion to/from Maps
    fromStrictMap,
    toStrictMap,
    fromLazyMap,
    toLazyMap,
    -- ** Conversion to/from list
    fromList,
    toList,
    -- *** Unsafe construction
    fromDistinctAscList,
    -- ** Conversion to/from vectors
    fromVector,
    toVector,
    -- *** Unsafe construction
    fromDistinctAscVector,
    -- ** Handling duplicates
    Occurrence, fromListDuplicates, fromVectorDuplicates,

    -- * Displaying 'Series'
    display, displayWith,
    noLongerThan,
    DisplayOptions(..), defaultDisplayOptions
) where

import           Control.DeepSeq        ( NFData(rnf) )
import           Control.Foldl          ( Fold(..), FoldM(..) )
import           Control.Monad.ST       ( runST )
import           Data.Bifoldable        ( Bifoldable )
import qualified Data.Bifoldable        as Bifoldable
import qualified Data.Foldable          as Foldable
import           Data.Foldable.WithIndex ( FoldableWithIndex(..))
import           Data.Function          ( on )
import           Data.Functor.WithIndex ( FunctorWithIndex(imap) )

import           Data.IntMap.Strict     ( IntMap )
import qualified Data.IntMap.Strict     as IntMap
import qualified Data.List              as List
import qualified Data.Map.Lazy          as ML
import           Data.Map.Strict        ( Map )
import qualified Data.Map.Strict        as MS
import           Data.Sequence          ( Seq )
import qualified Data.Sequence          as Seq
import           Data.Semigroup         ( Semigroup(..) )
import           Data.Series.Index      ( Index )
import qualified Data.Series.Index      as Index
import qualified Data.Series.Index.Internal as Index.Internal
import           Data.Set               ( Set )
import qualified Data.Set               as Set
import           Data.Traversable.WithIndex ( TraversableWithIndex(..) )
import qualified Data.Vector            as Boxed
import           Data.Vector.Algorithms.Intro ( sortUniqBy, sortBy )
import           Data.Vector.Generic    ( Vector )
import qualified Data.Vector.Generic    as Vector
import qualified Data.Vector.Generic.Mutable as GM
import qualified Data.Vector.Unboxed         as U
import qualified Data.Vector.Unboxed.Mutable as UM
 
import           Prelude                hiding ( take, takeWhile, drop, dropWhile, map, concatMap, foldMap, sum, length, null )
import qualified Prelude                as P



-- | A @Series v k a@ is a labeled array of type @v@ filled with values of type @a@,
-- indexed by keys of type @k@.
--
-- Like 'Data.Map.Strict.Map', they support efficient:
--
--      * random access by key ( \(O(\log n)\) );
--      * slice by key ( \(O(\log n)\) ).
--
-- Like 'Data.Vector.Vector', they support efficient:
--
--      * random access by index ( \(O(1)\) );
--      * slice by index ( \(O(1)\) );
--      * numerical operations.
--
data Series v k a 
    -- The reason the index is a set of keys is that we *want* keys to be ordered.
    -- This allows for efficient slicing of the underlying values, because
    -- if @k1 < k2@, then the values are also at indices @ix1 < ix2@.
    = MkSeries { forall {k} (v :: k -> *) k (a :: k). Series v k a -> Index k
index  :: Index k -- ^ The 'Index' of a series, which contains its (unique) keys in ascending order.
               , forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values :: v a     -- ^ The values of a series, in the order of its (unique) keys.
               }


-- | \(O(n)\) Convert between two types of 'Series'.
convert :: (Vector v1 a, Vector v2 a) => Series v1 k a -> Series v2 k a
{-# INLINE convert #-}
convert :: forall (v1 :: * -> *) a (v2 :: * -> *) k.
(Vector v1 a, Vector v2 a) =>
Series v1 k a -> Series v2 k a
convert (MkSeries Index k
ix v1 a
vs) = Index k -> v2 a -> Series v2 k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
ix (v2 a -> Series v2 k a) -> v2 a -> Series v2 k a
forall a b. (a -> b) -> a -> b
$ v1 a -> v2 a
forall (v :: * -> *) a (w :: * -> *).
(Vector v a, Vector w a) =>
v a -> w a
Vector.convert v1 a
vs 


-- | \(O(1)\) Create a 'Series' with a single element.
singleton :: Vector v a => k -> a -> Series v k a
{-# INLINE singleton #-}
singleton :: forall (v :: * -> *) a k. Vector v a => k -> a -> Series v k a
singleton k
k a
v = Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries (k -> Index k
forall k. k -> Index k
Index.singleton k
k) (v a -> Series v k a) -> v a -> Series v k a
forall a b. (a -> b) -> a -> b
$ a -> v a
forall (v :: * -> *) a. Vector v a => a -> v a
Vector.singleton a
v


-- | \(O(n)\) Generate a 'Series' by mapping every element of its index.
fromIndex :: (Vector v a) 
          => (k -> a) -> Index k -> Series v k a
{-# INLINE fromIndex #-}
fromIndex :: forall (v :: * -> *) a k.
Vector v a =>
(k -> a) -> Index k -> Series v k a
fromIndex k -> a
f Index k
ix = Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
ix (v a -> Series v k a) -> v a -> Series v k a
forall a b. (a -> b) -> a -> b
$ Vector a -> v a
forall (v :: * -> *) a (w :: * -> *).
(Vector v a, Vector w a) =>
v a -> w a
Vector.convert 
                             (Vector a -> v a) -> Vector a -> v a
forall a b. (a -> b) -> a -> b
$ (k -> a) -> Vector k -> Vector a
forall a b. (a -> b) -> Vector a -> Vector b
Boxed.map k -> a
f -- Using boxed vector to prevent a (Vector v k) constraint
                             (Vector k -> Vector a) -> Vector k -> Vector a
forall a b. (a -> b) -> a -> b
$ Index k -> Vector k
forall (v :: * -> *) k. Vector v k => Index k -> v k
Index.toAscVector Index k
ix


-- | The 'IsSeries' typeclass allow for ad-hoc definition
-- of conversion functions, converting to / from 'Series'.
class IsSeries t v k a where
    -- | Construct a 'Series' from some container of key-values pairs. There is no
    -- condition on the order of pairs. Duplicate keys are silently dropped. If you
    -- need to handle duplicate keys, see 'fromListDuplicates' or 'fromVectorDuplicates'.
    toSeries    :: t -> Series v k a

    -- | Construct a container from key-value pairs of a 'Series'. 
    -- The elements are returned in ascending order of keys. 
    fromSeries  :: Series v k a -> t


instance (Ord k, Vector v a) => IsSeries [(k, a)] v k a where
    -- | Construct a series from a list of key-value pairs. There is no
    -- condition on the order of pairs.
    --
    -- >>> let xs = toSeries [('b', 0::Int), ('a', 5), ('d', 1) ]
    -- >>> xs
    -- index | values
    -- ----- | ------
    --   'a' |      5
    --   'b' |      0
    --   'd' |      1
    --
    -- If you need to handle duplicate keys, take a look at `fromListDuplicates`.
    toSeries :: [(k, a)] -> Series v k a
    toSeries :: [(k, a)] -> Series v k a
toSeries = Map k a -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries (Map k a -> Series v k a)
-> ([(k, a)] -> Map k a) -> [(k, a)] -> Series v k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(k, a)] -> Map k a
forall k a. Ord k => [(k, a)] -> Map k a
MS.fromList
    {-# INLINE toSeries #-}

    -- | Construct a list from key-value pairs. The elements are in order sorted by key:
    --
    -- >>> let xs = Series.toSeries [ ('b', 0::Int), ('a', 5), ('d', 1) ]
    -- >>> xs
    -- index | values
    -- ----- | ------
    --   'a' |      5
    --   'b' |      0
    --   'd' |      1
    -- >>> fromSeries xs
    -- [('a',5),('b',0),('d',1)]
    fromSeries :: Series v k a -> [(k, a)]
    fromSeries :: Series v k a -> [(k, a)]
fromSeries (MkSeries Index k
ks v a
vs)= [k] -> [a] -> [(k, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Index k -> [k]
forall k. Index k -> [k]
Index.toAscList Index k
ks) (v a -> [a]
forall (v :: * -> *) a. Vector v a => v a -> [a]
Vector.toList v a
vs)
    {-# INLINE fromSeries #-}


-- | Construct a 'Series' from a list of key-value pairs. There is no
-- condition on the order of pairs. Duplicate keys are silently dropped. If you
-- need to handle duplicate keys, see 'fromListDuplicates'.
fromList :: (Vector v a, Ord k) => [(k, a)] -> Series v k a
{-# INLINE fromList #-}
fromList :: forall (v :: * -> *) a k.
(Vector v a, Ord k) =>
[(k, a)] -> Series v k a
fromList = [(k, a)] -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries


-- | \(O(n)\) Build a 'Series' from a list of pairs, where the first elements of the pairs (the keys)
-- are distinct elements in ascending order. The precondition that the keys be unique and sorted is not checked.
fromDistinctAscList :: (Vector v a) => [(k, a)] -> Series v k a
fromDistinctAscList :: forall (v :: * -> *) a k. Vector v a => [(k, a)] -> Series v k a
fromDistinctAscList [(k, a)]
xs 
    = let (![k]
ks, ![a]
vs) = [(k, a)] -> ([k], [a])
forall a b. [(a, b)] -> ([a], [b])
unzip [(k, a)]
xs 
       in Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries ([k] -> Index k
forall k. [k] -> Index k
Index.Internal.fromDistinctAscList [k]
ks) (Int -> [a] -> v a
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
Vector.fromListN ([a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
List.length [a]
vs) [a]
vs)


-- | Integer-like, non-negative number that specifies how many occurrences
-- of a key is present in a 'Series'.
--
-- The easiest way to convert from an 'Occurrence' to another integer-like type
-- is the 'fromIntegral' function.
newtype Occurrence = MkOcc Int
    deriving (Occurrence -> Occurrence -> Bool
(Occurrence -> Occurrence -> Bool)
-> (Occurrence -> Occurrence -> Bool) -> Eq Occurrence
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Occurrence -> Occurrence -> Bool
== :: Occurrence -> Occurrence -> Bool
$c/= :: Occurrence -> Occurrence -> Bool
/= :: Occurrence -> Occurrence -> Bool
Eq, Int -> Occurrence
Occurrence -> Int
Occurrence -> [Occurrence]
Occurrence -> Occurrence
Occurrence -> Occurrence -> [Occurrence]
Occurrence -> Occurrence -> Occurrence -> [Occurrence]
(Occurrence -> Occurrence)
-> (Occurrence -> Occurrence)
-> (Int -> Occurrence)
-> (Occurrence -> Int)
-> (Occurrence -> [Occurrence])
-> (Occurrence -> Occurrence -> [Occurrence])
-> (Occurrence -> Occurrence -> [Occurrence])
-> (Occurrence -> Occurrence -> Occurrence -> [Occurrence])
-> Enum Occurrence
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Occurrence -> Occurrence
succ :: Occurrence -> Occurrence
$cpred :: Occurrence -> Occurrence
pred :: Occurrence -> Occurrence
$ctoEnum :: Int -> Occurrence
toEnum :: Int -> Occurrence
$cfromEnum :: Occurrence -> Int
fromEnum :: Occurrence -> Int
$cenumFrom :: Occurrence -> [Occurrence]
enumFrom :: Occurrence -> [Occurrence]
$cenumFromThen :: Occurrence -> Occurrence -> [Occurrence]
enumFromThen :: Occurrence -> Occurrence -> [Occurrence]
$cenumFromTo :: Occurrence -> Occurrence -> [Occurrence]
enumFromTo :: Occurrence -> Occurrence -> [Occurrence]
$cenumFromThenTo :: Occurrence -> Occurrence -> Occurrence -> [Occurrence]
enumFromThenTo :: Occurrence -> Occurrence -> Occurrence -> [Occurrence]
Enum, Integer -> Occurrence
Occurrence -> Occurrence
Occurrence -> Occurrence -> Occurrence
(Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence)
-> (Occurrence -> Occurrence)
-> (Occurrence -> Occurrence)
-> (Integer -> Occurrence)
-> Num Occurrence
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Occurrence -> Occurrence -> Occurrence
+ :: Occurrence -> Occurrence -> Occurrence
$c- :: Occurrence -> Occurrence -> Occurrence
- :: Occurrence -> Occurrence -> Occurrence
$c* :: Occurrence -> Occurrence -> Occurrence
* :: Occurrence -> Occurrence -> Occurrence
$cnegate :: Occurrence -> Occurrence
negate :: Occurrence -> Occurrence
$cabs :: Occurrence -> Occurrence
abs :: Occurrence -> Occurrence
$csignum :: Occurrence -> Occurrence
signum :: Occurrence -> Occurrence
$cfromInteger :: Integer -> Occurrence
fromInteger :: Integer -> Occurrence
Num, Eq Occurrence
Eq Occurrence =>
(Occurrence -> Occurrence -> Ordering)
-> (Occurrence -> Occurrence -> Bool)
-> (Occurrence -> Occurrence -> Bool)
-> (Occurrence -> Occurrence -> Bool)
-> (Occurrence -> Occurrence -> Bool)
-> (Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> Occurrence)
-> Ord Occurrence
Occurrence -> Occurrence -> Bool
Occurrence -> Occurrence -> Ordering
Occurrence -> Occurrence -> Occurrence
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Occurrence -> Occurrence -> Ordering
compare :: Occurrence -> Occurrence -> Ordering
$c< :: Occurrence -> Occurrence -> Bool
< :: Occurrence -> Occurrence -> Bool
$c<= :: Occurrence -> Occurrence -> Bool
<= :: Occurrence -> Occurrence -> Bool
$c> :: Occurrence -> Occurrence -> Bool
> :: Occurrence -> Occurrence -> Bool
$c>= :: Occurrence -> Occurrence -> Bool
>= :: Occurrence -> Occurrence -> Bool
$cmax :: Occurrence -> Occurrence -> Occurrence
max :: Occurrence -> Occurrence -> Occurrence
$cmin :: Occurrence -> Occurrence -> Occurrence
min :: Occurrence -> Occurrence -> Occurrence
Ord, Enum Occurrence
Real Occurrence
(Real Occurrence, Enum Occurrence) =>
(Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> Occurrence)
-> (Occurrence -> Occurrence -> (Occurrence, Occurrence))
-> (Occurrence -> Occurrence -> (Occurrence, Occurrence))
-> (Occurrence -> Integer)
-> Integral Occurrence
Occurrence -> Integer
Occurrence -> Occurrence -> (Occurrence, Occurrence)
Occurrence -> Occurrence -> Occurrence
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
$cquot :: Occurrence -> Occurrence -> Occurrence
quot :: Occurrence -> Occurrence -> Occurrence
$crem :: Occurrence -> Occurrence -> Occurrence
rem :: Occurrence -> Occurrence -> Occurrence
$cdiv :: Occurrence -> Occurrence -> Occurrence
div :: Occurrence -> Occurrence -> Occurrence
$cmod :: Occurrence -> Occurrence -> Occurrence
mod :: Occurrence -> Occurrence -> Occurrence
$cquotRem :: Occurrence -> Occurrence -> (Occurrence, Occurrence)
quotRem :: Occurrence -> Occurrence -> (Occurrence, Occurrence)
$cdivMod :: Occurrence -> Occurrence -> (Occurrence, Occurrence)
divMod :: Occurrence -> Occurrence -> (Occurrence, Occurrence)
$ctoInteger :: Occurrence -> Integer
toInteger :: Occurrence -> Integer
Integral, Num Occurrence
Ord Occurrence
(Num Occurrence, Ord Occurrence) =>
(Occurrence -> Rational) -> Real Occurrence
Occurrence -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: Occurrence -> Rational
toRational :: Occurrence -> Rational
Real)
    deriving newtype (Int -> Occurrence -> ShowS
[Occurrence] -> ShowS
Occurrence -> String
(Int -> Occurrence -> ShowS)
-> (Occurrence -> String)
-> ([Occurrence] -> ShowS)
-> Show Occurrence
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Occurrence -> ShowS
showsPrec :: Int -> Occurrence -> ShowS
$cshow :: Occurrence -> String
show :: Occurrence -> String
$cshowList :: [Occurrence] -> ShowS
showList :: [Occurrence] -> ShowS
Show, Vector Vector Occurrence
MVector MVector Occurrence
(Vector Vector Occurrence, MVector MVector Occurrence) =>
Unbox Occurrence
forall a. (Vector Vector a, MVector MVector a) => Unbox a
U.Unbox) 

-- Occurrence needs to be an 'U.Unbox' type
-- so that 'fromVectorDuplicates' works with unboxed vectors
-- and series.
newtype instance UM.MVector s Occurrence = MV_Occ (UM.MVector s Int)
newtype instance U.Vector Occurrence = V_Occ (U.Vector Int)
deriving instance GM.MVector UM.MVector Occurrence
deriving instance Vector U.Vector Occurrence 


-- | Construct a series from a list of key-value pairs.
-- Contrary to 'fromList', values at duplicate keys are preserved. To keep each
-- key unique, an 'Occurrence' number counts up.
fromListDuplicates :: (Vector v a, Ord k) => [(k, a)] -> Series v (k, Occurrence) a
{-# INLINE fromListDuplicates #-}
fromListDuplicates :: forall (v :: * -> *) a k.
(Vector v a, Ord k) =>
[(k, a)] -> Series v (k, Occurrence) a
fromListDuplicates = Series Vector (k, Occurrence) a -> Series v (k, Occurrence) a
forall (v1 :: * -> *) a (v2 :: * -> *) k.
(Vector v1 a, Vector v2 a) =>
Series v1 k a -> Series v2 k a
convert (Series Vector (k, Occurrence) a -> Series v (k, Occurrence) a)
-> ([(k, a)] -> Series Vector (k, Occurrence) a)
-> [(k, a)]
-> Series v (k, Occurrence) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (k, a) -> Series Vector (k, Occurrence) a
forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a),
 Vector v (k, Occurrence)) =>
v (k, a) -> Series v (k, Occurrence) a
fromVectorDuplicates (Vector (k, a) -> Series Vector (k, Occurrence) a)
-> ([(k, a)] -> Vector (k, a))
-> [(k, a)]
-> Series Vector (k, Occurrence) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(k, a)] -> Vector (k, a)
forall a. [a] -> Vector a
Boxed.fromList


-- | Construct a list from key-value pairs. The elements are in order sorted by key. 
toList :: Vector v a => Series v k a -> [(k, a)]
{-# INLINE toList #-}
toList :: forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList (MkSeries Index k
ks v a
vs) = [k] -> [a] -> [(k, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Index k -> [k]
forall k. Index k -> [k]
Index.toAscList Index k
ks) (v a -> [a]
forall (v :: * -> *) a. Vector v a => v a -> [a]
Vector.toList v a
vs)


instance (Ord k) => IsSeries (Boxed.Vector (k, a)) Boxed.Vector k a where
    toSeries :: Vector (k, a) -> Series Vector k a
toSeries = Vector (k, a) -> Series Vector k a
forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a)) =>
v (k, a) -> Series v k a
fromVector
    {-# INLINE toSeries #-}

    fromSeries :: Series Vector k a -> Vector (k, a)
fromSeries = Series Vector k a -> Vector (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector
    {-# INLINE fromSeries #-}


instance (Ord k, U.Unbox a, U.Unbox k) => IsSeries (U.Vector (k, a)) U.Vector k a where
    toSeries :: U.Vector (k, a) -> Series U.Vector k a
    toSeries :: Vector (k, a) -> Series Vector k a
toSeries = Vector (k, a) -> Series Vector k a
forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a)) =>
v (k, a) -> Series v k a
fromVector
    {-# INLINE toSeries #-}

    fromSeries :: Series U.Vector k a -> U.Vector (k, a)
    fromSeries :: Series Vector k a -> Vector (k, a)
fromSeries = Series Vector k a -> Vector (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector
    {-# INLINE fromSeries #-}


-- | Construct a 'Series' from a 'Vector' of key-value pairs. There is no
-- condition on the order of pairs. Duplicate keys are silently dropped. If you
-- need to handle duplicate keys, see 'fromVectorDuplicates'.
--
-- Note that due to differences in sorting,
-- 'Series.fromList' and @'Series.fromVector' . 'Vector.fromList'@
-- may not be equivalent if the input list contains duplicate keys.
fromVector :: (Ord k, Vector v k, Vector v a, Vector v (k, a))
           => v (k, a) -> Series v k a
{-# INLINE fromVector #-}
fromVector :: forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a)) =>
v (k, a) -> Series v k a
fromVector v (k, a)
vec = let (v k
indexVector, v a
valuesVector) = v (k, a) -> (v k, v a)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v (a, b) -> (v a, v b)
Vector.unzip (v (k, a) -> (v k, v a)) -> v (k, a) -> (v k, v a)
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (v (k, a))) -> v (k, a)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (v (k, a))) -> v (k, a))
-> (forall s. ST s (v (k, a))) -> v (k, a)
forall a b. (a -> b) -> a -> b
$ do
                        Mutable v s (k, a)
mv <- v (k, a) -> ST s (Mutable v (PrimState (ST s)) (k, a))
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
Vector.thaw v (k, a)
vec
                        -- Note that we're using this particular flavor of `sortUniqBy`
                        -- because it both sorts AND removes duplicate keys
                        Mutable v s (k, a)
destMV <- Comparison (k, a)
-> Mutable v (PrimState (ST s)) (k, a)
-> ST s (Mutable v (PrimState (ST s)) (k, a))
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e) =>
Comparison e -> v (PrimState m) e -> m (v (PrimState m) e)
sortUniqBy (k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (k -> k -> Ordering) -> ((k, a) -> k) -> Comparison (k, a)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (k, a) -> k
forall a b. (a, b) -> a
fst) Mutable v s (k, a)
Mutable v (PrimState (ST s)) (k, a)
mv
                        v (k, a)
v <- Mutable v (PrimState (ST s)) (k, a) -> ST s (v (k, a))
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
Vector.freeze Mutable v s (k, a)
Mutable v (PrimState (ST s)) (k, a)
destMV
                        v (k, a) -> ST s (v (k, a))
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (v (k, a) -> v (k, a)
forall (v :: * -> *) a. Vector v a => v a -> v a
Vector.force v (k, a)
v)
              in Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries (v k -> Index k
forall (v :: * -> *) k. Vector v k => v k -> Index k
Index.Internal.fromDistinctAscVector v k
indexVector) v a
valuesVector


-- | \(O(n)\) Build a 'Series' from a vector of pairs, where the first elements of the pairs (the keys)
-- are distinct elements in ascending order. The precondition that the keys be unique and sorted is not checked.
fromDistinctAscVector :: (Vector v k, Vector v a, Vector v (k, a))
                      => v (k, a) -> Series v k a
fromDistinctAscVector :: forall (v :: * -> *) k a.
(Vector v k, Vector v a, Vector v (k, a)) =>
v (k, a) -> Series v k a
fromDistinctAscVector v (k, a)
xs 
    = let (v k
ks, v a
vs) = v (k, a) -> (v k, v a)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v (a, b) -> (v a, v b)
Vector.unzip v (k, a)
xs 
       in Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries (v k -> Index k
forall (v :: * -> *) k. Vector v k => v k -> Index k
Index.Internal.fromDistinctAscVector v k
ks) v a
vs


-- | Construct a 'Series' from a 'Vector' of key-value pairs, where there may be duplicate keys. 
-- There is no condition on the order of pairs.
fromVectorDuplicates :: (Ord k, Vector v k, Vector v a, Vector v (k, a), Vector v (k, Occurrence))
                     => v (k, a) -> Series v (k, Occurrence) a
{-# INLINE fromVectorDuplicates #-}
fromVectorDuplicates :: forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a),
 Vector v (k, Occurrence)) =>
v (k, a) -> Series v (k, Occurrence) a
fromVectorDuplicates v (k, a)
vec 
    = let (v k
indexVector, v a
valuesVector) 
            = v (k, a) -> (v k, v a)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v (a, b) -> (v a, v b)
Vector.unzip (v (k, a) -> (v k, v a)) -> v (k, a) -> (v k, v a)
forall a b. (a -> b) -> a -> b
$ (forall s. ST s (v (k, a))) -> v (k, a)
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (v (k, a))) -> v (k, a))
-> (forall s. ST s (v (k, a))) -> v (k, a)
forall a b. (a -> b) -> a -> b
$ do
                Mutable v s (k, a)
mv <- v (k, a) -> ST s (Mutable v (PrimState (ST s)) (k, a))
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
v a -> m (Mutable v (PrimState m) a)
Vector.thaw v (k, a)
vec
                Comparison (k, a) -> Mutable v (PrimState (ST s)) (k, a) -> ST s ()
forall (m :: * -> *) (v :: * -> * -> *) e.
(PrimMonad m, MVector v e) =>
Comparison e -> v (PrimState m) e -> m ()
sortBy (k -> k -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (k -> k -> Ordering) -> ((k, a) -> k) -> Comparison (k, a)
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (k, a) -> k
forall a b. (a, b) -> a
fst) Mutable v s (k, a)
Mutable v (PrimState (ST s)) (k, a)
mv
                v (k, a)
v <- Mutable v (PrimState (ST s)) (k, a) -> ST s (v (k, a))
forall (m :: * -> *) (v :: * -> *) a.
(PrimMonad m, Vector v a) =>
Mutable v (PrimState m) a -> m (v a)
Vector.freeze Mutable v s (k, a)
Mutable v (PrimState (ST s)) (k, a)
mv
                v (k, a) -> ST s (v (k, a))
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (v (k, a) -> v (k, a)
forall (v :: * -> *) a. Vector v a => v a -> v a
Vector.force v (k, a)
v)
        in Index (k, Occurrence) -> v a -> Series v (k, Occurrence) a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries (v (k, Occurrence) -> Index (k, Occurrence)
forall (v :: * -> *) k. Vector v k => v k -> Index k
Index.Internal.fromDistinctAscVector (v k -> v (k, Occurrence)
forall {v :: * -> *} {b} {t}.
(Vector v b, Vector v (b, t), Num t, Eq b) =>
v b -> v (b, t)
occurences v k
indexVector)) v a
valuesVector
    where
        occurences :: v b -> v (b, t)
occurences v b
vs 
            | v b -> Bool
forall (v :: * -> *) a. Vector v a => v a -> Bool
Vector.null v b
vs        = v (b, t)
forall (v :: * -> *) a. Vector v a => v a
Vector.empty
            | v b -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
Vector.length v b
vs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 = (b -> (b, t)) -> v b -> v (b, t)
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
Vector.map (,t
0) v b
vs
            | Bool
otherwise             = ((b, t) -> b -> (b, t)) -> (b, t) -> v b -> v (b, t)
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b -> a) -> a -> v b -> v a
Vector.scanl (b, t) -> b -> (b, t)
forall {a} {b}. (Eq a, Num b) => (a, b) -> a -> (a, b)
f (v b -> b
forall (v :: * -> *) a. Vector v a => v a -> a
Vector.head v b
vs, t
0) (v b -> v b
forall (v :: * -> *) a. Vector v a => v a -> v a
Vector.tail v b
vs)
            where
                f :: (a, b) -> a -> (a, b)
f (a
lastKey, b
lastOcc) a
newKey 
                    | a
lastKey a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
newKey = (a
newKey, b
lastOcc b -> b -> b
forall a. Num a => a -> a -> a
+ b
1)
                    | Bool
otherwise         = (a
newKey, b
0)


-- | Construct a 'Vector' of key-value pairs. The elements are in order sorted by key. 
toVector :: (Vector v a, Vector v k, Vector v (k, a)) 
         => Series v k a -> v (k, a)
{-# INLINE toVector #-}
toVector :: forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector (MkSeries Index k
ks v a
vs) = v k -> v a -> v (k, a)
forall (v :: * -> *) a b.
(Vector v a, Vector v b, Vector v (a, b)) =>
v a -> v b -> v (a, b)
Vector.zip (Index k -> v k
forall (v :: * -> *) k. Vector v k => Index k -> v k
Index.toAscVector Index k
ks) v a
vs


instance (Vector v a) => IsSeries (Map k a) v k a where
    toSeries :: Map k a -> Series v k a
    toSeries :: Map k a -> Series v k a
toSeries Map k a
mp = MkSeries 
                { index :: Index k
index  = Set k -> Index k
forall k. Set k -> Index k
Index.fromSet (Set k -> Index k) -> Set k -> Index k
forall a b. (a -> b) -> a -> b
$ Map k a -> Set k
forall k a. Map k a -> Set k
MS.keysSet Map k a
mp
                , values :: v a
values = Int -> [a] -> v a
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
Vector.fromListN (Map k a -> Int
forall k a. Map k a -> Int
MS.size Map k a
mp) ([a] -> v a) -> [a] -> v a
forall a b. (a -> b) -> a -> b
$ Map k a -> [a]
forall k a. Map k a -> [a]
MS.elems Map k a
mp
                }
    {-# INLINE toSeries #-}

    fromSeries :: Series v k a -> Map k a
    fromSeries :: Series v k a -> Map k a
fromSeries (MkSeries Index k
ks v a
vs)
        = [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
MS.fromDistinctAscList ([(k, a)] -> Map k a) -> [(k, a)] -> Map k a
forall a b. (a -> b) -> a -> b
$ [k] -> [a] -> [(k, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Index k -> [k]
forall k. Index k -> [k]
Index.toAscList Index k
ks) (v a -> [a]
forall (v :: * -> *) a. Vector v a => v a -> [a]
Vector.toList v a
vs)
    {-# INLINE fromSeries #-}


toLazyMap :: (Vector v a) => Series v k a -> Map k a
{-# INLINE toLazyMap #-}
toLazyMap :: forall (v :: * -> *) a k. Vector v a => Series v k a -> Map k a
toLazyMap = Series v k a -> Map k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
Series v k a -> t
fromSeries


-- | Construct a series from a lazy 'Data.Map.Lazy.Map'.
fromLazyMap :: (Vector v a) => ML.Map k a -> Series v k a
{-# INLINE fromLazyMap #-}
fromLazyMap :: forall (v :: * -> *) a k. Vector v a => Map k a -> Series v k a
fromLazyMap = Map k a -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries


-- | Convert a series into a strict 'Data.Map.Strict.Map'.
toStrictMap :: (Vector v a) => Series v k a -> Map k a
{-# INLINE toStrictMap #-}
toStrictMap :: forall (v :: * -> *) a k. Vector v a => Series v k a -> Map k a
toStrictMap (MkSeries Index k
ks v a
vs) = [(k, a)] -> Map k a
forall k a. [(k, a)] -> Map k a
MS.fromDistinctAscList ([(k, a)] -> Map k a) -> [(k, a)] -> Map k a
forall a b. (a -> b) -> a -> b
$ [k] -> [a] -> [(k, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Index k -> [k]
forall k. Index k -> [k]
Index.toAscList Index k
ks) (v a -> [a]
forall (v :: * -> *) a. Vector v a => v a -> [a]
Vector.toList v a
vs)


-- | Construct a series from a strict 'Data.Map.Strict.Map'.
fromStrictMap :: (Vector v a) => MS.Map k a -> Series v k a
{-# INLINE fromStrictMap #-}
fromStrictMap :: forall (v :: * -> *) a k. Vector v a => Map k a -> Series v k a
fromStrictMap Map k a
mp = MkSeries { index :: Index k
index  = Set k -> Index k
forall t k. IsIndex t k => t -> Index k
Index.toIndex (Set k -> Index k) -> Set k -> Index k
forall a b. (a -> b) -> a -> b
$ Map k a -> Set k
forall k a. Map k a -> Set k
MS.keysSet Map k a
mp
                            , values :: v a
values = Int -> [a] -> v a
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
Vector.fromListN (Map k a -> Int
forall k a. Map k a -> Int
MS.size Map k a
mp) ([a] -> v a) -> [a] -> v a
forall a b. (a -> b) -> a -> b
$ Map k a -> [a]
forall k a. Map k a -> [a]
MS.elems Map k a
mp
                            }


instance (Vector v a) => IsSeries (IntMap a) v Int a where
    toSeries :: IntMap a -> Series v Int a
    toSeries :: IntMap a -> Series v Int a
toSeries IntMap a
im = MkSeries 
                { index :: Index Int
index  = IntSet -> Index Int
forall t k. IsIndex t k => t -> Index k
Index.toIndex (IntSet -> Index Int) -> IntSet -> Index Int
forall a b. (a -> b) -> a -> b
$ IntMap a -> IntSet
forall a. IntMap a -> IntSet
IntMap.keysSet IntMap a
im
                , values :: v a
values = Int -> [a] -> v a
forall (v :: * -> *) a. Vector v a => Int -> [a] -> v a
Vector.fromListN (IntMap a -> Int
forall a. IntMap a -> Int
IntMap.size IntMap a
im)  ([a] -> v a) -> [a] -> v a
forall a b. (a -> b) -> a -> b
$ IntMap a -> [a]
forall a. IntMap a -> [a]
IntMap.elems IntMap a
im 
                }
    {-# INLINE toSeries #-}

    fromSeries :: Series v Int a -> IntMap a
    fromSeries :: Series v Int a -> IntMap a
fromSeries (MkSeries Index Int
ks v a
vs) 
        = [(Int, a)] -> IntMap a
forall a. [(Int, a)] -> IntMap a
IntMap.fromDistinctAscList ([(Int, a)] -> IntMap a) -> [(Int, a)] -> IntMap a
forall a b. (a -> b) -> a -> b
$ [Int] -> [a] -> [(Int, a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Index Int -> [Int]
forall k. Index k -> [k]
Index.toAscList Index Int
ks) (v a -> [a]
forall (v :: * -> *) a. Vector v a => v a -> [a]
Vector.toList v a
vs)
    {-# INLINE fromSeries #-}


instance (Ord k, Vector v a) => IsSeries (Seq (k, a)) v k a where
    toSeries :: Seq (k, a) -> Series v k a
    toSeries :: Seq (k, a) -> Series v k a
toSeries = [(k, a)] -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries ([(k, a)] -> Series v k a)
-> (Seq (k, a) -> [(k, a)]) -> Seq (k, a) -> Series v k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq (k, a) -> [(k, a)]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList
    {-# INLINE toSeries #-}

    fromSeries :: Series v k a -> Seq (k, a)
    fromSeries :: Series v k a -> Seq (k, a)
fromSeries = [(k, a)] -> Seq (k, a)
forall a. [a] -> Seq a
Seq.fromList ([(k, a)] -> Seq (k, a))
-> (Series v k a -> [(k, a)]) -> Series v k a -> Seq (k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> [(k, a)]
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
Series v k a -> t
fromSeries
    {-# INLINE fromSeries #-}


instance (Vector v a) => IsSeries (Set (k, a)) v k a where
    toSeries :: Set (k, a) -> Series v k a
    toSeries :: Set (k, a) -> Series v k a
toSeries = [(k, a)] -> Series v k a
forall (v :: * -> *) a k. Vector v a => [(k, a)] -> Series v k a
fromDistinctAscList ([(k, a)] -> Series v k a)
-> (Set (k, a) -> [(k, a)]) -> Set (k, a) -> Series v k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Set (k, a) -> [(k, a)]
forall a. Set a -> [a]
Set.toAscList
    {-# INLINE toSeries #-}

    fromSeries :: Series v k a -> Set (k, a)
    fromSeries :: Series v k a -> Set (k, a)
fromSeries = [(k, a)] -> Set (k, a)
forall a. [a] -> Set a
Set.fromDistinctAscList ([(k, a)] -> Set (k, a))
-> (Series v k a -> [(k, a)]) -> Series v k a -> Set (k, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> [(k, a)]
forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList
    {-# INLINE fromSeries #-}


-- | Get the first value of a 'Series'. If the 'Series' is empty,
-- this function returns 'Nothing'.
headM :: Vector v a => Series v k a -> Maybe a
{-# INLINE headM #-}
headM :: forall (v :: * -> *) a k. Vector v a => Series v k a -> Maybe a
headM (MkSeries Index k
_ v a
vs) = v a -> Maybe a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> m a
Vector.headM v a
vs


-- | Get the last value of a 'Series'. If the 'Series' is empty,
-- this function returns 'Nothing'.
lastM :: Vector v a => Series v k a -> Maybe a
{-# INLINE lastM #-}
lastM :: forall (v :: * -> *) a k. Vector v a => Series v k a -> Maybe a
lastM (MkSeries Index k
_ v a
vs) = v a -> Maybe a
forall (v :: * -> *) a (m :: * -> *).
(Vector v a, Monad m) =>
v a -> m a
Vector.lastM v a
vs


-- | \(O(\log n)\) @'take' n xs@ returns at most @n@ elements of the 'Series' @xs@.
take :: Vector v a => Int -> Series v k a -> Series v k a
{-# INLINE take #-}
take :: forall (v :: * -> *) a k.
Vector v a =>
Int -> Series v k a -> Series v k a
take Int
n (MkSeries Index k
ks v a
vs) 
    -- Index.take is O(log n) while Vector.take is O(1)
    = Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries (Int -> Index k -> Index k
forall k. Int -> Index k -> Index k
Index.take Int
n Index k
ks) (Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
Vector.take Int
n v a
vs)


-- | \(O(\log n)\) @'drop' n xs@ drops at most @n@ elements from the 'Series' @xs@.
drop :: Vector v a => Int -> Series v k a -> Series v k a
{-# INLINE drop #-}
drop :: forall (v :: * -> *) a k.
Vector v a =>
Int -> Series v k a -> Series v k a
drop Int
n (MkSeries Index k
ks v a
vs) 
    -- Index.drop is O(log n) while Vector.drop is O(1)
    = Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries (Int -> Index k -> Index k
forall k. Int -> Index k -> Index k
Index.drop Int
n Index k
ks) (Int -> v a -> v a
forall (v :: * -> *) a. Vector v a => Int -> v a -> v a
Vector.drop Int
n v a
vs)


-- | \(O(n)\) Returns the longest prefix (possibly empty) of the input 'Series' that satisfy a predicate.
takeWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a
{-# INLINE takeWhile #-}
takeWhile :: forall (v :: * -> *) a k.
Vector v a =>
(a -> Bool) -> Series v k a -> Series v k a
takeWhile a -> Bool
f (MkSeries Index k
ix v a
vs) = let taken :: v a
taken = (a -> Bool) -> v a -> v a
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
Vector.takeWhile a -> Bool
f v a
vs
                 in MkSeries { index :: Index k
index  = Int -> Index k -> Index k
forall k. Int -> Index k -> Index k
Index.take (v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
Vector.length v a
taken) Index k
ix
                             , values :: v a
values = v a
taken 
                             }


-- | \(O(n)\) Returns the complement of 'takeWhile'.
dropWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a
{-# INLINE dropWhile #-}
dropWhile :: forall (v :: * -> *) a k.
Vector v a =>
(a -> Bool) -> Series v k a -> Series v k a
dropWhile a -> Bool
f (MkSeries Index k
ix v a
vs) = let dropped :: v a
dropped = (a -> Bool) -> v a -> v a
forall (v :: * -> *) a. Vector v a => (a -> Bool) -> v a -> v a
Vector.dropWhile a -> Bool
f v a
vs
                 in MkSeries { index :: Index k
index  = Int -> Index k -> Index k
forall k. Int -> Index k -> Index k
Index.drop (Index k -> Int
forall k. Index k -> Int
Index.size Index k
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
- v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
Vector.length v a
dropped) Index k
ix
                             , values :: v a
values = v a
dropped
                             }


-- | \(O(n)\) Map every element of a 'Series'.
map :: (Vector v a, Vector v b) 
    => (a -> b) -> Series v k a -> Series v k b
{-# INLINE map #-}
map :: forall (v :: * -> *) a b k.
(Vector v a, Vector v b) =>
(a -> b) -> Series v k a -> Series v k b
map a -> b
f (MkSeries Index k
ix v a
xs) = Index k -> v b -> Series v k b
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
ix (v b -> Series v k b) -> v b -> Series v k b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> v a -> v b
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> b) -> v a -> v b
Vector.map a -> b
f v a
xs


-- | \(O(n)\) Map every element of a 'Series', possibly using the key as well.
mapWithKey :: (Vector v a, Vector v b) 
           => (k -> a -> b) -> Series v k a -> Series v k b
{-# INLINE mapWithKey #-}
mapWithKey :: forall (v :: * -> *) a b k.
(Vector v a, Vector v b) =>
(k -> a -> b) -> Series v k a -> Series v k b
mapWithKey k -> a -> b
f (MkSeries Index k
ix v a
xs) 
    -- We're using boxed vectors to map because we don't want any restrictions
    -- on the index type, i.e. we don't want the constraint Vector v k
    = let vs :: Vector b
vs = (k -> a -> b) -> Vector k -> Vector a -> Vector b
forall a b c. (a -> b -> c) -> Vector a -> Vector b -> Vector c
Boxed.zipWith k -> a -> b
f (Index k -> Vector k
forall (v :: * -> *) k. Vector v k => Index k -> v k
Index.toAscVector Index k
ix) (v a -> Vector a
forall (v :: * -> *) a (w :: * -> *).
(Vector v a, Vector w a) =>
v a -> w a
Vector.convert v a
xs)
       in Index k -> v b -> Series v k b
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
ix (Vector b -> v b
forall (v :: * -> *) a (w :: * -> *).
(Vector v a, Vector w a) =>
v a -> w a
Vector.convert Vector b
vs)


-- | \(O(n \log n)\).
-- Map each key in the index to another value. Note that the resulting series
-- may have less elements, because each key must be unique.
--
-- In case new keys are conflicting, the first element is kept.
mapIndex :: (Vector v a, Ord k, Ord g) => Series v k a -> (k -> g) -> Series v g a
{-# INLINE mapIndex #-}
mapIndex :: forall (v :: * -> *) a k g.
(Vector v a, Ord k, Ord g) =>
Series v k a -> (k -> g) -> Series v g a
mapIndex (MkSeries Index k
index v a
values) k -> g
f
    -- Note that the order in which items are kept appears to be backwards;
    -- See the examples for Data.Map.Strict.fromListWith
    = let mapping :: Map g k
mapping   = (k -> k -> k) -> [(g, k)] -> Map g k
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
MS.fromListWith (\k
_ k
x -> k
x) ([(g, k)] -> Map g k) -> [(g, k)] -> Map g k
forall a b. (a -> b) -> a -> b
$ [(k -> g
f k
k, k
k) | k
k <- Index k -> [k]
forall k. Index k -> [k]
Index.toAscList Index k
index]
          newvalues :: Map g a
newvalues = (k -> a) -> Map g k -> Map g a
forall a b. (a -> b) -> Map g a -> Map g b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\k
k -> v a
values v a -> Int -> a
forall (v :: * -> *) a.
(HasCallStack, Vector v a) =>
v a -> Int -> a
Vector.! k -> Index k -> Int
forall k. (HasCallStack, Ord k) => k -> Index k -> Int
Index.Internal.findIndex k
k Index k
index) Map g k
mapping
       in Map g a -> Series v g a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries Map g a
newvalues


-- | Map a function over all the elements of a 'Series' and concatenate the result into a single 'Series'.
concatMap :: (Vector v a, Vector v k, Vector v b, Vector v (k, a), Vector v (k, b), Ord k) 
          => (a -> Series v k b) 
          -> Series v k a 
          -> Series v k b
{-# INLINE concatMap #-}
concatMap :: forall (v :: * -> *) a k b.
(Vector v a, Vector v k, Vector v b, Vector v (k, a),
 Vector v (k, b), Ord k) =>
(a -> Series v k b) -> Series v k a -> Series v k b
concatMap a -> Series v k b
f = v (k, b) -> Series v k b
forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a)) =>
v (k, a) -> Series v k a
fromVector 
            (v (k, b) -> Series v k b)
-> (Series v k a -> v (k, b)) -> Series v k a -> Series v k b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> v (k, b)) -> v (k, a) -> v (k, b)
forall (v :: * -> *) a b.
(Vector v a, Vector v b) =>
(a -> v b) -> v a -> v b
Vector.concatMap (Series v k b -> v (k, b)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector (Series v k b -> v (k, b))
-> ((k, a) -> Series v k b) -> (k, a) -> v (k, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Series v k b
f (a -> Series v k b) -> ((k, a) -> a) -> (k, a) -> Series v k b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (k, a) -> a
forall a b. (a, b) -> b
snd) 
            (v (k, a) -> v (k, b))
-> (Series v k a -> v (k, a)) -> Series v k a -> v (k, b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector


instance (Vector v a, Ord k) => Semigroup (Series v k a) where
    {-# INLINE (<>) #-}
    (<>) :: Series v k a -> Series v k a -> Series v k a
    -- Despite all my effort, merging via conversion to Map remains fastest.
    Series v k a
xs <> :: Series v k a -> Series v k a -> Series v k a
<> Series v k a
ys = Map k a -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries (Map k a -> Series v k a) -> Map k a -> Series v k a
forall a b. (a -> b) -> a -> b
$ Series v k a -> Map k a
forall (v :: * -> *) a k. Vector v a => Series v k a -> Map k a
toStrictMap Series v k a
xs Map k a -> Map k a -> Map k a
forall a. Semigroup a => a -> a -> a
<> Series v k a -> Map k a
forall (v :: * -> *) a k. Vector v a => Series v k a -> Map k a
toStrictMap Series v k a
ys

    {-# INLINE sconcat #-}
    sconcat :: NonEmpty (Series v k a) -> Series v k a
sconcat = Map k a -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries (Map k a -> Series v k a)
-> (NonEmpty (Series v k a) -> Map k a)
-> NonEmpty (Series v k a)
-> Series v k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NonEmpty (Map k a) -> Map k a
forall a. Semigroup a => NonEmpty a -> a
sconcat (NonEmpty (Map k a) -> Map k a)
-> (NonEmpty (Series v k a) -> NonEmpty (Map k a))
-> NonEmpty (Series v k a)
-> Map k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Series v k a -> Map k a)
-> NonEmpty (Series v k a) -> NonEmpty (Map k a)
forall a b. (a -> b) -> NonEmpty a -> NonEmpty b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Series v k a -> Map k a
forall (v :: * -> *) a k. Vector v a => Series v k a -> Map k a
toStrictMap


instance (Vector v a, Ord k) => Monoid (Series v k a) where
    {-# INLINE mempty #-}
    mempty :: Series v k a
    mempty :: Series v k a
mempty = Index k -> v a -> Series v k a
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
forall a. Monoid a => a
mempty v a
forall (v :: * -> *) a. Vector v a => v a
Vector.empty

    {-# INLINE mappend #-}
    mappend :: Series v k a -> Series v k a -> Series v k a
    mappend :: Series v k a -> Series v k a -> Series v k a
mappend = Series v k a -> Series v k a -> Series v k a
forall a. Semigroup a => a -> a -> a
(<>)

    {-# INLINE mconcat #-}
    mconcat :: [Series v k a] -> Series v k a
    mconcat :: [Series v k a] -> Series v k a
mconcat = Map k a -> Series v k a
forall {k} t (v :: k -> *) k (a :: k).
IsSeries t v k a =>
t -> Series v k a
toSeries (Map k a -> Series v k a)
-> ([Series v k a] -> Map k a) -> [Series v k a] -> Series v k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Map k a] -> Map k a
forall a. Monoid a => [a] -> a
mconcat ([Map k a] -> Map k a)
-> ([Series v k a] -> [Map k a]) -> [Series v k a] -> Map k a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Series v k a -> Map k a) -> [Series v k a] -> [Map k a]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Series v k a -> Map k a
forall (v :: * -> *) a k. Vector v a => Series v k a -> Map k a
toStrictMap


instance (Vector v a, Eq k, Eq a) => Eq (Series v k a) where
    {-# INLINE (==) #-}
    (==) :: Series v k a -> Series v k a -> Bool
    (MkSeries Index k
ks1 v a
vs1) == :: Series v k a -> Series v k a -> Bool
== (MkSeries Index k
ks2 v a
vs2) = (Index k
ks1 Index k -> Index k -> Bool
forall a. Eq a => a -> a -> Bool
== Index k
ks2) Bool -> Bool -> Bool
&& (v a
vs1 v a -> v a -> Bool
forall (v :: * -> *) a. (Vector v a, Eq a) => v a -> v a -> Bool
`Vector.eq` v a
vs2)


instance (Vector v a, Ord (v a), Ord k, Ord a) => Ord (Series v k a) where
    {-# INLINE compare #-}
    compare :: Series v k a -> Series v k a -> Ordering
    compare :: Series v k a -> Series v k a -> Ordering
compare (MkSeries Index k
ks1 v a
vs1) (MkSeries Index k
ks2 v a
vs2) = (Index k, v a) -> (Index k, v a) -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (Index k
ks1, v a
vs1) (Index k
ks2, v a
vs2)


instance (Functor v) => Functor (Series v k) where
    {-# INLINE fmap #-}
    fmap :: (a -> b) -> Series v k a -> Series v k b
    fmap :: forall a b. (a -> b) -> Series v k a -> Series v k b
fmap a -> b
f (MkSeries Index k
ks v a
vs) = Index k -> v b -> Series v k b
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
ks ((a -> b) -> v a -> v b
forall a b. (a -> b) -> v a -> v b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f v a
vs)


instance (forall a. Vector v a, Functor v) => FunctorWithIndex k (Series v k) where
    {-# INLINE imap #-}
    imap :: (k -> a -> b) -> Series v k a -> Series v k b
    imap :: forall a b. (k -> a -> b) -> Series v k a -> Series v k b
imap = (k -> a -> b) -> Series v k a -> Series v k b
forall (v :: * -> *) a b k.
(Vector v a, Vector v b) =>
(k -> a -> b) -> Series v k a -> Series v k b
mapWithKey


-- Inlining all methods in 'Foldable'
-- is important in order for folds over a boxed
-- Series to have performance characteristics
-- be as close as possible to boxed vectors 
instance (Foldable v) => Foldable (Series v k) where
    {-# INLINE fold #-}
    fold :: Monoid m => Series v k m -> m
    fold :: forall m. Monoid m => Series v k m -> m
fold = v m -> m
forall m. Monoid m => v m -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
Foldable.fold (v m -> m) -> (Series v k m -> v m) -> Series v k m -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k m -> v m
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldMap #-}
    foldMap :: (Monoid m) => (a -> m) -> Series v k a -> m
    foldMap :: forall m a. Monoid m => (a -> m) -> Series v k a -> m
foldMap a -> m
f = (a -> m) -> v a -> m
forall m a. Monoid m => (a -> m) -> v a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap a -> m
f (v a -> m) -> (Series v k a -> v a) -> Series v k a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldMap' #-}
    foldMap' :: (Monoid m) => (a -> m) -> Series v k a -> m
    foldMap' :: forall m a. Monoid m => (a -> m) -> Series v k a -> m
foldMap' a -> m
f = (a -> m) -> v a -> m
forall m a. Monoid m => (a -> m) -> v a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap a -> m
f (v a -> m) -> (Series v k a -> v a) -> Series v k a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldr #-}
    foldr :: (a -> b -> b) -> b -> Series v k a -> b
    foldr :: forall a b. (a -> b -> b) -> b -> Series v k a -> b
foldr a -> b -> b
f b
i = (a -> b -> b) -> b -> v a -> b
forall a b. (a -> b -> b) -> b -> v a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr a -> b -> b
f b
i (v a -> b) -> (Series v k a -> v a) -> Series v k a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldr' #-}
    foldr' :: (a -> b -> b) -> b -> Series v k a -> b
    foldr' :: forall a b. (a -> b -> b) -> b -> Series v k a -> b
foldr' a -> b -> b
f b
i = (a -> b -> b) -> b -> v a -> b
forall a b. (a -> b -> b) -> b -> v a -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr' a -> b -> b
f b
i (v a -> b) -> (Series v k a -> v a) -> Series v k a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldl #-}
    foldl :: (b -> a -> b) -> b -> Series v k a -> b
    foldl :: forall b a. (b -> a -> b) -> b -> Series v k a -> b
foldl b -> a -> b
f b
i = (b -> a -> b) -> b -> v a -> b
forall b a. (b -> a -> b) -> b -> v a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl b -> a -> b
f b
i (v a -> b) -> (Series v k a -> v a) -> Series v k a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldl' #-}
    foldl' :: (b -> a -> b) -> b -> Series v k a -> b
    foldl' :: forall b a. (b -> a -> b) -> b -> Series v k a -> b
foldl' b -> a -> b
f b
i = (b -> a -> b) -> b -> v a -> b
forall b a. (b -> a -> b) -> b -> v a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl' b -> a -> b
f b
i (v a -> b) -> (Series v k a -> v a) -> Series v k a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldr1 #-}
    foldr1 :: (a -> a -> a) -> Series v k a -> a
    foldr1 :: forall a. (a -> a -> a) -> Series v k a -> a
foldr1 a -> a -> a
f = (a -> a -> a) -> v a -> a
forall a. (a -> a -> a) -> v a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Foldable.foldr1 a -> a -> a
f (v a -> a) -> (Series v k a -> v a) -> Series v k a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE foldl1 #-}
    foldl1 :: (a -> a -> a) -> Series v k a -> a
    foldl1 :: forall a. (a -> a -> a) -> Series v k a -> a
foldl1 a -> a -> a
f = (a -> a -> a) -> v a -> a
forall a. (a -> a -> a) -> v a -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
Foldable.foldl1 a -> a -> a
f (v a -> a) -> (Series v k a -> v a) -> Series v k a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE toList #-}
    toList :: Series v k a -> [a]
    toList :: forall a. Series v k a -> [a]
toList = v a -> [a]
forall a. v a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
Foldable.toList (v a -> [a]) -> (Series v k a -> v a) -> Series v k a -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE null #-}
    null :: Series v k a -> Bool
    null :: forall a. Series v k a -> Bool
null = v a -> Bool
forall a. v a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
Foldable.null (v a -> Bool) -> (Series v k a -> v a) -> Series v k a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE length #-}
    length :: Series v k a -> Int
    length :: forall a. Series v k a -> Int
length = v a -> Int
forall a. v a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Foldable.length (v a -> Int) -> (Series v k a -> v a) -> Series v k a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE elem #-}
    elem :: Eq a => a -> Series v k a -> Bool
    elem :: forall a. Eq a => a -> Series v k a -> Bool
elem a
e = a -> v a -> Bool
forall a. Eq a => a -> v a -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
Foldable.elem a
e (v a -> Bool) -> (Series v k a -> v a) -> Series v k a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE maximum #-}
    maximum :: Ord a => Series v k a -> a
    maximum :: forall a. Ord a => Series v k a -> a
maximum = v a -> a
forall a. Ord a => v a -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
Foldable.maximum (v a -> a) -> (Series v k a -> v a) -> Series v k a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE minimum #-}
    minimum :: Ord a => Series v k a -> a
    minimum :: forall a. Ord a => Series v k a -> a
minimum = v a -> a
forall a. Ord a => v a -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
Foldable.minimum (v a -> a) -> (Series v k a -> v a) -> Series v k a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE sum #-}
    sum :: Num a => Series v k a -> a
    sum :: forall a. Num a => Series v k a -> a
sum = v a -> a
forall a. Num a => v a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Foldable.sum (v a -> a) -> (Series v k a -> v a) -> Series v k a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values

    {-# INLINE product #-}
    product :: Num a => Series v k a -> a
    product :: forall a. Num a => Series v k a -> a
product = v a -> a
forall a. Num a => v a -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Foldable.product (v a -> a) -> (Series v k a -> v a) -> Series v k a -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values


instance (forall a. Vector v a, Vector v k, Foldable v, Functor v) => FoldableWithIndex k (Series v k) where
    {-# INLINE ifoldMap #-}
    ifoldMap :: Monoid m => (k -> a -> m) -> Series v k a -> m
    ifoldMap :: forall m a. Monoid m => (k -> a -> m) -> Series v k a -> m
ifoldMap = (k -> a -> m) -> Series v k a -> m
forall m (v :: * -> *) a k.
(Monoid m, Vector v a, Vector v k, Vector v (k, a)) =>
(k -> a -> m) -> Series v k a -> m
foldMapWithKey


instance (Foldable v) => Bifoldable (Series v) where
    {-# INLINE bifoldMap #-}
    bifoldMap :: Monoid m => (k -> m) -> (a -> m) -> Series v k a -> m
    bifoldMap :: forall m a b. Monoid m => (a -> m) -> (b -> m) -> Series v a b -> m
bifoldMap k -> m
fk a -> m
fv (MkSeries Index k
ks v a
vs) = (k -> m) -> Index k -> m
forall m a. Monoid m => (a -> m) -> Index a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
P.foldMap k -> m
fk Index k
ks m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> v a -> m
forall m a. Monoid m => (a -> m) -> v a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
Foldable.foldMap a -> m
fv v a
vs


instance (Traversable v) => Traversable (Series v k) where
    {-# INLINE traverse #-}
    traverse :: Applicative f
             => (a -> f b) -> Series v k a -> f (Series v k b)
    traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Series v k a -> f (Series v k b)
traverse a -> f b
f (MkSeries Index k
ix v a
vs) = Index k -> v b -> Series v k b
forall {k} (v :: k -> *) k (a :: k). Index k -> v a -> Series v k a
MkSeries Index k
ix (v b -> Series v k b) -> f (v b) -> f (Series v k b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> v a -> f (v b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> v a -> f (v b)
traverse a -> f b
f v a
vs


instance (forall a. Vector v a, Functor v, Foldable v, Ord k, Traversable v) => TraversableWithIndex k (Series v k) where
    {-# INLINE itraverse #-}
    itraverse :: Applicative f => (k -> a -> f b) -> Series v k a -> f (Series v k b)
    itraverse :: forall (f :: * -> *) a b.
Applicative f =>
(k -> a -> f b) -> Series v k a -> f (Series v k b)
itraverse = (k -> a -> f b) -> Series v k a -> f (Series v k b)
forall (t :: * -> *) k (v :: * -> *) a b.
(Applicative t, Ord k, Traversable v, Vector v a, Vector v b,
 Vector v k, Vector v (k, a), Vector v (k, b)) =>
(k -> a -> t b) -> Series v k a -> t (Series v k b)
traverseWithKey


-- | \(O(n)\) Execute a 'Fold' over a 'Series'.
--
-- See also 'foldM' for monadic folds, and 'foldWithKey' to take keys into
-- account while folding.
fold :: Vector v a 
     => Fold a b  
     -> Series v k a 
     -> b
fold :: forall (v :: * -> *) a b k.
Vector v a =>
Fold a b -> Series v k a -> b
fold (Fold x -> a -> x
step x
init' x -> b
extract) 
    = x -> b
extract (x -> b) -> (Series v k a -> x) -> Series v k a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (x -> a -> x) -> x -> v a -> x
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
Vector.foldl' x -> a -> x
step x
init' (v a -> x) -> (Series v k a -> v a) -> Series v k a -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values
{-# INLINE fold #-}


-- | \(O(n)\) Execute a monadic 'FoldM' over a 'Series'.
--
-- See also 'fold' for pure folds, and 'foldMWithKey' to take keys into
-- account while folding.
foldM :: (Monad m, Vector v a)
      => FoldM m a b  
      -> Series v k a 
      -> m b
foldM :: forall (m :: * -> *) (v :: * -> *) a b k.
(Monad m, Vector v a) =>
FoldM m a b -> Series v k a -> m b
foldM (FoldM x -> a -> m x
step m x
init' x -> m b
extract) Series v k a
xs
    = m x
init' m x -> (x -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
i -> (x -> a -> m x) -> x -> v a -> m x
forall (m :: * -> *) (v :: * -> *) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m a
Vector.foldM' x -> a -> m x
step x
i (Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values Series v k a
xs) m x -> (x -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m b
extract
{-# INLINE foldM #-}


-- | \(O(n)\) Execute a 'Fold' over a 'Series', where the 'Fold' takes keys into account.
foldWithKey :: (Vector v a, Vector v k, Vector v (k, a)) 
            => Fold (k, a) b  
            -> Series v k a 
            -> b
foldWithKey :: forall (v :: * -> *) a k b.
(Vector v a, Vector v k, Vector v (k, a)) =>
Fold (k, a) b -> Series v k a -> b
foldWithKey (Fold x -> (k, a) -> x
step x
init' x -> b
extract) 
    = x -> b
extract (x -> b) -> (Series v k a -> x) -> Series v k a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (x -> (k, a) -> x) -> x -> v (k, a) -> x
forall (v :: * -> *) b a.
Vector v b =>
(a -> b -> a) -> a -> v b -> a
Vector.foldl' x -> (k, a) -> x
step x
init' (v (k, a) -> x) -> (Series v k a -> v (k, a)) -> Series v k a -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector
{-# INLINE foldWithKey #-}


-- | \(O(n)\) Execute a monadic 'FoldM' over a 'Series', where the 'FoldM' takes keys into account.
foldMWithKey :: (Monad m, Vector v a, Vector v k, Vector v (k, a)) 
             => FoldM m (k, a) b
             -> Series v k a 
             -> m b
foldMWithKey :: forall (m :: * -> *) (v :: * -> *) a k b.
(Monad m, Vector v a, Vector v k, Vector v (k, a)) =>
FoldM m (k, a) b -> Series v k a -> m b
foldMWithKey (FoldM x -> (k, a) -> m x
step m x
init' x -> m b
extract) Series v k a
xs
    = m x
init' m x -> (x -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \x
i -> (x -> (k, a) -> m x) -> x -> v (k, a) -> m x
forall (m :: * -> *) (v :: * -> *) b a.
(Monad m, Vector v b) =>
(a -> b -> m a) -> a -> v b -> m a
Vector.foldM' x -> (k, a) -> m x
step x
i (Series v k a -> v (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector Series v k a
xs) m x -> (x -> m b) -> m b
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= x -> m b
extract
{-# INLINE foldMWithKey #-}


-- | \(O(n)\) Fold over elements in a 'Series'.
foldMap :: (Monoid m, Vector v a) => (a -> m) -> Series v k a -> m
{-# INLINE foldMap #-}
foldMap :: forall m (v :: * -> *) a k.
(Monoid m, Vector v a) =>
(a -> m) -> Series v k a -> m
foldMap a -> m
f = (a -> m) -> v a -> m
forall m (v :: * -> *) a.
(Monoid m, Vector v a) =>
(a -> m) -> v a -> m
Vector.foldMap a -> m
f (v a -> m) -> (Series v k a -> v a) -> Series v k a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values


-- | \(O(n)\) Fold over pairs of keys and elements in a 'Series'.
-- See also 'bifoldMap'.
foldMapWithKey :: (Monoid m, Vector v a, Vector v k, Vector v (k, a)) => (k -> a -> m) -> Series v k a -> m
{-# INLINE foldMapWithKey #-}
foldMapWithKey :: forall m (v :: * -> *) a k.
(Monoid m, Vector v a, Vector v k, Vector v (k, a)) =>
(k -> a -> m) -> Series v k a -> m
foldMapWithKey k -> a -> m
f = ((k, a) -> m) -> v (k, a) -> m
forall m (v :: * -> *) a.
(Monoid m, Vector v a) =>
(a -> m) -> v a -> m
Vector.foldMap ((k -> a -> m) -> (k, a) -> m
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry k -> a -> m
f) (v (k, a) -> m) -> (Series v k a -> v (k, a)) -> Series v k a -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector


-- | \(O(n)\) Fold over keys and elements separately in a 'Series'.
-- See also 'foldMapWithKey'.
bifoldMap :: (Vector v a, Monoid m) => (k -> m) -> (a -> m) -> Series v k a -> m
{-# INLINE bifoldMap #-}
bifoldMap :: forall (v :: * -> *) a m k.
(Vector v a, Monoid m) =>
(k -> m) -> (a -> m) -> Series v k a -> m
bifoldMap k -> m
fk a -> m
fv (MkSeries Index k
ks v a
vs) = (k -> m) -> Index k -> m
forall m a. Monoid m => (a -> m) -> Index a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
P.foldMap k -> m
fk Index k
ks m -> m -> m
forall a. Semigroup a => a -> a -> a
<> (a -> m) -> v a -> m
forall m (v :: * -> *) a.
(Monoid m, Vector v a) =>
(a -> m) -> v a -> m
Vector.foldMap a -> m
fv v a
vs


-- | \(O(1)\) Extract the length of a 'Series'.
length :: Vector v a => Series v k a -> Int
{-# INLINE length #-}
length :: forall (v :: * -> *) a k. Vector v a => Series v k a -> Int
length = v a -> Int
forall (v :: * -> *) a. Vector v a => v a -> Int
Vector.length (v a -> Int) -> (Series v k a -> v a) -> Series v k a -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values


-- | \(O(1)\) Test whether a 'Series' is empty.
null :: Vector v a => Series v k a -> Bool
{-# INLINE null #-}
null :: forall (v :: * -> *) a k. Vector v a => Series v k a -> Bool
null = v a -> Bool
forall (v :: * -> *) a. Vector v a => v a -> Bool
Vector.null (v a -> Bool) -> (Series v k a -> v a) -> Series v k a -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v a
forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values


-- | \(O(n)\) Apply the monadic action to every element of a series and its
-- index, yielding a series of results.
mapWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) 
            => (k -> a -> m b) -> Series v k a -> m (Series v k b)
{-# INLINE mapWithKeyM #-}
mapWithKeyM :: forall (v :: * -> *) a b (m :: * -> *) k.
(Vector v a, Vector v b, Monad m, Ord k) =>
(k -> a -> m b) -> Series v k a -> m (Series v k b)
mapWithKeyM k -> a -> m b
f Series v k a
xs = let f' :: (k, a) -> m (k, b)
f' (k
key, a
val) = (k
key,) (b -> (k, b)) -> m b -> m (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> a -> m b
f k
key a
val
           in ([(k, b)] -> Series v k b) -> m [(k, b)] -> m (Series v k b)
forall a b. (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [(k, b)] -> Series v k b
forall (v :: * -> *) a k.
(Vector v a, Ord k) =>
[(k, a)] -> Series v k a
fromList (m [(k, b)] -> m (Series v k b)) -> m [(k, b)] -> m (Series v k b)
forall a b. (a -> b) -> a -> b
$ ((k, a) -> m (k, b)) -> [(k, a)] -> m [(k, b)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (k, a) -> m (k, b)
f' ([(k, a)] -> m [(k, b)]) -> [(k, a)] -> m [(k, b)]
forall a b. (a -> b) -> a -> b
$ Series v k a -> [(k, a)]
forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList Series v k a
xs


-- | \(O(n)\) Apply the monadic action to every element of a series and its
-- index, discarding the results.
mapWithKeyM_ :: (Vector v a, Monad m) 
             => (k -> a -> m b) -> Series v k a -> m ()
{-# INLINE mapWithKeyM_ #-}
mapWithKeyM_ :: forall (v :: * -> *) a (m :: * -> *) k b.
(Vector v a, Monad m) =>
(k -> a -> m b) -> Series v k a -> m ()
mapWithKeyM_ k -> a -> m b
f Series v k a
xs = let f' :: (k, a) -> m (k, b)
f' (k
key, a
val) = (k
key,) (b -> (k, b)) -> m b -> m (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> a -> m b
f k
key a
val
           in ((k, a) -> m (k, b)) -> [(k, a)] -> m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (k, a) -> m (k, b)
f' ([(k, a)] -> m ()) -> [(k, a)] -> m ()
forall a b. (a -> b) -> a -> b
$ Series v k a -> [(k, a)]
forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList Series v k a
xs


-- | \(O(n)\) Apply the monadic action to all elements of the series and their associated keys, 
-- yielding a series of results.
forWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) => Series v k a -> (k -> a -> m b) -> m (Series v k b)
{-# INLINE forWithKeyM #-}
forWithKeyM :: forall (v :: * -> *) a b (m :: * -> *) k.
(Vector v a, Vector v b, Monad m, Ord k) =>
Series v k a -> (k -> a -> m b) -> m (Series v k b)
forWithKeyM = ((k -> a -> m b) -> Series v k a -> m (Series v k b))
-> Series v k a -> (k -> a -> m b) -> m (Series v k b)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (k -> a -> m b) -> Series v k a -> m (Series v k b)
forall (v :: * -> *) a b (m :: * -> *) k.
(Vector v a, Vector v b, Monad m, Ord k) =>
(k -> a -> m b) -> Series v k a -> m (Series v k b)
mapWithKeyM


-- | \(O(n)\) Apply the monadic action to all elements of the series and their associated keys, 
-- discarding the results.
forWithKeyM_ :: (Vector v a, Monad m) => Series v k a -> (k -> a -> m b) -> m ()
{-# INLINE forWithKeyM_ #-}
forWithKeyM_ :: forall (v :: * -> *) a (m :: * -> *) k b.
(Vector v a, Monad m) =>
Series v k a -> (k -> a -> m b) -> m ()
forWithKeyM_ = ((k -> a -> m b) -> Series v k a -> m ())
-> Series v k a -> (k -> a -> m b) -> m ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip (k -> a -> m b) -> Series v k a -> m ()
forall (v :: * -> *) a (m :: * -> *) k b.
(Vector v a, Monad m) =>
(k -> a -> m b) -> Series v k a -> m ()
mapWithKeyM_


-- | \(O(n)\) Traverse a 'Series' with an Applicative action, taking into account both keys and values. 
traverseWithKey :: (Applicative t, Ord k, Traversable v, Vector v a, Vector v b, Vector v k, Vector v (k, a),  Vector v (k, b))
                => (k -> a -> t b) 
                -> Series v k a 
                -> t (Series v k b)
{-# INLINE traverseWithKey #-}
traverseWithKey :: forall (t :: * -> *) k (v :: * -> *) a b.
(Applicative t, Ord k, Traversable v, Vector v a, Vector v b,
 Vector v k, Vector v (k, a), Vector v (k, b)) =>
(k -> a -> t b) -> Series v k a -> t (Series v k b)
traverseWithKey k -> a -> t b
f = (v (k, b) -> Series v k b) -> t (v (k, b)) -> t (Series v k b)
forall a b. (a -> b) -> t a -> t b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap v (k, b) -> Series v k b
forall k (v :: * -> *) a.
(Ord k, Vector v k, Vector v a, Vector v (k, a)) =>
v (k, a) -> Series v k a
fromVector 
                  (t (v (k, b)) -> t (Series v k b))
-> (Series v k a -> t (v (k, b)))
-> Series v k a
-> t (Series v k b)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((k, a) -> t (k, b)) -> v (k, a) -> t (v (k, b))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> v a -> f (v b)
traverse (\(k
k, a
x) -> (k
k,) (b -> (k, b)) -> t b -> t (k, b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> k -> a -> t b
f k
k a
x) 
                  (v (k, a) -> t (v (k, b)))
-> (Series v k a -> v (k, a)) -> Series v k a -> t (v (k, b))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Series v k a -> v (k, a)
forall (v :: * -> *) a k.
(Vector v a, Vector v k, Vector v (k, a)) =>
Series v k a -> v (k, a)
toVector


instance (NFData (v a), NFData k) => NFData (Series v k a) where
    rnf :: Series v k a -> ()
    rnf :: Series v k a -> ()
rnf (MkSeries Index k
ks v a
vs) = Index k -> ()
forall a. NFData a => a -> ()
rnf Index k
ks () -> () -> ()
forall a b. a -> b -> b
`seq` v a -> ()
forall a. NFData a => a -> ()
rnf v a
vs


instance (Vector v a, Ord k, Show k, Show a) => Show (Series v k a) where
    show :: Series v k a -> String
    show :: Series v k a -> String
show = Series v k a -> String
forall (v :: * -> *) a k.
(Vector v a, Show k, Show a) =>
Series v k a -> String
display


-- | Options controlling how to display 'Series' in the 'displayWith' function.
-- Default options are provided by 'defaultDisplayOptions'.
--
-- To help with creating 'DisplayOptions', see 'noLongerThan'.
data DisplayOptions k a
    = DisplayOptions
    { forall k a. DisplayOptions k a -> Int
maximumNumberOfRows  :: Int
    -- ^ Maximum number of rows shown. These rows will be distributed evenly
    -- between the start of the 'Series' and the end. 
    , forall k a. DisplayOptions k a -> String
indexHeader          :: String
    -- ^ Header of the index column.
    , forall k a. DisplayOptions k a -> String
valuesHeader         :: String
    -- ^ Header of the values column.
    , forall k a. DisplayOptions k a -> k -> String
keyDisplayFunction   :: k -> String
    -- ^ Function used to display keys from the 'Series'. Use 'noLongerThan'
    -- to control the width of the index column.
    , forall k a. DisplayOptions k a -> a -> String
valueDisplayFunction :: a -> String
    -- ^ Function used to display values from the 'Series'. Use 'noLongerThan'
    -- to control the width of the values column.
    }


-- | Default 'Series' display options.
defaultDisplayOptions :: (Show k, Show a) => DisplayOptions k a
defaultDisplayOptions :: forall k a. (Show k, Show a) => DisplayOptions k a
defaultDisplayOptions 
    = DisplayOptions { maximumNumberOfRows :: Int
maximumNumberOfRows  = Int
6
                     , indexHeader :: String
indexHeader          = String
"index"
                     , valuesHeader :: String
valuesHeader         = String
"values"
                     , keyDisplayFunction :: k -> String
keyDisplayFunction   = k -> String
forall a. Show a => a -> String
show
                     , valueDisplayFunction :: a -> String
valueDisplayFunction = a -> String
forall a. Show a => a -> String
show
                     }


-- | This function modifies existing functions to limit the width of its result.
--
-- >>> let limit7 = (show :: Int -> String) `noLongerThan` 7
-- >>> limit7 123456789
-- "123456..."
noLongerThan :: (a -> String) -> Int -> (a -> String)
noLongerThan :: forall a. (a -> String) -> Int -> a -> String
noLongerThan a -> String
f Int
len a
x 
    = let raw :: String
raw = a -> String
f a
x
       in if String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
List.length String
raw Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
len
        then String
raw
        else Int -> ShowS
forall a. Int -> [a] -> [a]
List.take (String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
List.length String
raw Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
3) String
raw String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
"..."


-- | Display a 'Series' using default 'DisplayOptions'.
display :: (Vector v a, Show k, Show a) 
        => Series v k a 
        -> String
display :: forall (v :: * -> *) a k.
(Vector v a, Show k, Show a) =>
Series v k a -> String
display = DisplayOptions k a -> Series v k a -> String
forall (v :: * -> *) a k.
Vector v a =>
DisplayOptions k a -> Series v k a -> String
displayWith DisplayOptions k a
forall k a. (Show k, Show a) => DisplayOptions k a
defaultDisplayOptions


-- | Display a 'Series' using customizable 'DisplayOptions'.
displayWith :: (Vector v a) 
            => DisplayOptions k a
            -> Series v k a 
            -> String
displayWith :: forall (v :: * -> *) a k.
Vector v a =>
DisplayOptions k a -> Series v k a -> String
displayWith DisplayOptions{Int
String
a -> String
k -> String
maximumNumberOfRows :: forall k a. DisplayOptions k a -> Int
indexHeader :: forall k a. DisplayOptions k a -> String
valuesHeader :: forall k a. DisplayOptions k a -> String
keyDisplayFunction :: forall k a. DisplayOptions k a -> k -> String
valueDisplayFunction :: forall k a. DisplayOptions k a -> a -> String
maximumNumberOfRows :: Int
indexHeader :: String
valuesHeader :: String
keyDisplayFunction :: k -> String
valueDisplayFunction :: a -> String
..} Series v k a
xs
    = [(String, String)] -> String
formatGrid ([(String, String)] -> String) -> [(String, String)] -> String
forall a b. (a -> b) -> a -> b
$ if Series v k a -> Int
forall (v :: * -> *) a k. Vector v a => Series v k a -> Int
length Series v k a
xs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
maximumNumberOfRows
        then let headlength :: Int
headlength = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
maximumNumberOfRows Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
                 taillength :: Int
taillength = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
maximumNumberOfRows Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
headlength
              in [[(String, String)]] -> [(String, String)]
forall a. Monoid a => [a] -> a
mconcat [ [ (k -> String
keyDisplayFunction k
k, a -> String
valueDisplayFunction a
v) | (k
k, a
v) <- Series v k a -> [(k, a)]
forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList (Series v k a -> [(k, a)]) -> Series v k a -> [(k, a)]
forall a b. (a -> b) -> a -> b
$ Int -> Series v k a -> Series v k a
forall (v :: * -> *) a k.
Vector v a =>
Int -> Series v k a -> Series v k a
take Int
headlength Series v k a
xs]
                         , [ (String
"...", String
"...") ]
                         , [ (k -> String
keyDisplayFunction k
k, a -> String
valueDisplayFunction a
v) | (k
k, a
v) <- Series v k a -> [(k, a)]
forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList (Series v k a -> [(k, a)]) -> Series v k a -> [(k, a)]
forall a b. (a -> b) -> a -> b
$ Int -> Series v k a -> Series v k a
forall (v :: * -> *) a k.
Vector v a =>
Int -> Series v k a -> Series v k a
drop (Series v k a -> Int
forall (v :: * -> *) a k. Vector v a => Series v k a -> Int
length Series v k a
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
taillength) Series v k a
xs]
                         ] 
        else [ (k -> String
keyDisplayFunction k
k, a -> String
valueDisplayFunction a
v) | (k
k, a
v) <- Series v k a -> [(k, a)]
forall (v :: * -> *) a k. Vector v a => Series v k a -> [(k, a)]
toList Series v k a
xs ]

    where
        -- | Format a grid represented by a list of rows, where every row is a list of items
        -- All columns will have a fixed width
        formatGrid :: [ (String, String) ] -- List of rows
                   -> String
        formatGrid :: [(String, String)] -> String
formatGrid [(String, String)]
rows = [String] -> String
forall a. Monoid a => [a] -> a
mconcat ([String] -> String) -> [String] -> String
forall a b. (a -> b) -> a -> b
$ String -> [String] -> [String]
forall a. a -> [a] -> [a]
List.intersperse String
"\n" 
                                  ([String] -> [String]) -> [String] -> [String]
forall a b. (a -> b) -> a -> b
$ [ Int -> ShowS
pad Int
indexWidth String
k String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" | " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> ShowS
pad Int
valuesWidth String
v 
                                    | (String
k, String
v) <- [(String, String)]
rows'
                                    ] 
            where
                rows' :: [(String, String)]
rows' = [ (String
indexHeader, String
valuesHeader) ] [(String, String)] -> [(String, String)] -> [(String, String)]
forall a. Semigroup a => a -> a -> a
<> [ (String
"-----", String
"------")] [(String, String)] -> [(String, String)] -> [(String, String)]
forall a. Semigroup a => a -> a -> a
<> [(String, String)]
rows
                ([String]
indexCol, [String]
valuesCol) = [(String, String)] -> ([String], [String])
forall a b. [(a, b)] -> ([a], [b])
unzip [(String, String)]
rows'
                width :: t (t a) -> Int
width t (t a)
col = t Int -> Int
forall a. Ord a => t a -> a
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum (t a -> Int
forall a. t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
P.length (t a -> Int) -> t (t a) -> t Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> t (t a)
col)
                indexWidth :: Int
indexWidth = [String] -> Int
forall {t :: * -> *} {t :: * -> *} {a}.
(Functor t, Foldable t, Foldable t) =>
t (t a) -> Int
width [String]
indexCol
                valuesWidth :: Int
valuesWidth = [String] -> Int
forall {t :: * -> *} {t :: * -> *} {a}.
(Functor t, Foldable t, Foldable t) =>
t (t a) -> Int
width [String]
valuesCol

                -- | Pad a string to a minimum of @n@ characters wide.
                pad :: Int -> String -> String 
                pad :: Int -> ShowS
pad Int
n String
s
                    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
P.length String
s = String
s
                    | Bool
otherwise     = Int -> Char -> String
forall a. Int -> a -> [a]
replicate (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- String -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
P.length String
s) Char
' ' String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
s