module Resource.Collection
  ( enumerate
  , size
  , toVector
  , toVectorStorable

  , Generic1
  , Generically1(..)
  ) where

import RIO

import Data.List qualified as List
import Data.Traversable (mapAccumL)
import GHC.Generics (Generic1, Generically1(..))
import RIO.Vector qualified as Vector
import RIO.Vector.Storable qualified as VectorStorable

{-# INLINE size #-}
size :: (Foldable t, Num size) => t a -> size
size :: forall (t :: * -> *) size a. (Foldable t, Num size) => t a -> size
size = [a] -> size
forall i a. Num i => [a] -> i
List.genericLength ([a] -> size) -> (t a -> [a]) -> t a -> size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall a. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

enumerate :: (Traversable t, Num ix) => t a -> t (ix, a)
enumerate :: forall (t :: * -> *) ix a.
(Traversable t, Num ix) =>
t a -> t (ix, a)
enumerate = (ix, t (ix, a)) -> t (ix, a)
forall a b. (a, b) -> b
snd ((ix, t (ix, a)) -> t (ix, a))
-> (t a -> (ix, t (ix, a))) -> t a -> t (ix, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ix -> a -> (ix, (ix, a))) -> ix -> t a -> (ix, t (ix, a))
forall (t :: * -> *) s a b.
Traversable t =>
(s -> a -> (s, b)) -> s -> t a -> (s, t b)
mapAccumL ix -> a -> (ix, (ix, a))
forall {a} {b}. Num a => a -> b -> (a, (a, b))
f ix
0
  where
    f :: a -> b -> (a, (a, b))
f a
a b
b = (a
a a -> a -> a
forall a. Num a => a -> a -> a
+ a
1, (a
a, b
b))

{-# INLINE toVector #-}
toVector :: Foldable collection => collection a -> Vector a
toVector :: forall (collection :: * -> *) a.
Foldable collection =>
collection a -> Vector a
toVector = [a] -> Vector a
forall (v :: * -> *) a. Vector v a => [a] -> v a
Vector.fromList ([a] -> Vector a)
-> (collection a -> [a]) -> collection a -> Vector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. collection a -> [a]
forall a. collection a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

{-# INLINE toVectorStorable #-}
toVectorStorable
  :: (Foldable collection, Storable a)
  => collection a
  -> VectorStorable.Vector a
toVectorStorable :: forall (collection :: * -> *) a.
(Foldable collection, Storable a) =>
collection a -> Vector a
toVectorStorable = [a] -> Vector a
forall a. Storable a => [a] -> Vector a
VectorStorable.fromList ([a] -> Vector a)
-> (collection a -> [a]) -> collection a -> Vector a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. collection a -> [a]
forall a. collection a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList

data Example a = Example
  { forall a. Example a -> a
one :: a
  , forall a. Example a -> a
two :: a
  -- , nay :: Bool
  , forall a. Example a -> [Bool]
huh :: [Bool]
  }
  deriving stock (Example a -> Example a -> Bool
(Example a -> Example a -> Bool)
-> (Example a -> Example a -> Bool) -> Eq (Example a)
forall a. Eq a => Example a -> Example a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Example a -> Example a -> Bool
== :: Example a -> Example a -> Bool
$c/= :: forall a. Eq a => Example a -> Example a -> Bool
/= :: Example a -> Example a -> Bool
Eq, Eq (Example a)
Eq (Example a)
-> (Example a -> Example a -> Ordering)
-> (Example a -> Example a -> Bool)
-> (Example a -> Example a -> Bool)
-> (Example a -> Example a -> Bool)
-> (Example a -> Example a -> Bool)
-> (Example a -> Example a -> Example a)
-> (Example a -> Example a -> Example a)
-> Ord (Example a)
Example a -> Example a -> Bool
Example a -> Example a -> Ordering
Example a -> Example a -> Example a
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
forall {a}. Ord a => Eq (Example a)
forall a. Ord a => Example a -> Example a -> Bool
forall a. Ord a => Example a -> Example a -> Ordering
forall a. Ord a => Example a -> Example a -> Example a
$ccompare :: forall a. Ord a => Example a -> Example a -> Ordering
compare :: Example a -> Example a -> Ordering
$c< :: forall a. Ord a => Example a -> Example a -> Bool
< :: Example a -> Example a -> Bool
$c<= :: forall a. Ord a => Example a -> Example a -> Bool
<= :: Example a -> Example a -> Bool
$c> :: forall a. Ord a => Example a -> Example a -> Bool
> :: Example a -> Example a -> Bool
$c>= :: forall a. Ord a => Example a -> Example a -> Bool
>= :: Example a -> Example a -> Bool
$cmax :: forall a. Ord a => Example a -> Example a -> Example a
max :: Example a -> Example a -> Example a
$cmin :: forall a. Ord a => Example a -> Example a -> Example a
min :: Example a -> Example a -> Example a
Ord, Int -> Example a -> ShowS
[Example a] -> ShowS
Example a -> String
(Int -> Example a -> ShowS)
-> (Example a -> String)
-> ([Example a] -> ShowS)
-> Show (Example a)
forall a. Show a => Int -> Example a -> ShowS
forall a. Show a => [Example a] -> ShowS
forall a. Show a => Example a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> Example a -> ShowS
showsPrec :: Int -> Example a -> ShowS
$cshow :: forall a. Show a => Example a -> String
show :: Example a -> String
$cshowList :: forall a. Show a => [Example a] -> ShowS
showList :: [Example a] -> ShowS
Show, (forall a b. (a -> b) -> Example a -> Example b)
-> (forall a b. a -> Example b -> Example a) -> Functor Example
forall a b. a -> Example b -> Example a
forall a b. (a -> b) -> Example a -> Example b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> Example a -> Example b
fmap :: forall a b. (a -> b) -> Example a -> Example b
$c<$ :: forall a b. a -> Example b -> Example a
<$ :: forall a b. a -> Example b -> Example a
Functor, (forall m. Monoid m => Example m -> m)
-> (forall m a. Monoid m => (a -> m) -> Example a -> m)
-> (forall m a. Monoid m => (a -> m) -> Example a -> m)
-> (forall a b. (a -> b -> b) -> b -> Example a -> b)
-> (forall a b. (a -> b -> b) -> b -> Example a -> b)
-> (forall b a. (b -> a -> b) -> b -> Example a -> b)
-> (forall b a. (b -> a -> b) -> b -> Example a -> b)
-> (forall a. (a -> a -> a) -> Example a -> a)
-> (forall a. (a -> a -> a) -> Example a -> a)
-> (forall a. Example a -> [a])
-> (forall a. Example a -> Bool)
-> (forall a. Example a -> Int)
-> (forall a. Eq a => a -> Example a -> Bool)
-> (forall a. Ord a => Example a -> a)
-> (forall a. Ord a => Example a -> a)
-> (forall a. Num a => Example a -> a)
-> (forall a. Num a => Example a -> a)
-> Foldable Example
forall a. Eq a => a -> Example a -> Bool
forall a. Num a => Example a -> a
forall a. Ord a => Example a -> a
forall m. Monoid m => Example m -> m
forall a. Example a -> Bool
forall a. Example a -> Int
forall a. Example a -> [a]
forall a. (a -> a -> a) -> Example a -> a
forall m a. Monoid m => (a -> m) -> Example a -> m
forall b a. (b -> a -> b) -> b -> Example a -> b
forall a b. (a -> b -> b) -> b -> Example 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
$cfold :: forall m. Monoid m => Example m -> m
fold :: forall m. Monoid m => Example m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> Example a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Example a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> Example a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> Example a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> Example a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Example a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> Example a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Example a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> Example a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Example a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> Example a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> Example a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> Example a -> a
foldr1 :: forall a. (a -> a -> a) -> Example a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> Example a -> a
foldl1 :: forall a. (a -> a -> a) -> Example a -> a
$ctoList :: forall a. Example a -> [a]
toList :: forall a. Example a -> [a]
$cnull :: forall a. Example a -> Bool
null :: forall a. Example a -> Bool
$clength :: forall a. Example a -> Int
length :: forall a. Example a -> Int
$celem :: forall a. Eq a => a -> Example a -> Bool
elem :: forall a. Eq a => a -> Example a -> Bool
$cmaximum :: forall a. Ord a => Example a -> a
maximum :: forall a. Ord a => Example a -> a
$cminimum :: forall a. Ord a => Example a -> a
minimum :: forall a. Ord a => Example a -> a
$csum :: forall a. Num a => Example a -> a
sum :: forall a. Num a => Example a -> a
$cproduct :: forall a. Num a => Example a -> a
product :: forall a. Num a => Example a -> a
Foldable, Functor Example
Foldable Example
Functor Example
-> Foldable Example
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> Example a -> f (Example b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    Example (f a) -> f (Example a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> Example a -> m (Example b))
-> (forall (m :: * -> *) a.
    Monad m =>
    Example (m a) -> m (Example a))
-> Traversable Example
forall (t :: * -> *).
Functor t
-> Foldable t
-> (forall (f :: * -> *) a b.
    Applicative f =>
    (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a. Monad m => Example (m a) -> m (Example a)
forall (f :: * -> *) a.
Applicative f =>
Example (f a) -> f (Example a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Example a -> m (Example b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Example a -> f (Example b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Example a -> f (Example b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Example a -> f (Example b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
Example (f a) -> f (Example a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
Example (f a) -> f (Example a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Example a -> m (Example b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Example a -> m (Example b)
$csequence :: forall (m :: * -> *) a. Monad m => Example (m a) -> m (Example a)
sequence :: forall (m :: * -> *) a. Monad m => Example (m a) -> m (Example a)
Traversable, (forall a. Example a -> Rep1 Example a)
-> (forall a. Rep1 Example a -> Example a) -> Generic1 Example
forall a. Rep1 Example a -> Example a
forall a. Example a -> Rep1 Example a
forall k (f :: k -> *).
(forall (a :: k). f a -> Rep1 f a)
-> (forall (a :: k). Rep1 f a -> f a) -> Generic1 f
$cfrom1 :: forall a. Example a -> Rep1 Example a
from1 :: forall a. Example a -> Rep1 Example a
$cto1 :: forall a. Rep1 Example a -> Example a
to1 :: forall a. Rep1 Example a -> Example a
Generic1)
  deriving Functor Example
Functor Example
-> (forall a. a -> Example a)
-> (forall a b. Example (a -> b) -> Example a -> Example b)
-> (forall a b c.
    (a -> b -> c) -> Example a -> Example b -> Example c)
-> (forall a b. Example a -> Example b -> Example b)
-> (forall a b. Example a -> Example b -> Example a)
-> Applicative Example
forall a. a -> Example a
forall a b. Example a -> Example b -> Example a
forall a b. Example a -> Example b -> Example b
forall a b. Example (a -> b) -> Example a -> Example b
forall a b c. (a -> b -> c) -> Example a -> Example b -> Example 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
$cpure :: forall a. a -> Example a
pure :: forall a. a -> Example a
$c<*> :: forall a b. Example (a -> b) -> Example a -> Example b
<*> :: forall a b. Example (a -> b) -> Example a -> Example b
$cliftA2 :: forall a b c. (a -> b -> c) -> Example a -> Example b -> Example c
liftA2 :: forall a b c. (a -> b -> c) -> Example a -> Example b -> Example c
$c*> :: forall a b. Example a -> Example b -> Example b
*> :: forall a b. Example a -> Example b -> Example b
$c<* :: forall a b. Example a -> Example b -> Example a
<* :: forall a b. Example a -> Example b -> Example a
Applicative via Generically1 Example

_example :: Example (Char, Int)
_example :: Example (Char, Int)
_example = (,) (Char -> Int -> (Char, Int))
-> Example Char -> Example (Int -> (Char, Int))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> Example Char
forall a. a -> Example a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Char
'!' Example (Int -> (Char, Int)) -> Example Int -> Example (Char, Int)
forall a b. Example (a -> b) -> Example a -> Example b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> Example Int
forall a. a -> Example a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
2