Functors: I was WRONG! - FunFunFunction #11

Fun Fun Function · Beginner ·🔧 Backend Engineering ·10y ago

Key Takeaways

Functors in JavaScript, specifically the map method in arrays, promises, streams, and trees, and their implementation in functional languages

Full Transcript

good Monday morning 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 in order for this video to make sense you need to have watch my previous video on funtours uh because I make a lot of Corrections to that video in this video you also need to be familiar with the concepts of map and Promises uh if you're feeling Rusty on those uh links to videos about them is in the description I would like to geek out on the definition of funter for one more video because the last video got a very big response I didn't expect to be people people to be so interested in funter to be honest uh but it also got a lot of uh good Corrections from some wonderful people in the functional programming Community especially Dr Boolean uh on Twitter who deserves an extra shout out for helping me you should really follow him if you're interested in functional programming today I'm going to make a stab at a more accurate definition than the one I made in the previous video in the last video I said that functors are functions like map that is not correct functors are objects that have a map method so for example it is array that is the functor because it has an implementation of map it's not map that is the functor it is the object that is implementing map that is the functor the most common example of a functor in the world is the JavaScript array other objects that are very often implemented as functors are promises streams or trees for example so you can think of functors as some sort of the generic equivalent of array having a map method funs are things in general not just arrays that we can map in the last video I mentioned that array. filter is a functor that is completely wrong filter has nothing to do with funs nothing at all it's objects that Implement map that we are interested in and the reason I got confused about this is that the community is a bit confused there are a lot of blog post describing uh functors as uh functions and uh refer to filter as a functor um and the reason that the community is confused is that in category Theory uh the subset of math that relates to functors a functor is actually a function and to make matters even more confusing the uh the word functor is often used in computer science to be completely synonymous with higher order function however hope is not lost for a common definition 95% of the functional programming Community uh including hcll closure uh F Scala settled on the definition that objects that Implement map are functors and to keep us sane that is the definition that we are going with in this video so what exactly do we mean when we say that uh the object should Implement map I mean we cannot just Implement map and have it return and call it a functor there needs to be some godamn rules if I had a bigger beard I would now refer to the functor laws in hll yeah but the the funter laws are great and all but I think that they sacrifice understandability uh in order to be correct and tur it's good to check them out eventually but you know the HTML spec is not a good place to start learning web development either instead let's have a look at the funter that we all know and love array and look at what it is that makes it qualify as a functor so here we have an array of dragon objects and we use map to just get the names of the Dragons again I would like to stress here that map itself is not what we refer to with the word funter it is array that is a funter because it has a map method we're going to explore three crucial things that array map does in order to qualify as a funter but before I do that I must show you the worst thing ever let's let's talk about three crucial things that uh array. map does in order to qualify array as a functor one transformation of contents the basic idea of the functor is that uh the map functions takes the contents of the functor and uses the Callback pass to map to uh transform the contents into something else this this function right here is the transformation function it trans forms the dragon object into just a dragon name a string and this transformation that's the first barrier of Entry that array passes in order to be called a fun some people um and these are the same some people that like uh homework or to do taxes like to point out that the uh transformation callback that we pass to um array. map actually gets two arguments uh the object and the array index which kind of stretches the definition of a functor but I think that there is a point where a secret Club becomes so secret that it has no members uh so let's just give array. map some slack here promises are often implemented as fun the promises built into ecmascript 6 do not have a map function but uh most promised libraries do so for example if you use Bluebird you can do this if we look at this code we create a promise that yields a dragon object after after two seconds and when we have it we map the name and uh then write that name to the console and note that the map callback is exactly the same as the previous example in that example we saw the array functor shielding the transformation callback from the complex reality that there are more dragons than one while the promise functor instead prot TS the transformation callback from the complex reality that there isn't any dragon until later i' like to make a small detour here and remind you that this is a really good example of what functional programming is all about breaking problems into teeny tiny super simple functions and then composing those functions together using super cool glue like functors functional programming allows us to do this create a solution separately for iterating arrays uh waiting for objects until later and actually extracting the name and once we've created those solution and have them separately we can easily compose them together and since they're separate Lego blocks we can reuse these things all over our app so we don't clutter them with uh waiting logic or uh array iteration logic that's solved now in one place and this allows us to reuse way more code than we would generally be able to in an objectoriented world two maintain structure the second thing that array. map does in order to uh qualify uh array for the title of functor is that it maintains structure if you call map on an array that is three items long you will get out an array that is three items long array map doesn't change the length of the array it doesn't return null and it doesn't return anything other than an array with the same length like you see in the example we transform the individual values contained in the array and we even change their types actually but map cannot alter the structure of the array itself and this is why this string functor in the last example was wrong it does get the um intuitive Spirit of functors right so if that example made you get functors uh then don't worry you don't need to erase that part of your brain but string doesn't strictly work as a functor even if we give it a map method because functors need to be able to contain any type functors are meant to be generic containers for anything uh like array or promise or stream or tree string doesn't work as a functor because it means that we cannot maintain structure and let's say that a transformation callback returned false and try to shove that into uh a string ABC like we just it doesn't compute a string with a map method welded onto it gets the spirit of the functor right but it needs to be a container for any object in order to be called a funtor and string isn't that three return a new functor the third and final thing that array map does in in order to be funter material is that the value that map returns must be another functor of the same type it's because of this strength that we can chain map calls like this here we have the same array of dragons but after we extract the names we also get the length of each name because the first map function returns a functor we can keep calling map on it you can do this map map map map map chaining with promises or streams or any other functor it's pretty cool in summary a functor is an object that has a map method arrays in JavaScript Implement map and are therefore functors promises streams and trees often Implement map in functional programming languages and when they do they too are considered functors the map method of the functor takes the funter contents and transforms them using the transformation callback that is passed to map map then returns a new functor of the same type which contains the transformed values today has been a little dry and a little theoretical I know we have explored the definition of a funter even more and we have looked at the most common funter array but in the next episode we are going to have a look at the coolest and most underused functor streams as usual that episode will be released on Monday 0800 GMT do not miss it put it in your calendar until next Monday morning stay curious

