Functors - FunFunFunction #10

Fun Fun Function · Beginner ·🌐 Frontend Engineering ·10y ago

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 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
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

📰
Starting the Frontend for a Full-Stack E-commerce Store (Auth Store, Axios, and Public vs. Protected Routing)
Learn to set up the frontend for a full-stack e-commerce store using Auth Store, Axios, and public vs. protected routing
Dev.to · Chinwuba
📰
Practice Frontend Typeahead Interviews With AbortController and ARIA
Improve your frontend interview skills by practicing typeahead searches with AbortController and ARIA, learning to handle debounce, cancellation, keyboard navigation, and accessibility features.
Dev.to · Karuha
📰
Building Maintainable Frontend Systems
Learn to build maintainable frontend systems for long-term success by following practical guidelines and best practices
Dev.to · Ufomadu Nnaemeka
📰
Using the Publish–Subscribe Pattern in React with Native Browser Events
Learn to simplify component communication in React using the publish-subscribe pattern with native browser events
Medium · JavaScript
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →