add more folds

This commit is contained in:
rnhmjoj 2016-11-15 22:57:19 +01:00
parent ee653f3822
commit 8abcc2f1f1
No known key found for this signature in database
GPG Key ID: 362BB82B7E496B7C

View File

@ -22,7 +22,7 @@ import Data.Nat
import Data.Bool
import Data.Char (Char)
import Data.Function ((), ($))
import Data.TypeClass (Show, IsList, IsString, Eq)
import Data.TypeClass (Show, IsList, IsString, Eq, Num)
import Data.Singletons (SingI, sing)
import qualified Data.TypeClass as T
@ -196,6 +196,8 @@ foldr₁ f (x :- Nil) = x
foldr f (x :- y :- ys) = f x (foldr f (y :- ys))
-- * Special folds
-- | The concatenation of all the elements of a vector of vectors.
concat Vec (Vec a n) m Vec a (m :× n)
concat Nil = Nil
@ -207,7 +209,7 @@ concat (xs :- xss) = gcastWith proof (xs ⧺ concat xss)
-- | Map a function over all the elements of a vector and concatenate
-- the resulting vectos.
-- the resulting vectors.
concatMap (a Vec b n) Vec a m Vec b (m :× n)
concatMap f = concat map f
@ -219,6 +221,38 @@ replicate SZ _ = Nil
replicate (SS n) a = a :- replicate n a
-- | 'and' returns the conjunction of a container of bools.
and Vec 𝔹 (S n) 𝔹
and = foldr ()
-- | 'or' returns the disjunction of a container of bools.
or Vec 𝔹 (S n) 𝔹
or = foldr ()
-- | Determines whether any element of the structure satisfies the predicate.
any (a 𝔹) Vec a n 𝔹
any _ Nil = T
any p xs = or (map p xs)
-- | Determines whether all elements of the structure satisfy the predicate.
all (a 𝔹) Vec a n 𝔹
all _ Nil = T
all p xs = and (map p xs)
-- | The 'sum' function computes the sum of the numbers of a vector.
sum Num a Vec a n a
sum = foldr (T.+) 0
-- | The 'product' function computes the product of the numbers of a vector.
product Num a Vec a n a
product = foldr (T.*) 0
-- | 'take' @n@ applied to a 'Vec', returns the first @n@ element
-- of the vector
take S n Vec a m Vec a n