module Data.Sequential (Sequential(..)) where



import           Data.Foldable
import           Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import           Data.Vector   (Vector)
import qualified Data.Vector   as V



-- | Data structures that are sequential, so that we can use them to build
-- input-polymorphic functions. This is quite handy, as a lot of functions require
-- special input, and it’s cumbersome to juggle 'V.fromList' and 'toList' all over
-- the place.
--
-- It’s a good idea to add the ideal type as part of the type signature,
--
-- @
-- -- This will work best with a vector input, but can convert everything else as well
-- doStuff :: 'Sequential' vector => vector a -> [a]
-- @
class Foldable f => Sequential f where
    toVector :: f a -> Vector a
    toSeq :: f a -> Seq a

instance Sequential [] where
    {-# INLINE toVector #-}
    toVector :: forall a. [a] -> Vector a
toVector = [a] -> Vector a
forall a. [a] -> Vector a
V.fromList
    {-# INLINE toSeq #-}
    toSeq :: forall a. [a] -> Seq a
toSeq = [a] -> Seq a
forall a. [a] -> Seq a
Seq.fromList

instance Sequential Vector where
    {-# INLINE toVector #-}
    toVector :: forall a. Vector a -> Vector a
toVector = Vector a -> Vector a
forall a. a -> a
id
    {-# INLINE toSeq #-}
    toSeq :: forall a. Vector a -> Seq a
toSeq = [a] -> Seq a
forall a. [a] -> Seq a
Seq.fromList ([a] -> Seq a) -> (Vector a -> [a]) -> Vector a -> Seq a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector a -> [a]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

instance Sequential Seq where
    {-# INLINE toVector #-}
    toVector :: forall a. Seq a -> Vector a
toVector = [a] -> Vector a
forall a. [a] -> Vector a
V.fromList ([a] -> Vector a) -> (Seq a -> [a]) -> Seq a -> Vector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Seq a -> [a]
forall a. Seq a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
    {-# INLINE toSeq #-}
    toSeq :: forall a. Seq a -> Seq a
toSeq = Seq a -> Seq a
forall a. a -> a
id