{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Series.Generic.Definition (
Series(..),
convert,
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,
IsSeries(..),
fromStrictMap,
toStrictMap,
fromLazyMap,
toLazyMap,
fromList,
toList,
fromDistinctAscList,
fromVector,
toVector,
fromDistinctAscVector,
Occurrence, fromListDuplicates, fromVectorDuplicates,
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
data Series v k a
= MkSeries { forall {k} (v :: k -> *) k (a :: k). Series v k a -> Index k
index :: Index k
, forall {k} (v :: k -> *) k (a :: k). Series v k a -> v a
values :: v a
}
convert :: (Vector v1 a, Vector v2 a) => Series v1 k a -> Series v2 k a
{-# INLINABLE 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
singleton :: Vector v a => k -> a -> Series v k a
{-# INLINABLE 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
fromIndex :: (Vector v a)
=> (k -> a) -> Index k -> Series v k a
{-# INLINABLE 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
(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
class IsSeries t v k a where
toSeries :: t -> Series v k a
fromSeries :: Series v k a -> t
instance (Ord k, Vector v a) => IsSeries [(k, a)] v k a where
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
{-# INLINABLE toSeries #-}
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)
{-# INLINABLE fromSeries #-}
fromList :: (Vector v a, Ord k) => [(k, a)] -> Series v k a
{-# INLINABLE 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
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)
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)
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
fromListDuplicates :: (Vector v a, Ord k) => [(k, a)] -> Series v (k, Occurrence) a
{-# INLINABLE 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
toList :: Vector v a => Series v k a -> [(k, a)]
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE fromSeries #-}
fromVector :: (Ord k, Vector v k, Vector v a, Vector v (k, a))
=> v (k, a) -> Series v k a
{-# INLINABLE 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
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
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
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
{-# INLINABLE 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)
toVector :: (Vector v a, Vector v k, Vector v (k, a))
=> Series v k a -> v (k, a)
{-# INLINABLE 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
}
{-# INLINABLE 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)
{-# INLINABLE fromSeries #-}
toLazyMap :: (Vector v a) => Series v k a -> Map k a
{-# INLINABLE 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
fromLazyMap :: (Vector v a) => ML.Map k a -> Series v k a
{-# INLINABLE 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
toStrictMap :: (Vector v a) => Series v k a -> Map k a
{-# INLINABLE 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)
fromStrictMap :: (Vector v a) => MS.Map k a -> Series v k a
{-# INLINABLE 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
}
{-# INLINABLE 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)
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE fromSeries #-}
headM :: Vector v a => Series v k a -> Maybe a
{-# INLINABLE 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
lastM :: Vector v a => Series v k a -> Maybe a
{-# INLINABLE 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
take :: Vector v a => Int -> Series v k a -> Series v k a
{-# INLINABLE 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 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)
drop :: Vector v a => Int -> Series v k a -> Series v k a
{-# INLINABLE 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 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)
takeWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a
{-# INLINABLE 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
}
dropWhile :: Vector v a => (a -> Bool) -> Series v k a -> Series v k a
{-# INLINABLE 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
}
map :: (Vector v a, Vector v b)
=> (a -> b) -> Series v k a -> Series v k b
{-# INLINABLE 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
mapWithKey :: (Vector v a, Vector v b)
=> (k -> a -> b) -> Series v k a -> Series v k b
{-# INLINABLE 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)
= 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)
mapIndex :: (Vector v a, Ord k, Ord g) => Series v k a -> (k -> g) -> Series v g a
{-# INLINABLE 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
= 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
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
{-# INLINABLE 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
{-# INLINABLE (<>) #-}
(<>) :: Series v k a -> Series v k a -> Series v k a
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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
(<>)
{-# INLINABLE 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
{-# INLINABLE (==) #-}
(==) :: 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
instance (Foldable v) => Foldable (Series v k) where
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
{-# INLINABLE 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
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
{-# INLINABLE fold #-}
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
{-# INLINABLE foldM #-}
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
{-# INLINABLE foldWithKey #-}
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
{-# INLINABLE foldMWithKey #-}
foldMap :: (Monoid m, Vector v a) => (a -> m) -> Series v k a -> m
{-# INLINABLE 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
foldMapWithKey :: (Monoid m, Vector v a, Vector v k, Vector v (k, a)) => (k -> a -> m) -> Series v k a -> m
{-# INLINABLE 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
bifoldMap :: (Vector v a, Monoid m) => (k -> m) -> (a -> m) -> Series v k a -> m
{-# INLINABLE 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
length :: Vector v a => Series v k a -> Int
{-# INLINABLE 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
null :: Vector v a => Series v k a -> Bool
{-# INLINABLE 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
mapWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k)
=> (k -> a -> m b) -> Series v k a -> m (Series v k b)
{-# INLINABLE 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
mapWithKeyM_ :: (Vector v a, Monad m)
=> (k -> a -> m b) -> Series v k a -> m ()
{-# INLINABLE 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
forWithKeyM :: (Vector v a, Vector v b, Monad m, Ord k) => Series v k a -> (k -> a -> m b) -> m (Series v k b)
{-# INLINABLE 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
forWithKeyM_ :: (Vector v a, Monad m) => Series v k a -> (k -> a -> m b) -> m ()
{-# INLINABLE 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_
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)
{-# INLINABLE 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
data DisplayOptions k a
= DisplayOptions
{ forall k a. DisplayOptions k a -> Int
maximumNumberOfRows :: Int
, :: String
, :: String
, forall k a. DisplayOptions k a -> k -> String
keyDisplayFunction :: k -> String
, forall k a. DisplayOptions k a -> a -> String
valueDisplayFunction :: a -> String
}
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
}
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 :: (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
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
formatGrid :: [ (String, String) ]
-> 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 :: 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