Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Functional programming has some pretty important central rules that we should understand.
The rules
- Computation is the evaluation of functions
- Programming is done with expressions
- No side-effects from computation
- Functions are first-class citizens
- Functions should be limited in scope and functionality
-
0:00
Okay, let's see if we can set some ground rules and pick up a couple of
-
0:03
theoretical concepts before we get into the nitty gritty of writing code.
-
0:07
The first two are pretty straightforward.
-
0:09
Computation is the evaluation of functions.
-
0:13
This means that we do our work computing values and outcomes all in functions.
-
0:17
We'll try to keep as much of our work inside of discrete functions as possible.
-
0:22
To me, this is easiest to think of as math.
-
0:24
When we're just talking about it, we take two and combine it with another two, and
-
0:27
we get four.
-
0:28
But in programming we'd be using an add function that takes
-
0:31
at least two arguments.
-
0:33
That function then sends back the four.
-
0:35
So our computation was done by evaluating the add function.
-
0:39
This ties neatly into our next rule.
-
0:41
Programming is done with expressions.
-
0:43
We do our work by using expressions,
-
0:46
passing the output of one function into another, either through chaining or
-
0:50
by assigning the output to a variable and then using that variable.
-
0:54
Again, we can go back to math.
-
0:55
If we have an equation of 5 minus 3 times 2, that of course has subtraction and
-
1:00
multiplication in it.
-
1:02
And according to the order of operations in math, we do the multiplication and
-
1:06
use its resulting value in the subtraction to get us our answer of minus 1.
-
1:09
And just like doing the same math problem over and over again will
-
1:12
always get you the same answer, we should have no side effects from computation.
-
1:18
We don't want to change values that are outside of our function's scope.
-
1:22
We have to be careful with this when we're using mutable types like lists and
-
1:26
dictionaries.
-
1:27
Eliminating side effects means that we can run a function with the same
-
1:30
inputs multiple times and nothing else, no other inputs or
-
1:33
values anywhere in the stack will change.
-
1:35
It also means that we can more easily predict the state of the entire program
-
1:39
at any point in time.
-
1:40
Functions are first-class citizens.
-
1:44
In Python, functions can be provided as an argument to a function call or
-
1:47
returned from a function.
-
1:49
Since functions are usable as values just like variables are,
-
1:52
they're considered to be first-class.
-
1:55
To get the most out of functional programming,
-
1:57
a language needs to be able to do more with functions than just call them.
-
2:00
Being able to chain functions together, take functions as arguments, and
-
2:04
return functions as values gives us some fluidity to how we solve a problem.
-
2:08
Functions should be limited in scope and functionality.
-
2:12
We want to make sure our functions have a well-defined purpose.
-
2:15
We want them to achieve a single task and then give us the answer from that task.
-
2:19
By limiting the scope, or amount of data that a function has, and
-
2:23
the functionality of the function, we can keep our code clean, small, and
-
2:27
effective, all great things.
-
2:30
Okay, the next video will actually get us some practice
-
2:32
to go along with all this theory.
-
2:34
I'll see you there in a bit.
You need to sign up for Treehouse in order to download course files.
Sign up