r/learnprogramming • u/Temporary-Warthog250 • Apr 05 '22
Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them
Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.
Someone else who reads yours and does understand it, try to explain it to them to help them learn
1.6k
Upvotes
5
u/ExtraFig6 Apr 05 '22 edited Apr 05 '22
Start with functor.
If you tell me programming languages you're familiar with i can give you code examples
If you've used a function like map or for each, you've probably used functors.
A functor has two parts. It does something to types, and it does something to functions between those types.
For example, list. What does list do to types? It gives you a new type called list of ___. So
Now what about functions? Well, you can map functions across a list. So if you have a function
You can map it
By doing f to each element in the list, returning the result.
Optional/maybe/nullable is also a functor. You can think of it as being a list that can only have 0 or 1 element.
What does map do? If you've used an Elvis operator, it's like that. If the input is null, just return null. Otherwise, do the function.