Functors - FunFunFunction #10
Skills:
Algorithm Basics80%
Key Takeaways
Introduces functors in the context of JavaScript, discussing their properties and behavior
Full Transcript
this is fun fun function a Monday Morning Show where we try to become more excited and confident about programming by exploring old wisdom wild ideas and having fun a lot of people have requested another functional programming video and uh I figured that I'd try my hand at one this week and I would like to talk about functors functors is actually something that you are probably already using in your everyday programming map and filter are functors for instance today we are going to explore what it is that makes map and Filter part of the funter family and we're also going to write a little funter of our own but before we do that we're going to do some programming without funs to get a sense of the problem that funs are here to solve behold Enterprise grade function + one this is a bit have a stupid function but I am using a super simple example here to not distract us from the concept uh that we're going to explore imagine that in the real world plus one is doing something much more complicated the function plus one just adds one to whatever value you pass to it so here we pass it a three and we get a four however if we pass in an array of numbers say three and four like this things will of course break but we would like it to work if we pass in Array 34 we would like it to return array 45 so we write something like this this no magic here just a lot of code if value is an array we iterate over the array and create a new array adding one to every number pause if you want to have a look but it's not that important uh and this works now we get a 4 five if we input 34 next up let's say that we discovered that we need plus one to work on strings in addition to arrays and single numbers like this if we pass it the string ABC it should output BCD so we add another bunch of code to handle the string case you can pause the video if you're interested in what the code does but it's really just incrementing the charts it's not too interesting the important thing to note here is that it's now pretty annoying that the function does a whole lot more than just plus it but it works it's big but it works but let's say that we also need to write a minus1 function then we would have to duplicate this whole shebang between the two functions or I guess that plus and minus could be generalized into an I don't know arithmetic function but when we add Square division floor and every other MTH math function on the sky we would end up with a huge generic math function instead it feels like there is a way to generalize this upper part here the thing that does the iteration of the string and the array respectively it doesn't really feel like it belongs in the plus function anyway right and this is where functors come in for the first part the array JavaScript actually has a uh built-in functor that you are probably already familiar with Mac BK this is all there is to pling arrays are you getting a sense of how funs are making things easier map should be familiar to you by the way if it is not go check out my video on map here if you don't know map the rest of this video will not make sense as I mentioned before if you've been programming JavaScript for a while you've probably been using funs without knowing that they are part of this family called functors in a minute we're going to talk about what it is that makes functor qualify as a part of the funter family but first I want to show you another example of a functor this time we are going to write it ourselves by using array map we've sorted out the uh array Plus in case but what about the other case Plus in strings going from ABC to B CD we will solve this by making a string Factor like this this time I really want you to pause the video and take this code in for a little bit before continuing what you see in the string functor is almost the same logic from the big version of the plus one function that we saw before nothing strange going on here really we pass in a string ABC into the string functor and we're getting BCD out string functor takes a value and a function function FN as you see value in this case is ABC and FN refers to the plus one function string functor goes on to split the string into an array called chars it then Maps over the array of chars the Char parameter that is being passed to the map callback is each character in the form of a string with the length of one so it's just you know a string that is B we convert that character to a number and we pass that into FN and again FN is going to be+ one in this execution we then take the return value of FN which is a number and convert that number to a character map will return an array of these incremented characters and we then join them together using join and because string functor now does this unpacking process for us we can now let plus one and minus one do their thing plus one and minus one no longer need all that iteration logic inside of them because that work is handled by the functors so string functor and map are both functors functors are functions that take a value and a function the value might be uh an array in the case of map or a string in the case of the string functor the functor is responsible for unwrapping the individual values of the value being passed in and then passes each of those into the function and then it takes the processed values and creates a new structure for those values that it returns another example of a functor would be array do filter for instance array dot for each is similar but it's not a functor it does take a value and it does take a function and it does unwrap the value into its component and passes it to the the function but it doesn't return anything in a structured form and that is what makes it not a functor let's summarize in the context of JavaScript a functor is a function that when given a value and a function will unwrap the value into its individual Parts feed those parts into the function that it has been given and take the return values and return them in a structured form and that is a fun I would really like your feedback on this video did this give you anything I picked funter for this episode because I um I haven't done a functional video in a long time and there's been a lot of requests for that and especially the elusive Moon ad have been among those requests and functors is one step on the way to understanding the reason that I've been dragging my feet uh with making functional programming videos is that from this point on and functors moving on it gets very mathematical the definition of functors that I've given you here today is correct in the context of JavaScript I think uh but in a mathematical context it's actually much more abstract and in that sense I'm not sure that I've given you exactly the right thing because it gets messy functors is a concept from a branch of mathematics called category Theory and I'm not entirely sure how to proceed with uh teaching these things I mean should we start learning category theory on the other hand a lot of people say that you don't need to understand category theory in order to understand monads or maybe monads is something that we should stay away from completely maybe I should start doing as series on has I think I really need your feedback on this one let me know your thoughts in the comments as usual the next episode will be out early Monday morning GMT time don't miss out subscribe and until next Monday stay t
Original Description
💖 Support the show by becoming a Patreon
https://www.patreon.com/funfunfunction
Big props to @santoshrajan for his article Functors (http://functionaljavascript.blogspot.se/2013/07/functors.html), which heavily inspired this video.
In the context of JavaScript, a functor is a function, that, given a value and another function, unwraps the values to get to its inner value(s), calls the given function with the inner value(s), wraps the returned values in a new structure, and returns that new structure.
Full script here: https://medium.com/@mpjme/f559dd80a658
💛 Follow on Twitch and support by becoming a Subscriber
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 whi
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 21 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
▶
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: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
Using the Publish–Subscribe Pattern in React with Native Browser Events
Medium · JavaScript
I built a small js library to enhance native html attributes instead of using a heavy UI component or writing tons of js
Reddit r/webdev
Part 5A — React Props Explained: Pass Data Between Components Like a Pro
Medium · Programming
Why I built a CV maker with 0 dependencies, 0 backend, and 0 signups published: true
Dev.to · Belkacem Halim
🎓
Tutor Explanation
DeepCamp AI