Check out a free preview of the full Introduction to Elm, v2 course

The "Maybe Overview" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Richard contrasts Elm with JavaScript when accessing the first element of an array, and an empty array. The concepts of "Just" and "Maybe" are introduced.


Transcript from the "Maybe Overview" Lesson

>> Richard Feldman: Talk about list.head, maybe. Type Variables and Pipelines. Okay, so let's start with a little bit of JavaScript code. Let's say I got a function here called first, and it's going to take a list of users and return the first user. So users[0], that's how this is done in JavaScript.

So if I call first on an array that has Sam and Jess in it, it's going to return Sam. If I call first passing an empty array, it's going to return undefined. That's what happens in JavaScript when you do [0] on an array that is empty. So what about in Elm?

Elm doesn't have a concept of undefined, so clearly it's going to have to do something else. So here I'm defining the same function. First users =, and I'm saying List.head users. So in Elm, List.head means gives me the first element in the list. I can call first passing Sam and Jess.

Now this is gonna give me back something slightly different. It's not giving me back Sam like it did with JavaScript. It's giving me back just Sam. And if I call first on empty list, it's giving me back nothing. So what's going on here? What's the deal with this just and nothing?

Well, those are basically part of a unit type call, sorry a custom type call maybe. So it's kind of like compare the implications of using these two different approaches. So here we saw two different outcomes. Calling it with an array that was not empty, that had Sam as the first element, got us back Sam.

Calling it with empty array got us back undefined. On the Elm side, we got back just and nothing. So if I were to call first our new users, it's new users being either Sam or undefined. And I call .length on that, It'll give me back two if I successfully got back something valid.

Or I might give back, hey cannot read property length of undefined. Like I said, that should be a three, Sam has three letters in it. In contrast, the Elm version I have just "Sam" and Nothing. Because this is not a string, this is actually a wrapper around a string, this is a custom type wrapper around a string.

I have to do a case expression on it before I can actually do anything with that string. So I can say case first newUsers of and if I got a Just, then I unwrap the string in there and give it a name, I'll call it user. And now I can call String.length on that, that'll work.

If I got Nothing, I have to do something else, I don't have the user in scope to attempt to do anything on. So, maybe I'll just default to returning 0. So the difference here is that if I want to operate on the results of List.head, I first have to run this case expression to deal with the two variants inside this custom type know as maybe.

If I got adjust, then I can actually work on it. If I got Nothing, I can't work on it. And so this is the difference, List.head will give you back just wrapped around the particular value inside the list that was there if it found one. And if the list was empty, it will give you back nothing.

Which doesn't wrap anything, because in that circumstance, there is nothing to give back.
>> Richard Feldman: So if we call List.head passing Sam and Jess, we get back to Sam. List.head of Sam and Jess is being called on a list of strings and we get back what's known as a Maybe String.

So Maybe is a parameterize type and its type parameter matches the list that it got. So if I call List.head on two floats, that would give me back a Maybe Float. And of course, if I call List.head on two Booleans, it would give me back a Maybe Bool.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now