1:12
JavaScript
lesson by Brian Lonsdorf

A gallery of monoid definitions and some use cases

*cue retro music*

```
const Sum = x =>
({
x,
concat: ({x: y}) => Sum(x + y)
})
Sum.empty = () => Sum(0)
```

```
const Product = x =>
({
x,
concat: ({x: y}) => Product(x * y)
})
Product.empty = () => Product(1)
```

```
const Any = x =>
({
x,
concat: ({x: y}) => Any(x || y)
})
Any.empty = () => Any(false)
```

```
const All = x =>
({
x,
concat: ({x: y}) => All(x && y)
})
All.empty = () => All(true)
```

```
const Max = x =>
({
x,
concat: ({x: y}) => Max(x > y ? x : y)
})
Max.empty = () => Max(-Infinity)
```

```
const Min = x =>
({
x,
concat: ({x: y }) => Min(x < y ? x : y)
})
Min.empty = () => Min(Infinity)
```

```
const Right = x =>
({
fold: (f, g) => g(x),
map: f => Right(f(x)),
concat: o =>
o.fold(e => Left(e),
r => Right(x.concat(r)))
})
const Left = x =>
({
fold: (f, g) => f(x),
map: f => Left(x),
concat: o => Left(x)
})
```

```
const stats = List.of({page: 'Home', views: 40},
{page: 'About', views: 10},
{page: 'Blog', views: 4})
stats.foldMap(x =>
fromNullable(x.views).map(Sum), Right(Sum(0)))
// Right(Sum(54))
```

```
const stats = List.of({page: 'Home', views: 40},
{page: 'About', views: 10},
{page: 'Blog', views: null})
stats.foldMap(x =>
fromNullable(x.views).map(Sum), Right(Sum(0)))
// Left(null)
```

```
const First = either =>
({
fold: f => f(either),
concat: o =>
either.isLeft ? o : First(either)
})
First.empty = () => First(Left())
```

```
const find = (xs, f) =>
List(xs)
.foldMap(x =>
First(f(x) ? Right(x) : Left()), First.empty())
.fold(x => x)
find([3,4,5,6,7], x => x > 4)
// Right(5)
```

```
const Fn = f =>
({
fold: f,
concat: o =>
Fn(x => f(x).concat(o.fold(x)))
})
```

```
const hasVowels = x => !!x.match(/[aeiou]/ig)
const longWord = x => x.length >= 5
const both = Fn(compose(All, hasVowels))
.concat(Fn(compose(All, longWord)))
['gym', 'bird', 'lilac']
.filter(x => both.fold(x).x)
// [lilac]
```

```
const Pair = (x, y) =>
({
x,
y,
concat: ({x: 1, y: 1}) =>
Pair(x.concat(x1), y.concat(y1))
})
```

## Comment Guidelines

Member comments are a way for PRO Members to communicate, interact, and ask questions about a lesson.

## Commenting Guidelines

Here are some basic guidelines for commenting on egghead.io.

## Be on-topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact click here.

## Avoid meta-discussion

Is your comment resembles:

It will likely be deleted as "meta".

## Code problem?

Should be accompanied by code! JSFiddle, Plunker, CodePen, etc all provide a way to share code and discuss it in context.

## Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

egghead.io comment guidelines

A gallery of monoid definitions and some use cases

Having trouble with the foldMap over the stats List.of. Getting

`acc.concat is not a function`

, any ideas? Here's my code: http://bit.ly/2hqhXZ8Posted on Github, but posting here for others. Data.Either has not implemented concat/traverse yet. Here's our either

Sweeet, thanks! FWIW I also added

`isRight: true, isLeft: false`

to`Right`

and vice versa to`Left`

in order to get`First`

and`find`

working. Really enjoying this course, thank you.Hi. I am really enjoying this course so far: I generally have the same questions as the little characters attending Pr Frisby 😂

I just have a couple questions:

Thank you!