The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

A curated collection of Monoids and their uses

A curated collection of Monoids and their uses

1:12
A gallery of monoid definitions and some use cases
Watch this lesson now
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
Avatar
Fred

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:

  1. why is empty() defined in a separate statement instead of within the ({}) like fold, map & concat ?
  2. do you agree that neutral() or identity() would have been more appropriate than empty() if you consider the underlying math

Thank you!

In reply to egghead.io
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?