vec-0.5.1: Vec: length-indexed (sized) list
Safe HaskellSafe
LanguageHaskell2010

Data.Vec.Pull

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 on purpose doesn'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

Instances details
Representable (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Associated Types

type Rep (Vec n) #

Methods

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

index :: Vec n a -> Rep (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 #

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 #

(SNatI m, n ~ 'S m) => Foldable1 (Vec n) Source # 
Instance details

Defined in Data.Vec.Pull

Methods

fold1 :: Semigroup m => Vec n m -> m #

foldMap1 :: Semigroup m => (a -> m) -> Vec n a -> m #

foldMap1' :: Semigroup m => (a -> m) -> Vec n a -> m #

toNonEmpty :: Vec n a -> NonEmpty a #

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

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

head :: Vec n a -> a #

last :: Vec n a -> a #

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

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

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

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

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 #

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 #

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 #

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) #

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 #

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

Since: 0.4

Instance details

Defined in Data.Vec.Pull

Methods

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

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

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 #

FunctorWithIndex (Fin n) (Vec n) Source #

Since: 0.4

Instance details

Defined in Data.Vec.Pull

Methods

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

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 #

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 #

n ~ 'Z => Boring (Vec n a) Source #

Since: 0.4.1

Instance details

Defined in Data.Vec.Pull

Methods

boring :: Vec n a #

(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 #

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.

toNonEmpty :: SNatI n => Vec ('S n) a -> NonEmpty a Source #

Convert Vec to NonEmpty.

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

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. SNatI 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.

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

cons :: a -> Vec n a -> Vec ('S n) a Source #

Cons an element in front of a Vec.

snoc :: forall a n. SNatI n => Vec n a -> a -> Vec ('S n) a Source #

Add a single element at the end of a Vec.

Since: 0.2.1

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

The first element of a Vec.

last :: forall n a. SNatI n => Vec ('S n) a -> a Source #

The last element of a Vec.

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

The elements after the head of a Vec.

init :: forall n a. SNatI n => Vec ('S n) a -> Vec n a Source #

The elements before the last of a Vec.

Reverse

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

Reverse Vec.

Since: 0.2.1

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 #

>>> L.fromPull $ map not $ L.toPull $ True L.::: False L.::: L.VNil
False ::: True ::: VNil

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

>>> L.fromPull $ imap (,) $ L.toPull $ '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.

repeat :: x -> Vec n x Source #

Repeat value

Since: 0.2.1

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