Check out a free preview of the full Hardcore Functional Architecture Patterns in JavaScript course:
The "Function Modeling" 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 explains that, instead of modeling data, it is possible to model a function. Functional modelling allows to use different methods.

Get Unlimited Access Now

Transcript from the "Function Modeling" Lesson

[00:00:00]
>> We're gonna talk a little bit about function modeling. Let's hop over to functions. Doo doo doo doo doo doo. Let's see what to get myself to start. Thank you, Brian. That's very nice to start off with nothing. Okay, so [LAUGH] so we just did function modeling with validations.

[00:00:20] Did anybody catch that? We said, I'm going to, instead of modeling a type of data and the data inside that type, I'm gonna model a function. We're gonna call it run, and then we're able to combine functions. So let's talk about that a little bit more. Let's give ourself some functions, my favorite ones, toUpper, actually I should probably just bring in rammed or something but it's fine, x to UpperCase.

[00:00:48] All right, and other favorite function exclaimed we'll just do x concat, [LAUGH] there we go. And then let's see what else, should be fine. Okay, so what we're trying to do here is take these functions and let's say I want to combine them and compose them in different ways.

[00:01:16] I want to model them and do interesting things with them. Function doesn't give you any methods on it. So what we could do is actually make function. And that takes some, which I'm gonna call run and run we'll expose that. So we can actually run it. Let's just start by doing a concat.

[00:01:36] Because we've just worked with monoids. We're just fresh from that. So if I want to concat two functions, how would I do that? Well, I have another function. And I'll call other.run. And I know I have my run. And I have to run the function to get the value out.

[00:01:55] And then I concat the value that the other one gave me, right? But it have to be closed, I have to return new function, if I'm combining two functions I have to return a function. So we'll start there, we'll start with function. And hey, look, function takes an argument.

[00:02:09] Now all of a sudden have an x, out of nowhere, we just have an x now, isn't that neat? I get that passed in. So now I can run my other one with x. Remember my function with x, and I can concat the results. That is cool. Does anybody have any questions on that?

[00:02:26] That's exactly what we just did with validations. And what's interesting about this is concating two functions says I have two boxes. You can think of functions as a factor, I think we can formally make it a factor, let's do that. Now, let's map f over our function. So you always keep it back in the box, get an x, run it on the x, we get a value.

[00:02:53] And we run f on that value. And now we have a factor, right? Run is gonna return us a value, function you can think of as a box holding a value and you have to run it to get the value and then we run f on it. So where I was going with that is is we have these ways to combine functions, map is just a composition on a function.

[00:03:17] And concat says run both and then combine the results. I'm holding this thing and just like the intuition says from all the other examples, I'll open up my box and concat what's inside. So, the box example kind of breaks down when we're talking about functions because you don't really think of functions as a box but, you can I guess.

[00:03:38] So let's do function to upper and we'll concat that with its function exclaim. All right, great. And then we'll run that with a fp sux. All right [LAUGH]. All right, no function modelling zero. Okay, so what happened here? The first one UpperCase it returned a string. And since strings a semigroup or a monoid, we concat it.

[00:04:18] We concatt it with the result of running the other one. Interesting. So we've run two functions and we've concacted the results. I could do something where we put them in an arrays and we'll get the arrays of the results and so on. But this is starting to form the basis of what we call the reader monad.

[00:04:43] We're actually modeling a function itself and if we map over it, let's do this. Let's map over the result, which is this pull number now, right? And then we'll whatever, take a slice of the first couple slice, three. Does that work? All right, cool. So we're able to map over the result of combining these two functions.

[00:05:14] And then, we run it on that. So, what is it? What does it mean to make function a monad? What would that mean? Monads talk about nesting, right? So we'd say, I have a function within a function, right? Each function takes some argument, right, and this takes a y.

[00:05:36] We have an x and a y here now. We wanna flatten these two into one function. That's what monads do. So, let's write that. See what happens here. So we have chain to chain F. And what's gonna happen is we need to return a function on the outside.

[00:06:01] And the reason for that is well, we could just see, if I just decided to run my function, I don't even have an x to run it with. That doesn't work. [LAUGH] So I have to start by saying okay, but function it's gonna take an x, right. And then I'll run my function with the x.

[00:06:22] And we are saying okay, just like map, I want to call f on that, right. Pass the value of running my function into the next function. Maybe it's helpful before we go any further to see what it means. So let's say I am going to chain to upper with another function, so we get our UpperCase upper, and then I'm gonna return another function, right?

[00:06:51] That does exclaim on, oops, I spelled it wrong on the upper. And let's see, this takes a y and I guess it just doesn't do anything with it right now. We'll figure out what to do with this y but we have another argument, that's kind of the point.

[00:07:09] We just have this y out of nowhere. So let's get this to work and then we'll kind of look at what's happening here. So high, all right, so we're in this thing, we ran the function, we got another function out and we actually have to run that function now, right?

[00:07:29] With, say the x again and see what happens there. So let's go ahead and play through this one. See what happens and we get high cool,