Transcript from the "Mathematical, Pure Functions" Lesson

[00:00:00]

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

[00:00:20] 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.

[00:00:34] 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.

[00:00:56] 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.

[00:01:22] 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.

[00:01:40] 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.

[00:01:58] 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.

[00:02:23] 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?

[00:02:37] 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.

[00:02:57] 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.

[00:03:12] [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.

[00:03:31] 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.

[00:03:45] 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.

[00:04:05] 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.

[00:04:28] 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.

[00:04:47] 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.

[00:05:10] 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.

[00:05:29] 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.

[00:05:47] 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.