{-|
Module      : Prosidy.Types.Series
Description : A type of items occuring in a sequence.
Copyright   : ©2020 James Alexander Feldman-Crough
License     : MPL-2.0
Maintainer  : alex@fldcr.com
-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Prosidy.Types.Series
    ( -- * Possibly empty collections
      Series(..)
    , asSeq
    , fromSeq
    , toSeq
      -- * Known non-empty collections
    , SeriesNE
    , fromSeqNE
    , toSeqNE
    )
where

import           Data.Sequence                  ( Seq )
import           GHC.Generics                   ( Generic )
import           Data.Aeson                     ( ToJSON(..)
                                                , FromJSON(..)
                                                )
import           Control.DeepSeq                ( NFData )
import           Data.Binary                    ( Binary(..) )
import           Data.Hashable                  ( Hashable(..) )
import           Data.Foldable                  ( toList
                                                , foldl'
                                                )
import           Control.Monad                  ( guard )

import qualified Data.Sequence                 as Seq

-- | A newtype wrapper around a sequential collection.
--
-- Currently, 'Series' is implemented as a 'Seq', but this is not guarenteed to
-- be true.
newtype Series a = Series (Seq a)
  deriving stock ((forall x. Series a -> Rep (Series a) x)
-> (forall x. Rep (Series a) x -> Series a) -> Generic (Series a)
forall x. Rep (Series a) x -> Series a
forall x. Series a -> Rep (Series a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Series a) x -> Series a
forall a x. Series a -> Rep (Series a) x
$cto :: forall a x. Rep (Series a) x -> Series a
$cfrom :: forall a x. Series a -> Rep (Series a) x
Generic, Int -> Series a -> ShowS
[Series a] -> ShowS
Series a -> String
(Int -> Series a -> ShowS)
-> (Series a -> String) -> ([Series a] -> ShowS) -> Show (Series a)
forall a. Show a => Int -> Series a -> ShowS
forall a. Show a => [Series a] -> ShowS
forall a. Show a => Series a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Series a] -> ShowS
$cshowList :: forall a. Show a => [Series a] -> ShowS
show :: Series a -> String
$cshow :: forall a. Show a => Series a -> String
showsPrec :: Int -> Series a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Series a -> ShowS
Show)
  deriving newtype (Series a -> Series a -> Bool
(Series a -> Series a -> Bool)
-> (Series a -> Series a -> Bool) -> Eq (Series a)
forall a. Eq a => Series a -> Series a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Series a -> Series a -> Bool
$c/= :: forall a. Eq a => Series a -> Series a -> Bool
== :: Series a -> Series a -> Bool
$c== :: forall a. Eq a => Series a -> Series a -> Bool
Eq, a -> Series a -> Bool
Series m -> m
Series a -> [a]
Series a -> Bool
Series a -> Int
Series a -> a
Series a -> a
Series a -> a
Series a -> a
(a -> m) -> Series a -> m
(a -> m) -> Series a -> m
(a -> b -> b) -> b -> Series a -> b
(a -> b -> b) -> b -> Series a -> b
(b -> a -> b) -> b -> Series a -> b
(b -> a -> b) -> b -> Series a -> b
(a -> a -> a) -> Series a -> a
(a -> a -> a) -> Series a -> a
(forall m. Monoid m => Series m -> m)
-> (forall m a. Monoid m => (a -> m) -> Series a -> m)
-> (forall m a. Monoid m => (a -> m) -> Series a -> m)
-> (forall a b. (a -> b -> b) -> b -> Series a -> b)
-> (forall a b. (a -> b -> b) -> b -> Series a -> b)
-> (forall b a. (b -> a -> b) -> b -> Series a -> b)
-> (forall b a. (b -> a -> b) -> b -> Series a -> b)
-> (forall a. (a -> a -> a) -> Series a -> a)
-> (forall a. (a -> a -> a) -> Series a -> a)
-> (forall a. Series a -> [a])
-> (forall a. Series a -> Bool)
-> (forall a. Series a -> Int)
-> (forall a. Eq a => a -> Series a -> Bool)
-> (forall a. Ord a => Series a -> a)
-> (forall a. Ord a => Series a -> a)
-> (forall a. Num a => Series a -> a)
-> (forall a. Num a => Series a -> a)
-> Foldable Series
forall a. Eq a => a -> Series a -> Bool
forall a. Num a => Series a -> a
forall a. Ord a => Series a -> a
forall m. Monoid m => Series m -> m
forall a. Series a -> Bool
forall a. Series a -> Int
forall a. Series a -> [a]
forall a. (a -> a -> a) -> Series a -> a
forall m a. Monoid m => (a -> m) -> Series a -> m
forall b a. (b -> a -> b) -> b -> Series a -> b
forall a b. (a -> b -> b) -> b -> Series a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: Series a -> a
$cproduct :: forall a. Num a => Series a -> a
sum :: Series a -> a
$csum :: forall a. Num a => Series a -> a
minimum :: Series a -> a
$cminimum :: forall a. Ord a => Series a -> a
maximum :: Series a -> a
$cmaximum :: forall a. Ord a => Series a -> a
elem :: a -> Series a -> Bool
$celem :: forall a. Eq a => a -> Series a -> Bool
length :: Series a -> Int
$clength :: forall a. Series a -> Int
null :: Series a -> Bool
$cnull :: forall a. Series a -> Bool
toList :: Series a -> [a]
$ctoList :: forall a. Series a -> [a]
foldl1 :: (a -> a -> a) -> Series a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Series a -> a
foldr1 :: (a -> a -> a) -> Series a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> Series a -> a
foldl' :: (b -> a -> b) -> b -> Series a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Series a -> b
foldl :: (b -> a -> b) -> b -> Series a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Series a -> b
foldr' :: (a -> b -> b) -> b -> Series a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Series a -> b
foldr :: (a -> b -> b) -> b -> Series a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> Series a -> b
foldMap' :: (a -> m) -> Series a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Series a -> m
foldMap :: (a -> m) -> Series a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Series a -> m
fold :: Series m -> m
$cfold :: forall m. Monoid m => Series m -> m
Foldable, a -> Series b -> Series a
(a -> b) -> Series a -> Series b
(forall a b. (a -> b) -> Series a -> Series b)
-> (forall a b. a -> Series b -> Series a) -> Functor Series
forall a b. a -> Series b -> Series a
forall a b. (a -> b) -> Series a -> Series b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Series b -> Series a
$c<$ :: forall a b. a -> Series b -> Series a
fmap :: (a -> b) -> Series a -> Series b
$cfmap :: forall a b. (a -> b) -> Series a -> Series b
Functor, Functor Series
a -> Series a
Functor Series =>
(forall a. a -> Series a)
-> (forall a b. Series (a -> b) -> Series a -> Series b)
-> (forall a b c.
    (a -> b -> c) -> Series a -> Series b -> Series c)
-> (forall a b. Series a -> Series b -> Series b)
-> (forall a b. Series a -> Series b -> Series a)
-> Applicative Series
Series a -> Series b -> Series b
Series a -> Series b -> Series a
Series (a -> b) -> Series a -> Series b
(a -> b -> c) -> Series a -> Series b -> Series c
forall a. a -> Series a
forall a b. Series a -> Series b -> Series a
forall a b. Series a -> Series b -> Series b
forall a b. Series (a -> b) -> Series a -> Series b
forall a b c. (a -> b -> c) -> Series a -> Series b -> Series c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: Series a -> Series b -> Series a
$c<* :: forall a b. Series a -> Series b -> Series a
*> :: Series a -> Series b -> Series b
$c*> :: forall a b. Series a -> Series b -> Series b
liftA2 :: (a -> b -> c) -> Series a -> Series b -> Series c
$cliftA2 :: forall a b c. (a -> b -> c) -> Series a -> Series b -> Series c
<*> :: Series (a -> b) -> Series a -> Series b
$c<*> :: forall a b. Series (a -> b) -> Series a -> Series b
pure :: a -> Series a
$cpure :: forall a. a -> Series a
$cp1Applicative :: Functor Series
Applicative, [Series a] -> Encoding
[Series a] -> Value
Series a -> Encoding
Series a -> Value
(Series a -> Value)
-> (Series a -> Encoding)
-> ([Series a] -> Value)
-> ([Series a] -> Encoding)
-> ToJSON (Series a)
forall a. ToJSON a => [Series a] -> Encoding
forall a. ToJSON a => [Series a] -> Value
forall a. ToJSON a => Series a -> Encoding
forall a. ToJSON a => Series a -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Series a] -> Encoding
$ctoEncodingList :: forall a. ToJSON a => [Series a] -> Encoding
toJSONList :: [Series a] -> Value
$ctoJSONList :: forall a. ToJSON a => [Series a] -> Value
toEncoding :: Series a -> Encoding
$ctoEncoding :: forall a. ToJSON a => Series a -> Encoding
toJSON :: Series a -> Value
$ctoJSON :: forall a. ToJSON a => Series a -> Value
ToJSON, Value -> Parser [Series a]
Value -> Parser (Series a)
(Value -> Parser (Series a))
-> (Value -> Parser [Series a]) -> FromJSON (Series a)
forall a. FromJSON a => Value -> Parser [Series a]
forall a. FromJSON a => Value -> Parser (Series a)
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [Series a]
$cparseJSONList :: forall a. FromJSON a => Value -> Parser [Series a]
parseJSON :: Value -> Parser (Series a)
$cparseJSON :: forall a. FromJSON a => Value -> Parser (Series a)
FromJSON, Series a -> ()
(Series a -> ()) -> NFData (Series a)
forall a. NFData a => Series a -> ()
forall a. (a -> ()) -> NFData a
rnf :: Series a -> ()
$crnf :: forall a. NFData a => Series a -> ()
NFData, b -> Series a -> Series a
NonEmpty (Series a) -> Series a
Series a -> Series a -> Series a
(Series a -> Series a -> Series a)
-> (NonEmpty (Series a) -> Series a)
-> (forall b. Integral b => b -> Series a -> Series a)
-> Semigroup (Series a)
forall b. Integral b => b -> Series a -> Series a
forall a. NonEmpty (Series a) -> Series a
forall a. Series a -> Series a -> Series a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall a b. Integral b => b -> Series a -> Series a
stimes :: b -> Series a -> Series a
$cstimes :: forall a b. Integral b => b -> Series a -> Series a
sconcat :: NonEmpty (Series a) -> Series a
$csconcat :: forall a. NonEmpty (Series a) -> Series a
<> :: Series a -> Series a -> Series a
$c<> :: forall a. Series a -> Series a -> Series a
Semigroup, Semigroup (Series a)
Series a
Semigroup (Series a) =>
Series a
-> (Series a -> Series a -> Series a)
-> ([Series a] -> Series a)
-> Monoid (Series a)
[Series a] -> Series a
Series a -> Series a -> Series a
forall a. Semigroup (Series a)
forall a. Series a
forall a.
Semigroup a =>
a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall a. [Series a] -> Series a
forall a. Series a -> Series a -> Series a
mconcat :: [Series a] -> Series a
$cmconcat :: forall a. [Series a] -> Series a
mappend :: Series a -> Series a -> Series a
$cmappend :: forall a. Series a -> Series a -> Series a
mempty :: Series a
$cmempty :: forall a. Series a
$cp1Monoid :: forall a. Semigroup (Series a)
Monoid)

