Instructor: We're importing the maybe type from crocks and we're pulling in some functions from a utils file. Our article object is being passed into getArticleName, which returns a maybe that contains the name property from the article.
The alt method recovers from a nothing here by replacing the nothing with a just of the hard-coded value. Then, the resulting maybe is mapped over createURL to give us a full URL with the article name as a slug.
Instead of using this hard-coded value for our default page name, let's use the A/B flag variable that we have here and a function to get that value dynamically. We're going to use the getDefaultPageName function that we've imported from utils. I'm going to make a call of that here, so we can see what that does.
We're going to pass in the A/B flag. We'll see that the result of this is a function. Passing in our flag is going to result in a function being returned. That function is going to take no arguments, but when we call it, it's going to give us back our default value. We'll see if we change this true to a false, we're going to get a different value for our default page name.
Now that we understand how this works, let's take this additional call off of here. We want to call this with the first argument and get our function back. Let's assign this to a constant. I'll define a constant here and we'll call it getDefaultFromNothing.
Now that we have this set up, let's see how we can use it to recover from a nothing, coming from getArticleName. I'm going to come down here, and I'm going to replace this call to alt with a call to a method called coalesce. We can see from the error message that we're getting, that coalesce requires both left and right functions.
What does that mean? What it means is we're going to pass two functions in as arguments. The first one is going to be considered our left. The second one will be considered our right.
The left function will be invoked in the case of a nothing and the right function will be invoked in the case of a just. In the case of a nothing, we want to recover from this with a value that we're going to get from our getDefaultFromNothing function.
We're going to put that here. That's going to be our left function. You'll notice that we have it set up so that getDefaultPageName takes the value ahead of time and returns a function that accepts no values.
The reason for that is that when we get a nothing here, we're going to have no value to pass along. We have to have that ready to go, so that we have a function that takes no arguments and returns the value.
Now, we can define our right function. In the case of a just, we don't really want to do anything with it yet. We're going to take that value and we're going to return it. We'll define a quick little identity function inline here.
Whatever we return from coalesce is going to be wrapped in a just. In the case of a nothing, it will get a value from getDefaultFromNothing. It will return that as a just, which means it's going to continue through our transformations.
In the case of a just, in this case, we're passing it through using that identity function on the right. Let's see this work.
I'm going to come up here. I'm going to update the article, so that we get a nothing from getArticleName. We'll see that we're getting page not found as our slug.
Now, if I come down here and I update this to false, you'll see that our slug is now uh-oh, because getDefaultFromNothing is returning a different value based on our A/B flag.
Now, let's say we have a situation where instead of getDefaultPageName, we have a function coming in from our utility, getDefaultPageURL. It's going to return the entire URL instead of just a name that needs transformation.
Now, I end up in this situation where we recover from the nothing and it still goes through our transformation. We end up appending a URL to the end of a URL. That's obviously not what we want.
What we can do is, we can come down here. Instead of passing whatever we get from coalesce through our mapping, we can replace the right with the mapping function itself. We're going to recover and we're going to get that URL out.
That's going to be the end of it, but if we go back to a situation where we have our name, then that's going to be passed through our transformation in coalesce and come out the other end as a just.