Map - Part 2 of Functional Programming in JavaScript

Fun Fun Function · Intermediate ·☁️ DevOps & Cloud ·11y ago

Key Takeaways

Uses JavaScript to demonstrate the map higher-order function in functional programming

Full Transcript

hello in this series we are learning how to do functional programming in JavaScript you can find the full series by clicking there in the last episode we learned about higher order functions since functions are just values like any other value in JavaScript we can exploit that by making functions that take other functions as their arguments and that is called a higher order function we also learned to use a higher order function filter we learned that filter is a method on the array object that takes another function as its argument and uses that function to filter the array in this episode we're going to learn about something new and awesome called map just like filter map is another higher order function also so like filter it goes through an array but unlike filter it doesn't throw the objects away instead it transforms them what what does it mean let me show you take a look at this array you might notice that I'm reusing the array from the previous video but that is simply because arrays of an is very very common in Enterprise grade programming but the problem is different this time around we want to get an array of all the names of all the animals this is super simple to solve with a map but before I do I want to show you how we would solve this using a for Loop so let's check out what this does just console log the output run it and you'll see that this just logs out the names the code is super straightforward it just Loops the animals array and uh for each uh animal it just picks the name property and it pushes it uh into the names array that we create on line 10 now let's do the exact same thing but using map instead I'll comment out the for Loop here and keep it around for comparison [Music] purposes right let's do this remember that I told you that map is a function on the array object just like filter is map will take a callback function just like filter does the Callback function will be past each item in the animals array but here is where map gets different from filter filter expected the its callback function to return a true or false value that determined whether or not the item should be included in the array or not map will include all items in the array but instead it expects the Callback function to return a transformed object that it will put into uh the new array instead of the uh the original animal in this case that will be the name like that now let's save and run it and see if it produces the same output as the for Loop bam exactly the same using map to return a subset of an object like this where we return just the the uh the the name property is a very common usage pattern however since map just expects the call back to return any object we can use it to create completely new objects let's run that Tada fluffikins is a rabbit Coro is a dog Hamilton is a yeah they're getting it don't but do you see how little code this is it is amazing I'm going to put the for Loop example next to the um the map example so that you can compare and I'm going to remove the extra code that we added to the map so that they are doing the same thing now check here when I'm selecting the the the map example and here in the lower left corner you see that this it's 6 6 characters and when I select the uh the for Loop example you see that it's 89 characters that is like 30% more let me put it put it like in one line just to uh show you show you the size difference I'm going to pull this out a bit that is a a lot shorter short code is good because less code almost always means less bugs but it gets even better in ecmascript 6 we're getting Arrow functions I'm going to rewrite this using Arrow functions Arrow functions is a new shorter and better Syntax for functions in ecmascript 6 it looks like this so it's a bit shorter but it gets better if your function logic fits on one line like in this example you can just get rid of the return statement and the uh curly brackets and this will uh whatever statement is after the arrow function will be implicitly returned so uh let me comment out the the stuff that we keep around for comparison and go back to the console and run this whoops this won't work right off the bat because as I said Arrow functions are a part of the ecmascript 6 standard and that is not fully implemented into browsers or node yet however if you running the latest version of ijs you can just use the harmony Arrow functions flag and it works if you're watching this video from the future you don't have to do this in functional programming it's common to go even shorter and just shorten the uh variable names to X like this bam let me put this next to the original for Loop just to show you how gorgeous this [Music] is if you don't think this is absolutely beautiful you I you just have no soul there is you can't see Beauty in the world just check this this is 38 characters and this is 90 and that concludes this informational computer science video about the higher order function map uh which is which is part of this um uh uh this series about functional programming in JavaScript next time we will look at a classic uh higher order function uh called reduce don't miss it Please Subscribe if you like this also 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, we learn how to do functional programming in JavaScript. In this second one, I show you how and when to use the higher-order function map, and why it is the best thing ever. Resources: Code from the video https://gist.github.com/mpj/c5ae804e576042b3287d How to install and use io.js (for the arrow functions example) http://davidwalsh.name/es6-io 💛 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 grow as a developer and coworker. 📹
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fun Fun Function · Fun Fun Function · 2 of 60

1 Higher-order functions - Part 1 of Functional Programming in JavaScript
Higher-order functions - Part 1 of Functional Programming in JavaScript
Fun Fun Function
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

Related Reads

Up next
Containers vs Virtual Machines
KodeKloud
Watch →