add implicit take/drop

This commit is contained in:
rnhmjoj 2016-11-16 23:11:07 +01:00
parent f8365ca381
commit c1351e2dff
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C

View File

@ -280,18 +280,26 @@ product ∷ Num a ⇒ Vec a n → a
product = foldr (T.*) 0 product = foldr (T.*) 0
-- | 'take' @n@ applied to a 'Vec', returns the first @n@ element -- | 'take' returns the first @n@ element of the vector
-- of the vector take a n m. SingI n Vec a m Vec a n
take S n Vec a m Vec a n take = take' (sing S n)
take SZ _ = Nil
take (SS n) (x :- xs) = x :- take n xs
-- | 'drop' @n@ applied to a 'Vec', returns every element of -- | Variant of 'take' with an explicit argument.
-- the vector but the first @n@ take' S n Vec a m Vec a n
drop S n Vec a m Vec a (m :- n) take' SZ _ = Nil
drop SZ x = x take' (SS n) (x :- xs) = x :- take' n xs
drop (SS n) (x :- xs) = drop n xs
-- | 'drop' returns every element of the vector but the first @n@
drop a n m. SingI n Vec a m Vec a (m :- n)
drop = drop' (sing S n)
-- | Variant of 'drop' with an explicit argument.
drop' S n Vec a m Vec a (m :- n)
drop' SZ x = x
drop' (SS n) (x :- xs) = drop' n xs