Higher-order functions - Part 1 of Functional Programming in JavaScript
Key Takeaways
The video discusses higher-order functions in JavaScript, a fundamental concept in functional programming, and demonstrates how to use the filter function to compose small functions into bigger ones.
Full Transcript
hello in this video series I will teach you how to do functional programming in JavaScript in this first one I will Begin by telling you about one of the most important Concepts in functional programming and that is higher order functions I will talk about what higher order functions are and I will then move on to code and show you how to use them but before I do that I want to tell you why you should learn functional programming in case you don't know me I'm Matias panon mpj for short I have been a full-time programmer for about 10 years uh I've worked for absolute vodka uh blackberry and I I currently work as a front-end developer for Spotify I love functional programming functional programming has made programming so much more fun to me if you have been programming for a while but haven't yet tried your hand at functional programming before this video is going to change your life learning to program functionally is going to make you so much better programmer you will feel much more secure with the quality of your work and it will be a lot better and you will be more sord after you will be able to write your programs with less bugs in less time your code will have less bugs because your code will be easier to reason about and you will be able to write it in less time because you will be able to reuse more of your code higher order functions really like saying that it's um it's like saying quintessential it just makes you feel smart so um so let me tell you about higher order functions because that is going to make me feel super smart in Java script and and in all functional programming languages functions are values let me show you what I mean by that here we will see the basic function in its natural haby that so you'll all recognize this even if you don't know JavaScript because all non- insane programming languages have functions but not all programming languages can do this create an anonymous function and assign it to a variable just like any other value we can pass it around like this and you know yes this will be 90 again in functional programming languages functions are values just like strings or numbers functions can be assigned to variables or passed into other functions higher order functions but what are higher order functions good for well composition the fact that we can take one function and put it into another function allows us to compose a lot of small functions into bigger functions that's all a lot of theory let's look at how to actually use one of these things probably the most basic and useful higher order function is filter it's really simple filter is a function on the array that accepts another function as its argument which it will use to return a new filtered version of the array here we see a list of animals we have uh fluffy kins which is a rabbit we have Hamilton who is a dog and we have Ursula who is a cat and we want to filter out the dogs because we're writing Enterprise grade software here I am going to show you you how to do this using filter but before I do I want to remind you how you do this with a normal for Loop bam so this is just a Ultra normal for Loop it creates an uh array to hold the dogs it uh iterates over the animals animals array and uh if the species of the uh the animal it being iterated uh is equal to dog it will push that dog onto the animal's array now let's rewrite this using the filter function I'm going to comment out the for Loop and keep it around so that you can see it for comparison purposes and here goes filter so let's have a look at this filter accepts one argument uh another function functions like this functions that you send into other functions are called callback functions because the host function will call back to them filter will Loop through each item in the array and for each item it's going to pass it into the Callback function and when it does it will expect the Callback function to return either true or false to tell filter whether or not this item should be in the new array and after it's done it will return the new filtered array and that will be dogs remember how I said that uh you'll write software faster when you're doing functional programming notice here that the example that uses filter is a lot less code than the uh than the for Loop and that is not because the syntax is cosmetically shorter or anything it's because we're actually writing less code less Logic the reason that we need less logic is that when we write our software in in small simple functions they compose together which allows us to reuse functions all over the place so in the filter example we are just writing one line of logic really which is uh this one the line that determines what animals go goes into the array and the rest of the program creating a new array and stuffing animals in it that the um for Loop has to implement here and here that is actually handled inside the the filter function it's handled for us so we don't need to write it therefore the filter function the filter example is basically just one line of logic the Callback function and the filter function just SLO into each other they are composable I'd really like to stress how well these simple functions compose let's break out the Callback into a separate uh variable so notice here that is dog is just a function that checks that an object is a dog it doesn't really have anything to do with the filtering at all it's completely decoupled from it so we can use it for uh for other things for instance if we wanted the animals that aren't dogs we could pass it to um reject which is another higherorder function on the U uh on the array object that does the inverse of of filter so it would be anal dog and this will give us an array of animals that aren't dogs so compare this to the four Loop and you'll see that we've very cleanly broken the problem up into two completely separate problems the problem of uh determining if an animal is a uh is a dog or not and the problem of creating an array and uh stuffing um the objects into them and this way we we're allowed to think and reason and debug these problems separately and that is a lot easier than when the two solutions are are jumbled together like uh like in the for Loop today I've talked about how functional programming means that you can write software with less bugs in less time in JavaScript functions are values and you can exploit this by dividing your code into small simple functions and composing them together using higher order functions as an example I showed you the higher order function filter and how it compares to normal for loop we're just scratching the surface here learning functional programming is going to introduce you to A Whole New World that you didn't know existed there are many useful higher order functions besides filter next time I will talk about two of them map and reduce do not miss it hit subscribe and follow me on Twitter at mpj me stay curious see you next time
Original Description
💖 Support the show by becoming a Patreon
https://www.patreon.com/funfunfunction
In this video series, I will teach you how to do functional programming in JavaScript. In this first one, I will tell you about one of the most important concepts in functional programming: Higher-order functions. I will talk about what higher order-functions are, and then move on to code, and show you how to use them. I will also tell you why you should learn functional programming in the first place.
💛 Follow on Twitch
We record the show live Mondays 7 AM PT
https://twitch.tv/funfunfunction
💛 Fun Fun Forum
Private discussion forum with other viewers in between shows. https://www.funfunforum.com. Available to patron members, become one at https://www.patreon.com/funfunfunction
💛 mpj on Twitter
https://twitter.com/mpjme
💛 CircleCI (Show sponsor)
Robust and sleek Docker-based Continuous Integration as a service. I used CircleCI prior to them becoming a sponsor and I love that their free tier is powerful enough for small personal projects, even if they are private. Use this link when you sign up to let them know you came from here:
https://circleci.funfunfunction.com
💛 Quokka (Show sponsor)
Wonder how MPJ evaluates JavaScript inline his editor. Quokka is the answer - use this link when you buy to let them know you came from here:
http://quokka.funfunfunction.com
💛 FUN FUN FUNCTION
Since 2015, Fun Fun Function (FFF) is one of the longest running weekly YouTube shows on programming 🏅 thanks to its consistency and quality reaching 200,000+ developers.
🤦♂️ The Failing Together concept is what makes FFF unique. Most coding content out there focus on step-by-step tutorials. We think tutorials are too far removed from what everyday development is like. Instead, FFF has created a completely new learning environment where we grow from failure, by solving problems while intensively interacting with a live audience.
Tutorials try to solve a problem. Failing Together makes you gro
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 1 of 60
← Previous
Next →
▶
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
Too many tools and frameworks!
Fun Fun Function
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
Staying relevant as a programmer
Fun Fun Function
Factory Functions in JavaScript
Fun Fun Function
Composition over Inheritance
Fun Fun Function
Software needs to be better - FunFunFunction #1
Fun Fun Function
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
Clojure - FunFunFunction #5
Fun Fun Function
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
Functors - FunFunFunction #10
Fun Fun Function
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
Questions and Answers - FunFunFunction #12
Fun Fun Function
Streams - FunFunFunction #13
Fun Fun Function
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
Coders are herd animals - FunFunFunction #18
Fun Fun Function
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
Monad - FunFunFunction #21
Fun Fun Function
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
Questions and Answers - FunFunFunction #24
Fun Fun Function
Losing motivation - FunFunFunction #25
Fun Fun Function
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
What editor do you use? - FunFunFunction #31
Fun Fun Function
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
Haskell Basics - FunFunFunction #35
Fun Fun Function
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
Haskell lists - FunFunFunction #39
Fun Fun Function
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function
More on: JavaScript Fundamentals
View skill →Related Reads
📰
📰
📰
📰
Mechanics of Virtualization in the Frontend
Medium · JavaScript
When Software Transformed My Frontend Thinking
Medium · Programming
Here is how you can add your desired Google Fonts to your own React project.
Medium · Programming
I Was Asked to Design a File Uploader in a Frontend System Design Interview (SDE 2) | Part 1
Medium · JavaScript
🎓
Tutor Explanation
DeepCamp AI