instance Binary a => Binary (Series a) where
    get :: Get (Series a)
get = Seq a -> Series a
forall a. Seq a -> Series a
Series (Seq a -> Series a) -> ([a] -> Seq a) -> [a] -> Series a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Seq a
forall a. [a] -> Seq a
Seq.fromList ([a] -> Series a) -> Get [a] -> Get (Series a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [a]
forall t. Binary t => Get t
get
    {-# INLINE get #-}

    put :: Series a -> Put
put (Series xs :: Seq a
xs) = [a] -> Put
forall t. Binary t => t -> Put
put ([a] -> Put) -> [a] -> Put
forall a b. (a -> b) -> a -> b
$ Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Seq a
xs
    {-# INLINE put #-}

instance Hashable a => Hashable (Series a) where
    hashWithSalt :: Int -> Series a -> Int
hashWithSalt salt :: Int
salt (Series xs :: Seq a
xs) = (Int -> a -> Int) -> Int -> Seq a -> Int
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Int -> a -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt Seq a
xs

instance Traversable Series where
    traverse :: (a -> f b) -> Series a -> f (Series b)
traverse f :: a -> f b
f (Series xs :: Seq a
xs) = Seq b -> Series b
forall a. Seq a -> Series a
Series (Seq b -> Series b) -> f (Seq b) -> f (Series b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Seq a -> f (Seq b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f Seq a
xs

-- | A non-empty 'Series'. 
newtype SeriesNE a = SeriesNE (Seq a)
  deriving stock ((forall x. SeriesNE a -> Rep (SeriesNE a) x)
-> (forall x. Rep (SeriesNE a) x -> SeriesNE a)
-> Generic (SeriesNE a)
forall x. Rep (SeriesNE a) x -> SeriesNE a
forall x. SeriesNE a -> Rep (SeriesNE a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (SeriesNE a) x -> SeriesNE a
forall a x. SeriesNE a -> Rep (SeriesNE a) x
$cto :: forall a x. Rep (SeriesNE a) x -> SeriesNE a
$cfrom :: forall a x. SeriesNE a -> Rep (SeriesNE a) x
Generic, Int -> SeriesNE a -> ShowS
[SeriesNE a] -> ShowS
SeriesNE a -> String
(Int -> SeriesNE a -> ShowS)
-> (SeriesNE a -> String)
-> ([SeriesNE a] -> ShowS)
-> Show (SeriesNE a)
forall a. Show a => Int -> SeriesNE a -> ShowS
forall a. Show a => [SeriesNE a] -> ShowS
forall a. Show a => SeriesNE a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SeriesNE a] -> ShowS
$cshowList :: forall a. Show a => [SeriesNE a] -> ShowS
show :: SeriesNE a -> String
$cshow :: forall a. Show a => SeriesNE a -> String
showsPrec :: Int -> SeriesNE a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> SeriesNE a -> ShowS
Show)
  deriving newtype (SeriesNE a -> SeriesNE a -> Bool
(SeriesNE a -> SeriesNE a -> Bool)
-> (SeriesNE a -> SeriesNE a -> Bool) -> Eq (SeriesNE a)
forall a. Eq a => SeriesNE a -> SeriesNE a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SeriesNE a -> SeriesNE a -> Bool
$c/= :: forall a. Eq a => SeriesNE a -> SeriesNE a -> Bool
== :: SeriesNE a -> SeriesNE a -> Bool
$c== :: forall a. Eq a => SeriesNE a -> SeriesNE a -> Bool
Eq, a -> SeriesNE a -> Bool
SeriesNE m -> m
SeriesNE a -> [a]
SeriesNE a -> Bool
SeriesNE a -> Int
SeriesNE a -> a
SeriesNE a -> a
SeriesNE a -> a
SeriesNE a -> a
(a -> m) -> SeriesNE a -> m
(a -> m) -> SeriesNE a -> m
(a -> b -> b) -> b -> SeriesNE a -> b
(a -> b -> b) -> b -> SeriesNE a -> b
(b -> a -> b) -> b -> SeriesNE a -> b
(b -> a -> b) -> b -> SeriesNE a -> b
(a -> a -> a) -> SeriesNE a -> a
(a -> a -> a) -> SeriesNE a -> a
(forall m. Monoid m => SeriesNE m -> m)
-> (forall m a. Monoid m => (a -> m) -> SeriesNE a -> m)
-> (forall m a. Monoid m => (a -> m) -> SeriesNE a -> m)
-> (forall a b. (a -> b -> b) -> b -> SeriesNE a -> b)
-> (forall a b. (a -> b -> b) -> b -> SeriesNE a -> b)
-> (forall b a. (b -> a -> b) -> b -> SeriesNE a -> b)
-> (forall b a. (b -> a -> b) -> b -> SeriesNE a -> b)
-> (forall a. (a -> a -> a) -> SeriesNE a -> a)
-> (forall a. (a -> a -> a) -> SeriesNE a -> a)
-> (forall a. SeriesNE a -> [a])
-> (forall a. SeriesNE a -> Bool)
-> (forall a. SeriesNE a -> Int)
-> (forall a. Eq a => a -> SeriesNE a -> Bool)
-> (forall a. Ord a => SeriesNE a -> a)
-> (forall a. Ord a => SeriesNE a -> a)
-> (forall a. Num a => SeriesNE a -> a)
-> (forall a. Num a => SeriesNE a -> a)
-> Foldable SeriesNE
forall a. Eq a => a -> SeriesNE a -> Bool
forall a. Num a => SeriesNE a -> a
forall a. Ord a => SeriesNE a -> a
forall m. Monoid m => SeriesNE m -> m
forall a. SeriesNE a -> Bool
forall a. SeriesNE a -> Int
forall a. SeriesNE a -> [a]
forall a. (a -> a -> a) -> SeriesNE a -> a
forall m a. Monoid m => (a -> m) -> SeriesNE a -> m
forall b a. (b -> a -> b) -> b -> SeriesNE a -> b
forall a b. (a -> b -> b) -> b -> SeriesNE a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
product :: SeriesNE a -> a
$cproduct :: forall a. Num a => SeriesNE a -> a
sum :: SeriesNE a -> a
$csum :: forall a. Num a => SeriesNE a -> a
minimum :: SeriesNE a -> a
$cminimum :: forall a. Ord a => SeriesNE a -> a
maximum :: SeriesNE a -> a
$cmaximum :: forall a. Ord a => SeriesNE a -> a
elem :: a -> SeriesNE a -> Bool
$celem :: forall a. Eq a => a -> SeriesNE a -> Bool
length :: SeriesNE a -> Int
$clength :: forall a. SeriesNE a -> Int
null :: SeriesNE a -> Bool
$cnull :: forall a. SeriesNE a -> Bool
toList :: SeriesNE a -> [a]
$ctoList :: forall a. SeriesNE a -> [a]
foldl1 :: (a -> a -> a) -> SeriesNE a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> SeriesNE a -> a
foldr1 :: (a -> a -> a) -> SeriesNE a -> a
$cfoldr1 :: forall a. (a -> a -> a) -> SeriesNE a -> a
foldl' :: (b -> a -> b) -> b -> SeriesNE a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> SeriesNE a -> b
foldl :: (b -> a -> b) -> b -> SeriesNE a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> SeriesNE a -> b
foldr' :: (a -> b -> b) -> b -> SeriesNE a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> SeriesNE a -> b
foldr :: (a -> b -> b) -> b -> SeriesNE a -> b
$cfoldr :: forall a b. (a -> b -> b) -> b -> SeriesNE a -> b
foldMap' :: (a -> m) -> SeriesNE a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> SeriesNE a -> m
foldMap :: (a -> m) -> SeriesNE a -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> SeriesNE a -> m
fold :: SeriesNE m -> m
$cfold :: forall m. Monoid m => SeriesNE m -> m
Foldable, a -> SeriesNE b -> SeriesNE a
(a -> b) -> SeriesNE a -> SeriesNE b
(forall a b. (a -> b) -> SeriesNE a -> SeriesNE b)
-> (forall a b. a -> SeriesNE b -> SeriesNE a) -> Functor SeriesNE
forall a b. a -> SeriesNE b -> SeriesNE a
forall a b. (a -> b) -> SeriesNE a -> SeriesNE b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SeriesNE b -> SeriesNE a
$c<$ :: forall a b. a -> SeriesNE b -> SeriesNE a
fmap :: (a -> b) -> SeriesNE a -> SeriesNE b
$cfmap :: forall a b. (a -> b) -> SeriesNE a -> SeriesNE b
Functor, Functor SeriesNE
a -> SeriesNE a
Functor SeriesNE =>
(forall a. a -> SeriesNE a)
-> (forall a b. SeriesNE (a -> b) -> SeriesNE a -> SeriesNE b)
-> (forall a b c.
    (a -> b -> c) -> SeriesNE a -> SeriesNE b -> SeriesNE c)
-> (forall a b. SeriesNE a -> SeriesNE b -> SeriesNE b)
-> (forall a b. SeriesNE a -> SeriesNE b -> SeriesNE a)
-> Applicative SeriesNE
SeriesNE a -> SeriesNE b -> SeriesNE b
SeriesNE a -> SeriesNE b -> SeriesNE a
SeriesNE (a -> b) -> SeriesNE a -> SeriesNE b
(a -> b -> c) -> SeriesNE a -> SeriesNE b -> SeriesNE c
forall a. a -> SeriesNE a
forall a b. SeriesNE a -> SeriesNE b -> SeriesNE a
forall a b. SeriesNE a -> SeriesNE b -> SeriesNE b
forall a b. SeriesNE (a -> b) -> SeriesNE a -> SeriesNE b
forall a b c.
(a -> b -> c) -> SeriesNE a -> SeriesNE b -> SeriesNE c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: SeriesNE a -> SeriesNE b -> SeriesNE a
$c<* :: forall a b. SeriesNE a -> SeriesNE b -> SeriesNE a
*> :: SeriesNE a -> SeriesNE b -> SeriesNE b
$c*> :: forall a b. SeriesNE a -> SeriesNE b -> SeriesNE b
liftA2 :: (a -> b -> c) -> SeriesNE a -> SeriesNE b -> SeriesNE c
$cliftA2 :: forall a b c.
(a -> b -> c) -> SeriesNE a -> SeriesNE b -> SeriesNE c
<*> :: SeriesNE (a -> b) -> SeriesNE a -> SeriesNE b
$c<*> :: forall a b. SeriesNE (a -> b) -> SeriesNE a -> SeriesNE b
pure :: a -> SeriesNE a
$cpure :: forall a. a -> SeriesNE a
$cp1Applicative :: Functor SeriesNE
Applicative, [SeriesNE a] -> Encoding
[SeriesNE a] -> Value
SeriesNE a -> Encoding
SeriesNE a -> Value
(SeriesNE a -> Value)
-> (SeriesNE a -> Encoding)
-> ([SeriesNE a] -> Value)
-> ([SeriesNE a] -> Encoding)
-> ToJSON (SeriesNE a)
forall a. ToJSON a => [SeriesNE a] -> Encoding
forall a. ToJSON a => [SeriesNE a] -> Value
forall a. ToJSON a => SeriesNE a -> Encoding
forall a. ToJSON a => SeriesNE a -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [SeriesNE a] -> Encoding
$ctoEncodingList :: forall a. ToJSON a => [SeriesNE a] -> Encoding
toJSONList :: [SeriesNE a] -> Value
$ctoJSONList :: forall a. ToJSON a => [SeriesNE a] -> Value
toEncoding :: SeriesNE a -> Encoding
$ctoEncoding :: forall a. ToJSON a => SeriesNE a -> Encoding
toJSON :: SeriesNE a -> Value
$ctoJSON :: forall a. ToJSON a => SeriesNE a -> Value
ToJSON, SeriesNE a -> ()
(SeriesNE a -> ()) -> NFData (SeriesNE a)
forall a. NFData a => SeriesNE a -> ()
forall a. (a -> ()) -> NFData a
rnf :: SeriesNE a -> ()
$crnf :: forall a. NFData a => SeriesNE a -> ()
NFData, b -> SeriesNE a -> SeriesNE a
NonEmpty (SeriesNE a) -> SeriesNE a
SeriesNE a -> SeriesNE a -> SeriesNE a
(SeriesNE a -> SeriesNE a -> SeriesNE a)
-> (NonEmpty (SeriesNE a) -> SeriesNE a)
-> (forall b. Integral b => b -> SeriesNE a -> SeriesNE a)
-> Semigroup (SeriesNE a)
forall b. Integral b => b -> SeriesNE a -> SeriesNE a
forall a. NonEmpty (SeriesNE a) -> SeriesNE a
forall a. SeriesNE a -> SeriesNE a -> SeriesNE a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
forall a b. Integral b => b -> SeriesNE a -> SeriesNE a
stimes :: b -> SeriesNE a -> SeriesNE a
$cstimes :: forall a b. Integral b => b -> SeriesNE a -> SeriesNE a
sconcat :: NonEmpty (SeriesNE a) -> SeriesNE a
$csconcat :: forall a. NonEmpty (SeriesNE a) -> SeriesNE a
<> :: SeriesNE a -> SeriesNE a -> SeriesNE a
$c<> :: forall a. SeriesNE a -> SeriesNE a -> SeriesNE a
Semigroup)

instance Binary a => Binary (SeriesNE a) where
    get :: Get (SeriesNE a)
get =
        SeriesNE a
-> (SeriesNE a -> SeriesNE a) -> Maybe (SeriesNE a) -> SeriesNE a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> SeriesNE a
forall a. HasCallStack => String -> a
error "SeriesNE must be non-empty") SeriesNE a -> SeriesNE a
forall a. a -> a
id
            (Maybe (SeriesNE a) -> SeriesNE a)
-> ([a] -> Maybe (SeriesNE a)) -> [a] -> SeriesNE a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   Seq a -> Maybe (SeriesNE a)
forall a. Seq a -> Maybe (SeriesNE a)
fromSeqNE
            (Seq a -> Maybe (SeriesNE a))
-> ([a] -> Seq a) -> [a] -> Maybe (SeriesNE a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.   [a] -> Seq a
forall a. [a] -> Seq a
Seq.fromList
            ([a] -> SeriesNE a) -> Get [a] -> Get (SeriesNE a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get [a]
forall t. Binary t => Get t
get
    {-# INLINE get #-}

    put :: SeriesNE a -> Put
put (SeriesNE xs :: Seq a
xs) = [a] -> Put
forall t. Binary t => t -> Put
put ([a] -> Put) -> [a] -> Put
forall a b. (a -> b) -> a -> b
$ Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Seq a
xs
    {-# INLINE put #-}

instance FromJSON a => FromJSON (SeriesNE a) where
    parseJSON :: Value -> Parser (SeriesNE a)
parseJSON value :: Value
value = do
        Seq a
inner <- Value -> Parser (Seq a)
forall a. FromJSON a => Value -> Parser a
parseJSON Value
value
        Bool -> Parser ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq a
inner)
        SeriesNE a -> Parser (SeriesNE a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SeriesNE a -> Parser (SeriesNE a))
-> SeriesNE a -> Parser (SeriesNE a)
forall a b. (a -> b) -> a -> b
$ Seq a -> SeriesNE a
forall a. Seq a -> SeriesNE a
SeriesNE Seq a
inner

instance Hashable a => Hashable (SeriesNE a) where
    hashWithSalt :: Int -> SeriesNE a -> Int
hashWithSalt salt :: Int
salt (SeriesNE xs :: Seq a
xs) = (Int -> a -> Int) -> Int -> Seq a -> Int
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' Int -> a -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
salt Seq a
xs

instance Traversable SeriesNE where
    traverse :: (a -> f b) -> SeriesNE a -> f (SeriesNE b)
traverse f :: a -> f b
f (SeriesNE xs :: Seq a
xs) = Seq b -> SeriesNE b
forall a. Seq a -> SeriesNE a
SeriesNE (Seq b -> SeriesNE b) -> f (Seq b) -> f (SeriesNE b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> f b) -> Seq a -> f (Seq b)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse a -> f b
f Seq a
xs

-- | Given a function which operates on a 'Seq', return a function which
-- operates on a 'Series'.
asSeq :: Functor f => (Seq a -> f (Seq b)) -> Series a -> f (Series b)
asSeq :: (Seq a -> f (Seq b)) -> Series a -> f (Series b)
asSeq f :: Seq a -> f (Seq b)
f (Series s :: Seq a
s) = Seq b -> Series b
forall a. Seq a -> Series a
Series (Seq b -> Series b) -> f (Seq b) -> f (Series b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Seq a -> f (Seq b)
f Seq a
s

-- | Convert a 'Seq' into a 'Series'.
fromSeq :: Seq a -> Series a
fromSeq :: Seq a -> Series a
fromSeq = Seq a -> Series a
forall a. Seq a -> Series a
Series

-- | Convert a 'Series' into a 'Seq'.
toSeq :: Series a -> Seq a
toSeq :: Series a -> Seq a
toSeq (Series s :: Seq a
s) = Seq a
s

-- | Convert a non-empty 'Seq' into a 'SeriesNE'.
fromSeqNE :: Seq a -> Maybe (SeriesNE a)
fromSeqNE :: Seq a -> Maybe (SeriesNE a)
fromSeqNE s :: Seq a
s | Seq a -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null Seq a
s    = Maybe (SeriesNE a)
forall a. Maybe a
Nothing
fromSeqNE s :: Seq a
s | Bool
otherwise = SeriesNE a -> Maybe (SeriesNE a)
forall a. a -> Maybe a
Just (Seq a -> SeriesNE a
forall a. Seq a -> SeriesNE a
SeriesNE Seq a
s)

-- | Convert a 'SeriesNE' into a 'Seq'. The returned 'Seq' is guarenteed to
-- always contain at least one element.
toSeqNE :: SeriesNE a -> Seq a
toSeqNE :: SeriesNE a -> Seq a
toSeqNE (SeriesNE a :: Seq a
a) = Seq a
a