Original Description

💖 Support the show by becoming a Patreon https://www.patreon.com/funfunfunction A functor is an object that has a map method. Arrays in JavaScript implement map and are therefore functors. Promises, Streams and Trees often implement map in functional languages, and when they do, they are considered functors. The map method of the functor takes it’s own contents and transforms each of them using the transformation callback passed to map, and returns a new functor, which contains the structure as the first functor, but with the transformed values. Previous video on functors https://www.youtube.com/watch?v=YLIH8TKbAh4 Map https://www.youtube.com/watch?v=bCqtb-Z5YGQ Promises https://www.youtube.com/watch?v=2d7s3spWAzo Great functional person https://twitter.com/drboolean "What is a Functor?" by Tikhon Jelvis https://www.quora.com/Functional-Programming/What-is-a-functor-1/answer/Tikhon-Jelvis?share=1 Best place to learn Haskell http://learnyouahaskell.com/ Full script here https://medium.com/@mpjme/5f794a1ca246 💛 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 Funct
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fun Fun Function · Fun Fun Function · 22 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
21 Functors - FunFunFunction #10
Functors - FunFunFunction #10
Fun Fun Function
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 explains functors and their implementation in JavaScript, covering the map method in arrays, promises, streams, and trees, and how they are used in functional languages

Key Takeaways
  1. Define a functor as an object with a map method
  2. Implement the map method in JavaScript arrays
  3. Understand how promises, streams, and trees can be functors in functional languages
  4. Apply the map method to different data structures in JavaScript
💡 Functors are not just a theoretical concept, but a practical tool for working with data structures in JavaScript

Related Reads

📰
how to find in order you want
Learn to implement pagination and filtering in your API calls to retrieve data in the desired order
Dev.to · AlexfamDan
📰
I Started Learning to Code — So I Built My Own Product | SkillLink
Learn how to apply coding skills by building a personal project, accelerating the learning process
Dev.to · Mohamed Tarek
📰
Senior Java Interview Questions Aren’t Hard. The Follow-Ups Are.
Learn how to tackle tough follow-up questions in senior Java interviews to demonstrate your expertise and problem-solving skills
Medium · Programming
📰
Cannot Assign Requested Address in Production: The Connection Failure Most Backend Engineers Blame…
Troubleshoot connection failures in production by checking socket exhaustion and DNS resolution issues
Medium · Programming
Up next
OOP Concepts Explained|Object-Oriented Programming in 60 Seconds| #Shorts | #Simplilearn
Simplilearn
Watch →