Functors: I was WRONG! - FunFunFunction #11
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
Playlist
Uploads from Fun Fun Function · Fun Fun Function · 22 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
▶
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 to find in order you want
Dev.to · AlexfamDan
I Started Learning to Code — So I Built My Own Product | SkillLink
Dev.to · Mohamed Tarek
Senior Java Interview Questions Aren’t Hard. The Follow-Ups Are.
Medium · Programming
Cannot Assign Requested Address in Production: The Connection Failure Most Backend Engineers Blame…
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI