Map - Part 2 of Functional Programming in JavaScript
Skills:
JavaScript Fundamentals90%
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.
📹
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 2 of 60
1
▶
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
📰
📰
📰
📰
How I Replaced Three Manually Synchronized Applications with a Self-Healing PostgreSQL Cluster
Medium · DevOps
The runner deleted what it told me to rescue
Dev.to · Alkis Yuv
Configuration management with cdist to update servers
Medium · DevOps
Custom DNS Resolvers: Solving DNS Challenges in Modern Infrastructure
Medium · DevOps
🎓
Tutor Explanation
DeepCamp AI