Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Fcf.Class.Foldable
Description
Foldable types.
A minimal implementation of this interface is given by either FoldMap
or
Foldr
, but a default still needs to be given explicitly for the other.
data MyType a = ... {- Some custom Foldable type -} -- Method 1: Implement Foldr, default FoldMap. type instanceEval
(Foldr
f y xs) = ... {- Explicit implementation -} type instanceEval
(FoldMap
f xs) =FoldMapDefault_
f xs {- Default -} -- Method 2: Implement FoldMap, default Foldr. type instanceEval
(FoldMap
f xs) = ... {- Explicit implementation -} type instanceEval
(Foldr
f y xs) =FoldrDefault_
f y xs {- Default -}
Synopsis
- data Foldr (c :: a -> b -> Exp b) (d :: b) (e :: t a) (f :: b)
- data FoldMap (b :: a -> Exp m) (c :: t a) (d :: m)
- type FoldMapDefault_ (f :: a -> Exp k) (xs :: t a) = Eval (Foldr (Bicomap f (Pure :: k -> k -> Type) ((.<>) :: k -> k -> k -> Type)) (MEmpty :: k) xs)
- type FoldrDefault_ (f :: a -> k -> Exp k) (y :: k) (xs :: t a) = Eval (UnEndo (Eval (FoldMap (Pure1 ('Endo :: (k -> Exp k) -> Endo k) <=< Pure1 f) xs)) y)
- data And (a :: t Bool) (b :: Bool)
- data Or (a :: t Bool) (b :: Bool)
- data All (b :: a -> Exp Bool) (c :: t a) (d :: Bool)
- data Any (b :: a -> Exp Bool) (c :: t a) (d :: Bool)
- data Sum (a :: t Nat) (b :: Nat)
- data Concat (a :: t m) (b :: m)
- data ConcatMap (c :: a -> Exp [b]) (d :: t a) (e :: [b])
Core interface
data Foldr (c :: a -> b -> Exp b) (d :: b) (e :: t a) (f :: b) Source #
Right fold.
Example
>>>
:kind! Eval (Foldr (+) 0 [1, 2, 3, 4])
Eval (Foldr (+) 0 [1, 2, 3, 4]) :: Natural = 10
Instances
type Eval (Foldr f y ('Left _a :: Either a3 a1) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('Right x :: Either a3 a1) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('Just x) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('Nothing :: Maybe a1) :: a2 -> Type) Source # | |
type Eval (Foldr f y (x ': xs) :: a2 -> Type) Source # | |
type Eval (Foldr f y ('[] :: [a1]) :: a2 -> Type) Source # | |
Defined in Fcf.Class.Foldable |
data FoldMap (b :: a -> Exp m) (c :: t a) (d :: m) Source #
Type-level foldMap
.
Instances
type Eval (FoldMap f ('Left _a :: Either a3 a1) :: a2 -> Type) Source # | |
type Eval (FoldMap f ('Right x :: Either a3 a1) :: a2 -> Type) Source # | |
type Eval (FoldMap f ('Just x) :: a2 -> Type) Source # | |
type Eval (FoldMap f ('Nothing :: Maybe a1) :: a2 -> Type) Source # | |
type Eval (FoldMap f (x ': xs) :: a2 -> Type) Source # | |
type Eval (FoldMap f ('[] :: [a1]) :: a2 -> Type) Source # | |
Defined in Fcf.Class.Foldable |
Default implementations
type FoldMapDefault_ (f :: a -> Exp k) (xs :: t a) = Eval (Foldr (Bicomap f (Pure :: k -> k -> Type) ((.<>) :: k -> k -> k -> Type)) (MEmpty :: k) xs) Source #
Default implementation of FoldMap
.
Usage
To define an instance of FoldMap
for a custom MyType
for which you already have
an instance of Foldr
:
type instanceEval
(FoldMap
f (xs :: MyType a)) =FoldMapDefault_
f xs
Example
>>>
:kind! FoldMapDefault_ Pure [EQ, LT, GT]
FoldMapDefault_ Pure [EQ, LT, GT] :: Ordering = LT
type FoldrDefault_ (f :: a -> k -> Exp k) (y :: k) (xs :: t a) = Eval (UnEndo (Eval (FoldMap (Pure1 ('Endo :: (k -> Exp k) -> Endo k) <=< Pure1 f) xs)) y) Source #
Default implementation of Foldr
.
Usage
To define an instance of Foldr
for a custom MyType
for which you already
have an instance of FoldMap
:
type instanceEval
(Foldr
f y (xs :: MyType a)) =FoldrDefault_
f y xs
Example
>>>
:kind! FoldrDefault_ (.<>) EQ [EQ, LT, GT]
FoldrDefault_ (.<>) EQ [EQ, LT, GT] :: Ordering = LT
Derived operations
Predicates
data And (a :: t Bool) (b :: Bool) Source #
Give True
if all of the booleans in the list are True
.
Example
>>>
:kind! Eval (And [True, True])
Eval (And [True, True]) :: Bool = True
>>>
:kind! Eval (And [True, True, False])
Eval (And [True, True, False]) :: Bool = False
data Or (a :: t Bool) (b :: Bool) Source #
Give True
if any of the booleans in the list are True
.
Example
>>>
:kind! Eval (Or [True, True])
Eval (Or [True, True]) :: Bool = True
>>>
:kind! Eval (Or [False, False])
Eval (Or [False, False]) :: Bool = False
data All (b :: a -> Exp Bool) (c :: t a) (d :: Bool) Source #
Whether all elements of the list satisfy a predicate.
Note: this identifier conflicts with All
(from Data.Monoid).
Example
>>>
:kind! Eval (All (Flip (<) 6) [0,1,2,3,4,5])
Eval (All (Flip (<) 6) [0,1,2,3,4,5]) :: Bool = True
>>>
:kind! Eval (All (Flip (<) 5) [0,1,2,3,4,5])
Eval (All (Flip (<) 5) [0,1,2,3,4,5]) :: Bool = False
data Any (b :: a -> Exp Bool) (c :: t a) (d :: Bool) Source #
Whether any element of the list satisfies a predicate.
Note: this identifier conflicts with Any
(from Fcf.Utils),
Any
(from Data.Monoid), and Any
(from GHC.Exts).
Example
>>>
:kind! Eval (Any (Flip (<) 5) [0,1,2,3,4,5])
Eval (Any (Flip (<) 5) [0,1,2,3,4,5]) :: Bool = True
>>>
:kind! Eval (Any (Flip (<) 0) [0,1,2,3,4,5])
Eval (Any (Flip (<) 0) [0,1,2,3,4,5]) :: Bool = False
Numbers
data Sum (a :: t Nat) (b :: Nat) Source #
Sum a Nat
-list.
Example
>>>
:kind! Eval (Sum '[1,2,3])
Eval (Sum '[1,2,3]) :: Natural = 6
Lists
data Concat (a :: t m) (b :: m) Source #
Concatenate a collection of elements from a monoid.
Example
For example, fold a list of lists.
Concat :: [[a]] -> Exp [a]
>>>
:kind! Eval (Concat ([[1,2], [3,4], [5,6]]))
Eval (Concat ([[1,2], [3,4], [5,6]])) :: [Natural] = [1, 2, 3, 4, 5, 6]>>>
:kind! Eval (Concat ([[Int, Maybe Int], [Maybe String, Either Double Int]]))
Eval (Concat ([[Int, Maybe Int], [Maybe String, Either Double Int]])) :: [*] = [Int, Maybe Int, Maybe [Char], Either Double Int]