Check out a free preview of the full Hardcore Functional Programming in JavaScript course:
The "Either/IO Exercises 2, 3 & 4" Lesson is part of the full, Hardcore Functional Programming in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian picks up the pace a bit and demonstrates a few more exercises. Exercise 2 expands on exercise 1 by adding validation. You'll validate the for a length greater than 3 and return a Right or a Left based on that validation. In exercise 3, the Either used in exercise two should be rewritten to be a functor. Exercise 4 pulls text from a user input field and strips out the spaces. - Problem: http://jsbin.com/zegat - Solution: http://jsbin.com/tosusijoje

Get Unlimited Access Now

Transcript from the "Either/IO Exercises 2, 3 & 4" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Brian Lonsdorf: For exercise two, we basically wanna do what we did up here. We wanna return a right or a left based on the fact, if our length is greater than three, so it's like a validation, if our length is greater than three we will return a right otherwise we'll return a left.

[00:00:21] Okay. So We're going to return to right or left and the left says it should have this message in it. So my test passed. Okay. That part's done. And the right says it should have x in it. Okay. That is easy enough. Now what I wanna do? I want to check if the length is greater than three, I am going to return to the right.

[00:00:40] Okay. So if x is the length, greater than three, Cool. I had trouble.
>> Speaker 2: I had to put in parenthesis, the precedence was weird.
>> Brian Lonsdorf: Thank you.
>> Speaker 2: Screwed you up for a while.
>> Brian Lonsdorf: Sorry about that one.
>> Brian Lonsdorf: We could have done a if else too. But, there we go, exercise two passed.

[00:01:01] So all we did was write a validation that returned a right or left, which is going to affect the rest of our whole app from this point on. If they're not valid, it won't run. Now exercise three, this one is pretty fun. We're going to save these, and we'll see a console.log saved.

[00:01:23] Otherwise we won't, we wanna do, I don't know if this was a weird one or not. So we're gonna call this string or call the it's our function above we wanna use exercise two that we wrote ourself to see if their length is greater in three or not.

[00:01:41] Okay, so save them if it's greater than three. That's easy enough. Save over ex2, and the reason is because ex2 will return to right or left if it's greater than three, and then we'll save them or we won't based on that return to right or left. So we just kind of extended our computation to save if the validation was, we've done a validation here and it'll save or it won't.

[00:02:09] I know I'm going really fast over these, but are you guys kind of hanging in there or you just like, god what's going on? How are we feeling at this point? I'm going really fast to try to make up some time. Are you all right? All right. You guys okay?

[00:02:25] I mean like if someone is like, please help me, I will help you. [LAUGH] I'm not going to just run through it. So, let me know. All right moving it. So last three, my god, exercise four just ask us to get the texts. What is that gonna do?

[00:02:50] This one's interesting. Okay, this is IO. So what do we do for IO? Let's spend a little bit more time on this one. We are going to, it's gonna do this query selector, right? GetValue will do the query selector and return me the value. So I wanna get the value from this text input and then I wanna strip the spaces.

[00:03:14] Okay? Well just like always, we'll get our value and that returns us an I O. So I have to map strip spaces over it. At this point you should be like, this guy is just doing the same junk over and over again.
>> [INAUDIBLE]
>> Brian Lonsdorf: Did it not work?

[00:03:31] Did I spell it wrong? Spaces, here it is. But the beauty in that, is map is so generic that the type of container you put it in, you get different behaviors and it's very useful. So now this is a pure function that I have to run IO. If I don't ever run it, I never get anything out, totally fine.

[00:03:55] I get an IO with this whatever IO balance out of it. It's kind of weird.
>> Speaker 2: Is that is that in the output tab that's it's reading from? Is it reading from an actual text?
>> Brian Lonsdorf: Yeah, it's reading from these texts boxes.
>> Speaker 2: [CROSSTALK] looking things from a map but it's really from a text book now.

[00:04:10]
>> Brian Lonsdorf: Yeah, it's pretty neat. So it's purely reading from that, and we have to tell it, man you've gotta run IO, to actually kick this thing off. And that's cool because that means we could write our full app without worrying about anything, and it'll just return, we can just map and do other stuff to it and it's totally fine till the end where we kick it off.

[00:04:35] We push those dominoes.