This course has been updated! We now recommend you take the Hardcore Functional Programming in JavaScript, v2 course.
Transcript from the "Either" Lesson
[00:00:00]
>> [MUSIC]
[00:00:04]
>> Brian Lonsdorf: I'm going to go through Either kind of fast. What other talk was that, I don't know. Anyway, I gotta dim the screen so I don't get distracted. Okay, so Either is just like Maybe but it's used for error handling. Which is kind of neat, in a pure functional way you can handle errors.
[00:00:27] Now, it's like maybe, but we do have two subclasses this time. It's not gonna be just a maybe null or maybe something else. And if you see other implementations of maybe, you're gonna see just and nothing. And it's gonna look a lot more like either here. So, let's look at how this works.
[00:00:49] Just like maybe in container, if I run over right, the x becomes the two. And I add one to it. Beautiful. Left just like a maybe no it does not run it at all but what's cool about a left is it has some error message in it. Just kind of embedded in the type.
[00:01:10] It's not going on the function but it will run the message, it will return it with a message in there. So I could use that for errors so I could say if it's good keep running it with the value inside if not just return a left with the error message in it and those two types map works differently with both types.
[00:01:27] So if I decide somewhere in my app to return a left boom app stops running altogether. Error message ends up propagating to the end. Now we're just talking about that you may or may not have the value in a maybe. You might have a left and you might have a right, and you don't know.
[00:01:48] And you're telling your app that, and your coding to that. You're writing a safe app Aware of the fact that may or may not be there. Now if you say like, yeah, I don't want that anymore. I want to go back to [LAUGH] how it was. You can go back to being unsafe and you can get the value out.
[00:02:07] You really can. There are some functions that will do that. There is a lowercase either that takes two functions to run. One on If it's a left side. One of it it's a right side. It will actually pick apart the type and get you back out. Same with maybe.
[00:02:20] It will take a function to run if it's null or otherwise. Cuz sometimes you wanna take some action. You don't wanna just ignore the rest of your app. You want to take some action if it's null, or take some action if it's a left, but for the most part, these abstractions are wonderful.
[00:02:35] And you shouldn't be using those functions, that's the wrong way to do it, if you have to use one of those functions, it's like 2% of the time. And you're probably doing it wrong unless there's a specific action you want to take, and so just return the error message.
[00:02:49] Okay. So let's look at how this works a little bit more this should be kind of easier to understand at this point it's just like a maybe just like an identity just like a list. We're saying but it's a little bit more complicated because there's two types involved.
[00:03:07] It's left and its right. So if I say he determine the age of this user and then we'll add one to it because I don't know what else to do. I'm bored. I've run out of examples that are interesting. But the main thing is. It says if it does have an age return it, and if it doesn't, tell us we couldn't get agent and boom map just does the right thing.
[00:03:28] We're either going to add one to the age, or we're not going to get anything out, and it's just going to return that error message back. Do you guys see how that works as an error handling?
>> Speaker 2: Hey, Brian there's a couple of questions in chat here. One on what's right and left and whether they're part of the point freelib.
[00:03:44]
>> Brian Lonsdorf: Okay, all these libraries will be included in the examples and in the demo app so you can look at them further. But these are two subclasses of the either library. We're using folktale, just for those who are impatient, for this one And so, yeah, it's folktales either library, two subclasses.
[00:04:08]
>> Speaker 2: There is another question, is right kinda like my func or val while left is my val and my function.
>> Brian Lonsdorf: Hey, hey, hey, nice. No, I at some point put an either up. That was wrong and it messed up a lot of people's brains. So, sorry about that.
[00:04:31] Either is specifically like left will never run, it has the error in it. Right will always run. You're good. But it is a disjunction operator. You've got left or right. It's just that when you map over it, you never run the left. So, it's disjunction in that right.
[00:04:51] You know what I'm saying, it's the or in that sense, but why are they called left and right? I don't know. Anyway [LAUGH] Left and right is, I should tell you. Left and right, it either has two properties. It's left and it's right. It's x and y. I don't know.
[00:05:11] So it's either the left side or the right side of the either. If you think it is an or operator, it's left or right. Okay, moving on I'm gonna spend too much time on either because you kind of just understand that. Hey, it's gonna run on or right or it's going to run errands not gonna run on the left and just remember the part that top line And determine the age that if you wanted to run return a right if, you wanted to stop or turn left.
[00:05:37] There's not much more to it and the reason I spend so much time on it is when you're doing the synchronous stuff which we are all the time always kind of isn't as useful as when you're doing sequence stuff.