Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 832 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



A curated collection of Monoids and their uses

1:12 JavaScript lesson by

A gallery of monoid definitions and some use cases

Get the Code Now
click to level up

egghead.io comment guidelines

Avatar
egghead.io

A gallery of monoid definitions and some use cases

Avatar
Sean

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/2hqhXZ8

Avatar
Brian Lonsdorf

Posted on Github, but posting here for others. Data.Either has not implemented concat/traverse yet. Here's our either

const Right = x =>
({
  chain: f => f(x),
  ap: other => other.map(x),
  traverse: (of, f) => f(x).map(Right),
  map: f => Right(f(x)),
  fold: (f, g) => g(x),
  concat: o =>
    o.fold(_ => Right(x),
           y => Right(x.concat(y))),
  inspect: () => `Right(${x})`
})

const Left = x =>
({
  chain: f => Left(x),
  ap: other => Left(x),
  traverse: (of, f) => of(Left(x)),
  map: f => Left(x),
  fold: (f, g) => f(x),
  concat: o =>
    o.fold(_ => Left(x),
           y => o),
  inspect: () => `Left(${x})`
})

const fromNullable = x =>
  x != null ? Right(x) : Left(null)

const tryCatch = f => {
  try {
    return Right(f())
  } catch(e) {
    return Left(e)
  }
}

module.exports = { Right, Left, fromNullable, tryCatch, of: Right }
In reply to Sean
Avatar
Sean

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.

In reply to Brian Lonsdorf
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?