r/learnprogramming • • 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

738 comments sorted by

View all comments

Show parent comments

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

int           => list<int>
string     => list<string>
list<int> => list<list<int>>

Now what about functions? Well, you can map functions across a list. So if you have a function

 f : A -> B

You can map it

 list-map(f) : list<A> => list<B>

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.

1

u/shawntco Apr 06 '22

I'm a web developer, most Haskell and the like goes right over my head

1

u/ExtraFig6 Apr 06 '22

I can write examples with JavaScript then!

1

u/shawntco Apr 06 '22

oh gosh please do, that would help way more

edit: I'm also reasonably smart when it comes to Typescript and Python, if those would be more useful then go for it