3 ms·
Ok, so I think I'm half way there. Is passing a function pointer, or any pointer for that matter, the way it is done in C, related to lambdas? I understand the
by meagain20000 11y ago
Ok, so I think I'm half way there. Is passing a function pointer, or any pointer for that matter, the way it is done in C, related to lambdas?
I understand the use case of passing functions. Can be quite useful. Ok, still not fully there but I think I'm starting to understand what you are talking about.
- jholman 11y agoIt's useful to be able to pass functions. In C, the mechanism for doing so is the function pointer. Function pointers feel really complicated, relative to most other features in C, and relative to the mechanism for passing functions in other languages that allow it, but yeah, they do the job. Consider, for example, how you'd pass a specialized comparator to a generalized quicksort function. In C, you pass a function pointer to a cmp() function. In Python you just pass a function, because functions are first-class values that you can use like anything else - you can pass it by name, or you can pass a lambda. In Javascript you can do like in Python, or you can declare a full-fledged function anonymously inline. In pre-8 Java, you have to pass a weird object that instantiates a class that has a static method with the right name; this is ugly as shit, but it does WORK. In Java 8 you've got lambdas, so that sucks less. It's not like you can do anything you couldn't do before. It's just a little less stupid now.
- meagain20000 11y agoGot it. One thing that really bothers me is the name lambda. It seems to be used for something I'm already familiar with and have used plenty before but with the added benefit of obfuscation to make me feel stupid. I wonder if somebody is trying to make themselves feel smarter or I'm just stupid. Anyhow, KISS.
- gnaritas 11y agoLambda calculus was invented in 1937 to describe this concept; it existed before function pointers, so the name is actually accurate, you're just not familiar with it.