Understand the Maybe Data Type

Share this video with your friends

Send Tweet

In this lesson, we’ll get started with the Maybe type. We’ll look at the underlying Just and Nothing types and create a simple utility function to create a Maybe from a value. Then we’ll see how we can apply functions to values and skip invocation for values that might cause errors or unexpected results.

Tre' Codez
Tre' Codez
~ 7 years ago

Loving it!

Tre' Codez
Tre' Codez
~ 7 years ago

I wonder how you've made vs code show your return values! Rather cool

Also, what theme is that?

Tulio Miranda
Tulio Miranda
~ 7 years ago

@Wayne, I've just searched for that (couldn't continue with the class knowing that there was something as awesome as that to be discovered). I found the QuokkaJs product ( made by the same team that built WallabyJs )

So, there you go!

Andy Van Slaars
Andy Van Slaars(instructor)
~ 7 years ago

Yup, it is Quokka from the Wallaby team.

The theme in the videos is this one: https://github.com/orefalo/bcmarinacci.theme-material-operator-1.3.0

Platon
Platon
~ 7 years ago

Hi, how do I get "Just" + return value or "Nothing" printed in the console instead of an unwrapped Maybe object (check the console output to this lesson, it's some object of type "crocks/Maybe@3")?

Invoking option(0) on a Just returns "Foo" as expected.

Andy Van Slaars
Andy Van Slaars(instructor)
~ 7 years ago

Hi, how do I get "Just" + return value or "Nothing" printed in the console instead of an unwrapped Maybe object (check the console output to this lesson, it's some object of type "crocks/Maybe@3")?

You can call it with the inspect method like this: console.log(result.inspect())

tsvika
tsvika
~ 5 years ago

Thanks Andy, There's a hype around ADT these days and I'm trying to understand it. As for Maybe shouldn't Typescript or Reason resolve these kind of issues? what's the added value?

~ a year ago

Playing with this in typescript, using @types/crocks. inspect is missing from the Maybe type. Following the code in crocks I see it just returns a string so I added the method in the Maybe.d.ts file "inpect(): string" then realized they probably don't include it because it already has the built in toString() method which does the same thing.

console.log(result.toString()); // Just 3