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.


Transcript from the "Function Modeling" Lesson

>> 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.

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.

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.

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.

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 And I know I have my run. And I have to run the function to get the value out.

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.

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?

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.

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.

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.

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.

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.

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.

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.

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.

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.

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?

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.

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?

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,

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now