Check out a free preview of the full Introduction to Elm, v2 course:
The "Maybe & Pipelines Solution" 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 live-codes the solution to the Maybe and Pipelines exercise. - all of the clip

Get Unlimited Access Now

Transcript from the "Maybe & Pipelines Solution" Lesson

[00:00:00]
>> Richard Feldman: So first problem we have is the avatar URL. So we've got this maybe URL value. This a case maybeurl of and there are two possibilities. One is we actually got a URL and the other is this user did not specify an avatar is we're gonna have to do some sort of fall back.

[00:00:15] So we know the fallback case is gonna be smiley-cyrus here, and in the case that we actually have a URL for them we're just gonna return. So, in either pathway, in either situation, we've either got a URL or we've got Smiley Cyrus. So, let's see how that looks.

[00:00:32]
>> Richard Feldman: Fantastic, we now have actual avatar URL's for those folks. Great, they're back. Now, for the next one, actually you just sign up, blah blah, you can put whatever you want in this, doesn't really matter, example.com. Any password you want and as you can see, I've now got my Smiley Cyrus avatar that I'm rocking here.

[00:00:57] My feed is empty because I don't actually follow anyone, but the global feed shows me all this same stuff as I saw when I was logged out. So to reproduce the second bug, I need to open up new post, or edit an existing post. And so I'm gonna say] article title, Stuff.

[00:01:14] What's the article about, Things. Write the article, blah, blah, blah. And then to reproduce the bug, I need to put in tags that actually have too many spaces in between them, or at least, multiple spaces. So I hit Publish and it says, some tags are empty, curses. All right, so that's the problem, is that some of these spaces are actually getting interpreted as tags when we don't want them to be.

[00:01:37] We want them to just get thrown out. Okay, so that's the bug, to fix the bug we're going to go over to this code over here. All right, so we've got a pipeline that currently has one thing in it. We're splitting the tag string on a space, so we get a list of each of the things that are in between the spaces.

[00:01:55] Then we're calling List.map String.trim, which we'll trim all of them, but now we want to actually filter them. And we want to filter them such that we remove all the ones that have nothing in them. In other words, they were just spaces and they're going to be processed as empty tags.

[00:02:12] Okay, so we're gonna say tag and then tag double equals quote, did anyone try this? Okay, you probably know that it didn't work properly. It's not quite what we want and actually there's a special error message that I put in for this use case. So I'm just gonna put gibberish in here, tag, another, etc.

[00:02:36] I hit Publish is close but not quite. The filter are gonna returning true and it should be returning false. I'll admit this is an error of message was specific to the workshops, it's not what is actually there the real applications. Yeah, so this basically saying, filter it out only if its empty, sorry, keep it if it's empty.

[00:02:55] In other words, it does the opposite of what we wanted. It actually throws out all the ones that we should be keeping and then keeps the one that we wanted to be throwing out. If I change that to no equal to empty string, then it will actually work a good bit better.

[00:03:12] Now I can do this blah blah blah, tag the whatever spaces I, [COUGH] Publish Article. And sure enough, now I actually have a published successfully published article. The bug has been fixed, huzzah.