DEV Community

Basic monads in Javascript

Jason on September 03, 2018

I'm going to explain some common monads that you can start using in your javascript today. Monads will help make your code easier to read, more mai...
Collapse
 
edwilliams profile image
Ed Williams • Edited

This looks a excellent library; looking forward to using this. Could you help with the following example?

const data = [
  { wrongName: 'Jason', level: 7, cool: true },
  { wrongName: 'Blanche', level: 8, cool: false }
]

Maybe(data)
  .map(people => people.filter(person => person.cool))
  .map(people => people[0])
  .map(person => person.name)
  .map(name => name.toUpperCase())
  .cata({
    Just: data => console.log(data), // JASON
    Nothing: () => console.log('No data available')
  })

this appears to give me a Uncaught TypeError: Cannot read property 'toUpperCase'... but I imagined it might deal with the error in a similar way to Promises

Collapse
 
rametta profile image
Jason

For this example the following would be better if you weren't sure if some fields would be available

const data = [
  { wrongName: 'Jason', level: 7, cool: true },
  { wrongName: 'Blanche', level: 8, cool: false }
]

Maybe(data)
  .map(people => people.filter(person => person.cool))
  .chain(head)
  .chain(get(['name']))
  .map(name => name.toUpperCase())
  .cata({
    Just: data => console.log(data), // JASON
    Nothing: () => console.log('No data available')
  })
Collapse
 
yurakostin profile image
Yuri Kostin

It seems to looks better with Ramda, isn't it:

const data = [
  { wrongName: 'Jason', level: 7, cool: true },
  { wrongName: 'Blanche', level: 8, cool: false }
];

Maybe(data)
  .map(filter(propEq('cool', true)))
  .map(head)
  .map(prop('name'))
  .map(toUpper)
  .cata({
    Just: console.log
    Nothing: () => console.log('No data available')
  })

PS: thanks a lot for the article

Thread Thread
 
rametta profile image
Jason

This isn't exactly the same because Ramdas prop() does not return a Maybe, so if that prop did not exist then ramda would return an undefined, which will cause problems down the line. Same with head()

Thread Thread
 
yurakostin profile image
Yuri Kostin

You are totally right. Sorry for my mistake.

Collapse
 
artydev profile image
artydev • Edited

The first example could be written:

const data = [
  { name: 'Jason', level: 7, cool: false },
  { name: 'Blanche', level: 8, cool: false },
  { name: 'Hary', level: 8, cool: true }
]

for (let p of data) {
  if (p.cool) {
    console.log(p.name.toUpperCase())
    break;
  }
  else {
    continue
  }
}
Enter fullscreen mode Exit fullscreen mode

But I don't like it :-)
Regards

Collapse
 
cyyyu profile image
Chuang Yu

This is so cool.

FYI if you don't quite get the point this article shows, give a taste on Haskell. The monad thing is from Haskell which is an extremely neat programing language.

Collapse
 
whipgit profile image
Stijn Winand

Great stuff. I'll share this with my brother-in-law because it'll do a great job at explaining some FP fundamentals for us to start another conversation with.

Collapse
 
asicfr profile image
asic

Thanks, really cool example.
A preference for the lib apart from Oncha ?

Collapse
 
rametta profile image
Jason • Edited

Thanks! I prefer pratica because I wrote it :)

Collapse
 
subject026 profile image
lewis

Thanks so much for this Jason! I've always felt FP was a bit over my head but this is a great little example and explanation. Thanks again 👍

Collapse
 
arielgueta profile image
Ariel Gueta

Thanks, I wonder what the implications are for performance?

Collapse
 
rametta profile image
Jason

Typically pretty negligible. The Maybe monad just checks the value against null and undefined and calls the callback function. So, not much happening.

Collapse
 
arielgueta profile image
Ariel Gueta

Yes, but you also create a new callback function for each step, and the gc should clean them up.

Collapse
 
elprosystem profile image
elprosystem

Hello. My dear for those who do not know what monads is still the same. have to talk about how to make and use but without a lib

Collapse
 
jakubgawlikowski profile image
jakubgawlikowski

I find using Result very neat for modeling AND logic, but what about if I need to branch my logic and provide two different Ok results depending on OR logic?

Collapse
 
rametta profile image
Jason

That should be no problem either, instead of returning Result's you can use values, example:

Ok('some value')
  .map(x => x.length > 4 ? 'long' : 'short') // .map() will stay an 'Ok'
  .map(x => x.toUpperCase())
  .chain(x => x === 'LONG' ? Err('too long') : Ok(x)) // switch to Err branch if you want
  .cata(...)