Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course:
The "Creating Success & Fail Monoids" Lesson is part of the full, Hardcore Functional Architecture Patterns in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Brian demonstrates how to create success and failure types, and adds a concat method, therefore building two monoids.

Get Unlimited Access Now

Transcript from the "Creating Success & Fail Monoids" Lesson

>> So, this isn't great. So let's go ahead and make a type that does what we want it to do. So make a Success, and that'll be take some x. And so take this x into concat, another one, other.x. We gotta stay within the type, right? So we're gonna make Success and a failure.

[00:00:27] And they're both, we're gonna kinda define this as subclasses of the superclass of validation result or something. So it's closed in that it will always return you a validation result whether that's a success or a failure. Same thing with Either, you don't always get it right back when you concat to Either.

[00:00:47] Like you can get it right or left. It's closed under the Either type umbrella. But so we'll do the same thing here. So we'll say let's make a Fail. We'll say like isFail here so we know what it is. [LAUGH] And isFail: false up here, so that. I will say okay if the other one isFail, then what we want to do is keep the other one, right?

[00:01:17] Because that's just like I don't want to, I always want to keep the failures. But if not, we'll keep ourselves, and we'll just reconstruct a Success with x, cool. You know, if we were in a class, we could just do this, but I'm just remaking it. Any questions on how that works or why that works?

[00:01:41] if I'm trying to concat a success with a failure, I'm gonna keep the failure because I want the failure to continue on to be the result, because I don't want to accidentally pass. And with Fail, we'll say well, so I know I'm a failure. [LAUGH] I do know I'm a failure.

[00:02:02] But if the other one is a failure, then we actually want to concat, right? So we'll say other.x.concat, concat our x with the other x. This is kind of like the opposite of Either there, right? The failures are now concating their which would be collecting all the errors and the success.

[00:02:26] So if the other one's a failure, we'll actually concat them both, left to put it back in the Fail here. And otherwise, we'll just keep ourselves, sweet. All right, that looks like it might work, let's give it a shot. And we'll say, all right, instead of a Right, we'll do a Success.

[00:02:48] Instead of a Left, we'll do a fail. And let's see what's happening here. Run it, another blow up. It's a, I can't ever get it on the first try, I don't know how you all program but I suck. [LAUGH] So type is 81, it says other.fold is not a function, all right.

[00:03:12] Let's see what's happening here, looks like perhaps. I have a concat, I have a concat, returning Success and a failure. We're starting with an Either, there we go. So let's start with a Success with that in there, because I'm trying to compare two different things. And still blew up, same function.

[00:03:39] It's actually here. Derp, before I was returning Either in folding it out. Actually, let's make a nice little fold here so we can get out of our type here. So we get a f and a g, just like Either. And on the Success case, we'll run the right side with x.

[00:04:00] And on he Fail case, we will run the f, the left one. All right, so just like Either, this will hit the console.error or the console.log. And there we go. What do we get? We get, well this isn't working. [LAUGH] So looks like we passed it without it getting through.

[00:04:20] How's that working? We have a spec of an object key, and we're running this function. If it returns true, it will be a Success, if returns false, it is a Fail. So we're still getting the email as empty and it's happy. And if we look up here, we can see that I'm actually calling IsFail on the x, I don't wanna do that, I wanna call it on the type itself.

[00:04:49] So there, and up here, a little typo over there. And that should do the trick, let's see what happens. There we go, email bad. If I do both, do we get both? We do, we get both, awesome. If I put in a name, brian, and an email do we get?

[00:05:08], what happens here? Great, we get an array of just the object, terrific.