# Data.Foldable

- Package
- purescript-foldable-traversable
- Repository
- purescript/purescript-foldable-traversable

### #FoldableSource

`class Foldable f where`

`Foldable`

represents data structures which can be *folded*.

`foldr`

folds a structure from the right`foldl`

folds a structure from the left`foldMap`

folds a structure by accumulating values in a`Monoid`

Default implementations are provided by the following functions:

`foldrDefault`

`foldlDefault`

`foldMapDefaultR`

`foldMapDefaultL`

Note: some combinations of the default implementations are unsafe to use together - causing a non-terminating mutually recursive cycle. These combinations are documented per function.

#### Members

`foldMap :: forall a m. Monoid m => (a -> m) -> f a -> m`

`foldl :: forall a b. (b -> a -> b) -> b -> f a -> b`

`foldr :: forall a b. (a -> b -> b) -> b -> f a -> b`

#### Instances

### #foldrDefaultSource

`foldrDefault :: forall f a b. Foldable f => (a -> b -> b) -> b -> f a -> b`

A default implementation of `foldr`

using `foldMap`

.

Note: when defining a `Foldable`

instance, this function is unsafe to use
in combination with `foldMapDefaultR`

.

### #foldlDefaultSource

`foldlDefault :: forall f a b. Foldable f => (b -> a -> b) -> b -> f a -> b`

A default implementation of `foldl`

using `foldMap`

.

Note: when defining a `Foldable`

instance, this function is unsafe to use
in combination with `foldMapDefaultL`

.

### #foldMapDefaultRSource

`foldMapDefaultR :: forall f a m. Foldable f => Monoid m => (a -> m) -> f a -> m`

A default implementation of `foldMap`

using `foldr`

.

Note: when defining a `Foldable`

instance, this function is unsafe to use
in combination with `foldrDefault`

.

### #foldMapDefaultLSource

`foldMapDefaultL :: forall f a m. Foldable f => Monoid m => (a -> m) -> f a -> m`

A default implementation of `foldMap`

using `foldl`

.

Note: when defining a `Foldable`

instance, this function is unsafe to use
in combination with `foldlDefault`

.

### #traverse_Source

`traverse_ :: forall a b f m. Applicative m => Foldable f => (a -> m b) -> f a -> m Unit`

Traverse a data structure, performing some effects encoded by an
`Applicative`

functor at each value, ignoring the final result.

For example:

```
traverse_ print [1, 2, 3]
```

### #for_Source

`for_ :: forall a b f m. Applicative m => Foldable f => f a -> (a -> m b) -> m Unit`

A version of `traverse_`

with its arguments flipped.

This can be useful when running an action written using do notation for every element in a data structure:

For example:

```
for_ [1, 2, 3] \n -> do
print n
trace "squared is"
print (n * n)
```

### #sequence_Source

`sequence_ :: forall a f m. Applicative m => Foldable f => f (m a) -> m Unit`

Perform all of the effects in some data structure in the order
given by the `Foldable`

instance, ignoring the final result.

For example:

```
sequence_ [ trace "Hello, ", trace " world!" ]
```

### #intercalateSource

`intercalate :: forall f m. Foldable f => Monoid m => m -> f m -> m`

Fold a data structure, accumulating values in some `Monoid`

,
combining adjacent elements using the specified separator.

### #andSource

`and :: forall a f. Foldable f => HeytingAlgebra a => f a -> a`

The conjunction of all the values in a data structure. When specialized
to `Boolean`

, this function will test whether all of the values in a data
structure are `true`

.

### #orSource

`or :: forall a f. Foldable f => HeytingAlgebra a => f a -> a`

The disjunction of all the values in a data structure. When specialized
to `Boolean`

, this function will test whether any of the values in a data
structure is `true`

.

### #allSource

`all :: forall a b f. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b`

`all f`

is the same as `and <<< map f`

; map a function over the structure,
and then get the conjunction of the results.

### #anySource

`any :: forall a b f. Foldable f => HeytingAlgebra b => (a -> b) -> f a -> b`

`any f`

is the same as `or <<< map f`

; map a function over the structure,
and then get the disjunction of the results.