Higher-order functions - Part 1 of Functional Programming in JavaScript

Fun Fun Function · Intermediate ·🌐 Frontend Engineering ·11y ago

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
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fun Fun Function · Fun Fun Function · 1 of 60

← Previous Next →
Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
2 Map - Part 2 of Functional Programming in JavaScript
Map - Part 2 of Functional Programming in JavaScript
Fun Fun Function
3 Reduce basics - Part 3 of Functional Programming in JavaScript
Reduce basics - Part 3 of Functional Programming in JavaScript
Fun Fun Function
4 Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Destructuring: What, Why and How - Part 1 of ES6 JavaScript Features
Fun Fun Function
5 Reduce Advanced - Part 4 of Functional Programming in JavaScript
Reduce Advanced - Part 4 of Functional Programming in JavaScript
Fun Fun Function
6 Closures - Part 5 of Functional Programming in JavaScript
Closures - Part 5 of Functional Programming in JavaScript
Fun Fun Function
7 Too many tools and frameworks!
Too many tools and frameworks!
Fun Fun Function
8 Currying - Part 6 of Functional Programming in JavaScript
Currying - Part 6 of Functional Programming in JavaScript
Fun Fun Function
9 Recursion - Part 7 of Functional Programming in JavaScript
Recursion - Part 7 of Functional Programming in JavaScript
Fun Fun Function
10 Promises - Part 8 of Functional Programming in JavaScript
Promises - Part 8 of Functional Programming in JavaScript
Fun Fun Function
11 Staying relevant as a programmer
Staying relevant as a programmer
Fun Fun Function
12 Factory Functions in JavaScript
Factory Functions in JavaScript
Fun Fun Function
13 Composition over Inheritance
Composition over Inheritance
Fun Fun Function
14 Software needs to be better - FunFunFunction #1
Software needs to be better - FunFunFunction #1
Fun Fun Function
15 Unit testing: How to get your team started - FunFunFunction #2
Unit testing: How to get your team started - FunFunFunction #2
Fun Fun Function
16 Straight-line code over functions - FunFunFunction #3
Straight-line code over functions - FunFunFunction #3
Fun Fun Function
17 Clojure - FunFunFunction #5
Clojure - FunFunFunction #5
Fun Fun Function
18 The growth stages of a programmer - FunFunFunction #6
The growth stages of a programmer - FunFunFunction #6
Fun Fun Function
19 5 tips to quickly understand a new code base - FunFunFunction #7
5 tips to quickly understand a new code base - FunFunFunction #7
Fun Fun Function
20 Semicolons cannot save you! - FunFunFunction #9
Semicolons cannot save you! - FunFunFunction #9
Fun Fun Function
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
22 Functors: I was WRONG! - FunFunFunction #11
Functors: I was WRONG! - FunFunFunction #11
Fun Fun Function
23 Questions and Answers - FunFunFunction #12
Questions and Answers - FunFunFunction #12
Fun Fun Function
24 Streams - FunFunFunction #13
Streams - FunFunFunction #13
Fun Fun Function
25 Prototypes in JavaScript - FunFunFunction #16
Prototypes in JavaScript - FunFunFunction #16
Fun Fun Function
26 Fast or Flexible? - FunFunFunction #17
Fast or Flexible? - FunFunFunction #17
Fun Fun Function
27 Coders are herd animals - FunFunFunction #18
Coders are herd animals - FunFunFunction #18
Fun Fun Function
28 Weekend Kubernetes Shenanigans - FunFunFunction #19
Weekend Kubernetes Shenanigans - FunFunFunction #19
Fun Fun Function
29 Monad - FunFunFunction #21
Monad - FunFunFunction #21
Fun Fun Function
30 Moar Weekend Shenanigans - FunFunFunction #23
Moar Weekend Shenanigans - FunFunFunction #23
Fun Fun Function
31 Questions and Answers - FunFunFunction #24
Questions and Answers - FunFunFunction #24
Fun Fun Function
32 Losing motivation - FunFunFunction #25
Losing motivation - FunFunFunction #25
Fun Fun Function
33 LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
LONGEST KUBERNETES SHENANIGANS! - FunFunFunction #26
Fun Fun Function
34 Fast code is NOT important - FunFunFunction #27
Fast code is NOT important - FunFunFunction #27
Fun Fun Function
35 Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Pair Programming a Facebook Messenger Bot - FunFunFunction #28
Fun Fun Function
36 Writing unit tests for personal projects? - FunFunFunction #29
Writing unit tests for personal projects? - FunFunFunction #29
Fun Fun Function
37 Let's Code a Pomodoro Button - FunFunFunction #30
Let's Code a Pomodoro Button - FunFunFunction #30
Fun Fun Function
38 What editor do you use? - FunFunFunction #31
What editor do you use? - FunFunFunction #31
Fun Fun Function
39 Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Arrow functions in JavaScript - What, Why and How - FunFunFunction #32
Fun Fun Function
40 Is Programming Art? - MPJ's Musings - FunFunFunction #33
Is Programming Art? - MPJ's Musings - FunFunFunction #33
Fun Fun Function
41 Generators in JavaScript - What, Why and How - FunFunFunction #34
Generators in JavaScript - What, Why and How - FunFunFunction #34
Fun Fun Function
42 Haskell Basics - FunFunFunction #35
Haskell Basics - FunFunFunction #35
Fun Fun Function
43 Haskell - Baby's first functions - FunFunFunction #36
Haskell - Baby's first functions - FunFunFunction #36
Fun Fun Function
44 Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Is Big O relevant to you? - Q&A Part 1 - FunFunFunction #37
Fun Fun Function
45 How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
How much are you allowed to Google? - Q&A Part 2 - FunFunFunction #38
Fun Fun Function
46 Haskell lists - FunFunFunction #39
Haskell lists - FunFunFunction #39
Fun Fun Function
47 var, let and const - What, why and how - ES6 JavaScript Features
var, let and const - What, why and how - ES6 JavaScript Features
Fun Fun Function
48 Why are some programming languages popular? - MPJ's Musings  - FunFunFunction #41
Why are some programming languages popular? - MPJ's Musings - FunFunFunction #41
Fun Fun Function
49 Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Does a developer need to be nice? - MPJ's Musings - FunFunFunction #42
Fun Fun Function
50 bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
bind and this - Object Creation in JavaScript P1 - FunFunFunction #43
Fun Fun Function
51 Examples of this and bind - Object Creation in JavaScript P2 -  FunFunFunction #44
Examples of this and bind - Object Creation in JavaScript P2 - FunFunFunction #44
Fun Fun Function
52 Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Prototype basics - Object Creation in JavaScript P3 - FunFunFunction #46
Fun Fun Function
53 Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Separation of concerns RANT - MPJ's Musings - FunFunFunction #47
Fun Fun Function
54 Cellular Automata - Pair Programming - FunFunFunction #49
Cellular Automata - Pair Programming - FunFunFunction #49
Fun Fun Function
55 The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
The 'new' keyword - Object Creation in JavaScript P4 - FunFunFunction #50
Fun Fun Function
56 __proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
__proto__ vs prototype - Object Creation in JavaScript P5 - FunFunFunction #52
Fun Fun Function
57 Unity game pair programming - Let's code - FunFunFunction #53
Unity game pair programming - Let's code - FunFunFunction #53
Fun Fun Function
58 Throw out your tools - MPJ's Musings - FunFunFunction #54
Throw out your tools - MPJ's Musings - FunFunFunction #54
Fun Fun Function
59 Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Unit tests vs. Integration tests - MPJ's Musings - FunFunFunction #55
Fun Fun Function
60 Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Object.create - Object Creation in JavaScript P6 - FunFunFunction #57
Fun Fun Function

This video introduces higher-order functions in JavaScript and demonstrates how to use the filter function for composition. It highlights the benefits of functional programming, including writing software with less bugs in less time.

Key Takeaways
  1. Define a higher-order function
  2. Use the filter function to compose small functions
  3. Break down problems into separate functions
  4. Use callback functions to determine the output
💡 Higher-order functions enable composition of small functions into bigger ones, making code more reusable and efficient.

Related Reads

📰
Mechanics of Virtualization in the Frontend
Learn how to optimize frontend performance by virtualizing large lists, reducing the number of DOM elements and improving scrollability
Medium · JavaScript
📰
When Software Transformed My Frontend Thinking
Discover how software transformed one developer's frontend thinking, shifting focus from aesthetics to functionality and user experience
Medium · Programming
📰
Here is how you can add your desired Google Fonts to your own React project.
Add Google Fonts to your React project with these easy steps
Medium · Programming
📰
I Was Asked to Design a File Uploader in a Frontend System Design Interview (SDE 2) | Part 1
Learn to design a file uploader in a frontend system, a crucial skill for frontend engineers, and improve your system design interview skills
Medium · JavaScript
Up next
Sports on the Go: Africa makes history at 2026 FIFA World Cup
CGTN Africa
Watch →