Check out a free preview of the full Hardcore Functional Programming in JavaScript, v2 course

The "Mathematical, Pure Functions" Lesson is part of the full, Hardcore Functional Programming in JavaScript, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Brian explores three small tests to determine if one is writing a function: the object needs to be total, have an input for every output, be deterministic, and have no observable side effects.


Transcript from the "Mathematical, Pure Functions" Lesson

>> Functional programming, it's programming with functions. That's the definition that we're gonna use today. So, programming with functions, we should probably know what a function is. So we're gonna define a function from set theory as a single valued collection of pairs. So if you think of it it's just a set of inputs and outputs.

Just a bunch of, so if I give it a -2, I get a -1, and so on. As a counter example, this one on the right, we take a 1 and we get a D out. We give it a 2 we get a B out. And then we get a 2 we get a C out.

We don't know, it's like two different outputs for the same input. That disqualifies it from being a function. So then it becomes a relation. So we're gonna focus on one input to one output deterministically. So we call the input the domain and the output the range. And this is pretty neat.

You can define a function with a table. You can plot functions. You can train AI to learn functions. You can actually turn relations into functions by providing an ordering and grabbing the first. And we could even do stuff like define functions with just bracket syntax. [LAUGH] No, I couldn't fit all of the letters on the slide.

So this top function is not total. Which we'll talk about here in a minute. And the bottom one, I could never actually read it this way, right? There's like infinitely many numbers and I can't write all this out. So it's a little impractical. But that's why we're in programming land and we're gonna do functions with programs.

And in our worlds programming is not math, right? And we could look at three little tests to know if we're writing a function or if we're not writing a function. The first one is the function total. And we'll talk about this in a second. Second one is it deterministic.

And the third is, is there any observable side-effects? We don't want those. So a total function as opposed to a partial function means that for every input there is a corresponding output. So that practically, means if I say increment here on the left side, and I say, yeah, I'll take any number, any number, give me a number, give me a number.

And then, I'll give you the next number. And the I just blow up with a null, that's not a total number, [LAUGH] right? That's an old bit my friend used to be, ask me anything. I know everything ask me anything. And then, he'd be like, well, what's that?

And he'd be like, yep, I know that. [LAUGH] It's hilarious, anyway. So [LAUGH] on the right, we got increment and this one is total, right? It'll always give you another, within the bounds of whatever CPU limits and whatnot, or memory limits. But remembering limits, but the point is, we wanna have a corresponding output for every input.

And on the left, you could give it a four, three and it would just die on it. So that's not total. Incidentally, on the on the right side, if I just returned 100. It's a bizarre increment function, but it's total right? It always has an output for every input.

[LAUGH] So it's basically the contract that you will always have a value in the range. Versus, oops, not a value [LAUGH] like an error and all or something.
>> So for the first function there, could you say that it's total for zero to two?
>> And yep, that's exactly right.

If we were doing a typed language and I wrote on the left that it took any number and returns a number, that would not be total. But if I gave it an enum of zero, one, and two and said it only takes these then that would be total.

So it depends on how you frame it. Likewise, if we go back, if we said on the left side, what it returns either a number or a null, then that would make it total. But it's all about our claim that this function is just integers ensure, okay, deterministic.

What does that mean? Well, if you give it an input you always get the same output. I like to call it reliability. [LAUGH] So, look at an example on the left. So time since if I give it the same comment again and again, I'm gonna get a new value out.

So it's not the same output for the same input. Because the date keeps changing on me. If I keep creating these new dates, and I try to get the time since it's gonna change the output. On the right side, it looks much less functional or whatever, but it's totally deterministic.

If I give it two dates, it'll always give me the same difference. So that's deterministic functions. You always get the same output, given an input. And no side effects. This is last test, we're gonna re-do, is that, if you can observe any side effect at all besides it just competing a value, then, that disqualifies it from being a function.

So even something as simple as a log, which i'm not that strict on this but, think this is a good demonstration of like on the left side this is loging. So it's changing the outside world in an observable way. And on the right side, we're just appending the log as a value, totally functional.

Everybody's happy.
>> Who decides what is observable and what isn't?
>> Well, yeah, so that's a good question. You could always say my computer got hotter. [LAUGH] But I would I think it's use your discretion if you're changing the screen or the database or the state of the observable world.

That's why the word observable is in there. It's as a user of this program, am I observing a side effect? And that yeah, totally can be philosophical if we want it to be or just intuitive. [LAUGH] As far as the things that you'd see a side effects like writing to screens, changing database values, reading files, writing files, whatever.

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