vec-0.2: Vec: length-indexed (sized) list

Safe HaskellNone
LanguageHaskell2010

Data.Vec.Pull

Contents

Description

Pull/representable Vec n a = Fin n -> a.

The module tries to have same API as Data.Vec.Lazy, missing bits: withDict, toPull, fromPull, traverse (and variants), (++), concat and split.

Synopsis

Documentation

newtype Vec n a Source #

Easily fuseable Vec.

It unpurpose don't have bad (fusion-wise) instances, like Traversable. Generally, there aren't functions which would be bad consumers or bad producers.

Constructors

Vec 

Fields

Instances
Monad (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

(>>=) :: Vec n a -> (a -> Vec n b) -> Vec n b #

(>>) :: Vec n a -> Vec n b -> Vec n b #

return :: a -> Vec n a #

fail :: String -> Vec n a #

Functor (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

fmap :: (a -> b) -> Vec n a -> Vec n b #

(<$) :: a -> Vec n b -> Vec n a #

Applicative (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

pure :: a -> Vec n a #

(<*>) :: Vec n (a -> b) -> Vec n a -> Vec n b #

liftA2 :: (a -> b -> c) -> Vec n a -> Vec n b -> Vec n c #

(*>) :: Vec n a -> Vec n b -> Vec n b #

(<*) :: Vec n a -> Vec n b -> Vec n a #

SNatI n => Foldable (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

fold :: Monoid m => Vec n m -> m #

foldMap :: Monoid m => (a -> m) -> Vec n a -> m #

foldr :: (a -> b -> b) -> b -> Vec n a -> b #

foldr' :: (a -> b -> b) -> b -> Vec n a -> b #

foldl :: (b -> a -> b) -> b -> Vec n a -> b #

foldl' :: (b -> a -> b) -> b -> Vec n a -> b #

foldr1 :: (a -> a -> a) -> Vec n a -> a #

foldl1 :: (a -> a -> a) -> Vec n a -> a #

toList :: Vec n a -> [a] #

null :: Vec n a -> Bool #

length :: Vec n a -> Int #

elem :: Eq a => a -> Vec n a -> Bool #

maximum :: Ord a => Vec n a -> a #

minimum :: Ord a => Vec n a -> a #

sum :: Num a => Vec n a -> a #

product :: Num a => Vec n a -> a #

Distributive (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

distribute :: Functor f => f (Vec n a) -> Vec n (f a) #

collect :: Functor f => (a -> Vec n b) -> f a -> Vec n (f b) #

distributeM :: Monad m => m (Vec n a) -> Vec n (m a) #

collectM :: Monad m => (a -> Vec n b) -> m a -> Vec n (m b) #

Representable (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Associated Types

type Rep (Vec n) :: Type #

Methods

tabulate :: (Rep (Vec n) -> a) -> Vec n a #

index :: Vec n a -> Rep (Vec n) -> a #

Apply (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

(<.>) :: Vec n (a -> b) -> Vec n a -> Vec n b #

(.>) :: Vec n a -> Vec n b -> Vec n b #

(<.) :: Vec n a -> Vec n b -> Vec n a #

liftF2 :: (a -> b -> c) -> Vec n a -> Vec n b -> Vec n c #

Bind (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

(>>-) :: Vec n a -> (a -> Vec n b) -> Vec n b #

join :: Vec n (Vec n a) -> Vec n a #

FunctorWithIndex (Fin n) (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

imap :: (Fin n -> a -> b) -> Vec n a -> Vec n b #

imapped :: IndexedSetter (Fin n) (Vec n a) (Vec n b) a b #

SNatI n => FoldableWithIndex (Fin n) (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

ifoldMap :: Monoid m => (Fin n -> a -> m) -> Vec n a -> m #

ifolded :: IndexedFold (Fin n) (Vec n a) a #

ifoldr :: (Fin n -> a -> b -> b) -> b -> Vec n a -> b #

ifoldl :: (Fin n -> b -> a -> b) -> b -> Vec n a -> b #

ifoldr' :: (Fin n -> a -> b -> b) -> b -> Vec n a -> b #

ifoldl' :: (Fin n -> b -> a -> b) -> b -> Vec n a -> b #

(Eq a, SNatI n) => Eq (Vec n a) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

(==) :: Vec n a -> Vec n a -> Bool #

(/=) :: Vec n a -> Vec n a -> Bool #

Semigroup a => Semigroup (Vec n a) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

(<>) :: Vec n a -> Vec n a -> Vec n a #

sconcat :: NonEmpty (Vec n a) -> Vec n a #

stimes :: Integral b => b -> Vec n a -> Vec n a #

Monoid a => Monoid (Vec n a) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

mempty :: Vec n a #

mappend :: Vec n a -> Vec n a -> Vec n a #

mconcat :: [Vec n a] -> Vec n a #

type Rep (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

type Rep (Vec n) = Fin n

Construction

empty :: Vec Z a Source #

Empty Vec.

singleton :: a -> Vec (S Z) a Source #

Vec with exactly one element.

>>> L.fromPull $ singleton True
True ::: VNil

Conversions

toList :: SNatI n => Vec n a -> [a] Source #

Convert Vec to list.

fromList :: SNatI n => [a] -> Maybe (Vec n a) Source #

Convert list [a] to Vec n a. Returns Nothing if lengths don't match exactly.

>>> L.fromPull <$> fromList "foo" :: Maybe (L.Vec N.Nat3 Char)
Just ('f' ::: 'o' ::: 'o' ::: VNil)
>>> L.fromPull <$> fromList "quux" :: Maybe (L.Vec N.Nat3 Char)
Nothing
>>> L.fromPull <$> fromList "xy" :: Maybe (L.Vec N.Nat3 Char)
Nothing

_Vec :: SNatI n => Prism' [a] (Vec n a) Source #

Prism from list.

fromListPrefix :: SNatI n => [a] -> Maybe (Vec n a) Source #

Convert list [a] to Vec n a. Returns Nothing if input list is too short.

>>> L.fromPull <$> fromListPrefix "foo" :: Maybe (L.Vec N.Nat3 Char)
Just ('f' ::: 'o' ::: 'o' ::: VNil)
>>> L.fromPull <$> fromListPrefix "quux" :: Maybe (L.Vec N.Nat3 Char)
Just ('q' ::: 'u' ::: 'u' ::: VNil)
>>> L.fromPull <$> fromListPrefix "xy" :: Maybe (L.Vec N.Nat3 Char)
Nothing

reifyList :: [a] -> (forall n. InlineInduction n => Vec n a -> r) -> r Source #

Reify any list [a] to Vec n a.

>>> reifyList "foo" length
3

Indexing

(!) :: Vec n a -> Fin n -> a Source #

Indexing.

ix :: Fin n -> Lens' (Vec n a) a Source #

Index lens.

>>> ('a' L.::: 'b' L.::: 'c' L.::: L.VNil) ^. L._Pull . ix (FS FZ)
'b'
>>> ('a' L.::: 'b' L.::: 'c' L.::: L.VNil) & L._Pull . ix (FS FZ) .~ 'x'
'a' ::: 'x' ::: 'c' ::: VNil

_Cons :: Iso (Vec (S n) a) (Vec (S n) b) (a, Vec n a) (b, Vec n b) Source #

Match on non-empty Vec.

Note: lens _Cons is a Prism. In fact, Vec n a cannot have an instance of Cons as types don't match.

_head :: Lens' (Vec (S n) a) a Source #

Head lens. Note: lens _head is a Traversal'.

>>> ('a' L.::: 'b' L.::: 'c' L.::: L.VNil) ^. L._Pull . _head
'a'
>>> ('a' L.::: 'b' L.::: 'c' L.::: L.VNil) & L._Pull . _head .~ 'x'
'x' ::: 'b' ::: 'c' ::: VNil

_tail :: Lens' (Vec (S n) a) (Vec n a) Source #

Head lens. Note: lens _head is a Traversal'.

head :: Vec (S n) a -> a Source #

The first element of a Vec.

tail :: Vec (S n) a -> Vec n a Source #

The elements after the head of a Vec.

Folds

foldMap :: (Monoid m, SNatI n) => (a -> m) -> Vec n a -> m Source #

foldMap1 :: (Semigroup s, SNatI n) => (a -> s) -> Vec (S n) a -> s Source #

ifoldMap :: (Monoid m, SNatI n) => (Fin n -> a -> m) -> Vec n a -> m Source #

ifoldMap1 :: (Semigroup s, SNatI n) => (Fin (S n) -> a -> s) -> Vec (S n) a -> s Source #

There is no type-class for this :(

foldr :: SNatI n => (a -> b -> b) -> b -> Vec n a -> b Source #

Right fold.

ifoldr :: SNatI n => (Fin n -> a -> b -> b) -> b -> Vec n a -> b Source #

Right fold with an index.

foldl' :: SNatI n => (b -> a -> b) -> b -> Vec n a -> b Source #

Strict left fold.

Special folds

length :: forall n a. SNatI n => Vec n a -> Int Source #

Yield the length of a Vec.

null :: forall n a. SNatI n => Vec n a -> Bool Source #

Test whether a Vec is empty.

sum :: (Num a, SNatI n) => Vec n a -> a Source #

Strict sum.

product :: (Num a, SNatI n) => Vec n a -> a Source #

Strict product.

Mapping

map :: (a -> b) -> Vec n a -> Vec n b Source #

>>> over L._Pull (map not) (True L.::: False L.::: L.VNil)
False ::: True ::: VNil

imap :: (Fin n -> a -> b) -> Vec n a -> Vec n b Source #

>>> over L._Pull (imap (,)) ('a' L.::: 'b' L.::: 'c' L.::: L.VNil)
(0,'a') ::: (1,'b') ::: (2,'c') ::: VNil

Zipping

zipWith :: (a -> b -> c) -> Vec n a -> Vec n b -> Vec n c Source #

Zip two Vecs with a function.

izipWith :: (Fin n -> a -> b -> c) -> Vec n a -> Vec n b -> Vec n c Source #

Zip two Vecs. with a function that also takes the elements' indices.

Monadic

bind :: Vec n a -> (a -> Vec n b) -> Vec n b Source #

Monadic bind.

join :: Vec n (Vec n a) -> Vec n a Source #

Monadic join.

Universe

universe :: SNatI n => Vec n (Fin n) Source #

Get all Fin n in a Vec n.

>>> L.fromPull (universe :: Vec N.Nat3 (Fin N.Nat3))
0 ::: 1 ::: 2 ::: VNil