3 ms·
Please explain it like I'm five: what is the big deal about lamdas?
by meagain20000 11y ago
Please explain it like I'm five: what is the big deal about lamdas?
- ianhedoesit 11y agoI think this StackExchange question is pretty useful. http://programmers.stackexchange.com/questions/107687/what-is-the-origin-and-meaning-of-the-phrase-lambda-the-ultimate http://programmers.stackexchange.com/questions/107687/what-i...
- jholman 11y agoI will assume, for the purpose of this answer, that you mean "what is the big deal about Java 8 getting lambdas?". For other meanings of your question, see other replies. In (pre-8) Java, variables can only hold primitive values or objects (in Java, an object is an instance of a class [c.f.]). Similarly, arguments to a method must all be primitive values or objects. This is kinda crazy, by which I mean hilariously limiting. It's often useful to pass around other types of things, including types (in Java that'd be primitive types or classes or interfaces), or functions, or partially applied functions, or whatever. Of course, Java doesn't have functions, only methods. Whoops. See http://steve-yegge.blogspot.ca/2006/03/execution-in-kingdom-of-nouns.html http://steve-yegge.blogspot.ca/2006/03/execution-in-kingdom-... for a bit of snark about how silly this whole situation is. So Java programmers, while desperately lacking in aesthetic taste (I keed, I keed) and largely blinkered by the java-cultural context in which they find themselves, turn out to be a pretty clever lot after all. Clever enough that despite their language, they come up with all these conventions for doing things like passing around types and functions and so on. So you get ideas like the Runnable interface, wherein one makes a class just so that it can have a static method (perhaps called "run") that does what the function would do, and you then instantiate that class, and pass the instance, just so it can be used to get at the "run" method. It works, but it's oh so ugly. So finally, after twenty years of this suffering, now Java programmers have some nice new syntactic sugar, to allow them to pass functions around with less explicit ceremony, thereby catching up with advanced languages like Python, C, Lisp, and basically everything except Cobol. Of course, under the hood it's still Predicate<T>, a generic type that represents a functional interface, which is a new bit of jargon they came up with to shoehorn this elementary functionality into their baroque conceptual framework. And you furthermore, in Java 8 you can even declare them anonymously (unlike e.g. C, AFAIK), to expedite the most common use case, which is passing a function as an argument to a higher-order function (like "map" or whatever). They're called "lambdas" because of the existence of the lambda calculus, an abstraction for computing that uses nothing but anonymous functions, and because many other languages have therefore referred to their anonymous function syntax as "lambdas" (Python even uses "lambda" as a keyword for this). So the big deal about lambdas in Java is that Java has one less vile pain point. (Any five-year-old could understand this explanation, right?)
- HelloNurse 11y agoFor the record, some COBOL dialects appear to have function pointers. http://www-01.ibm.com/support/knowledgecenter/SS6SG3_4.1.0/com.ibm.entcobol.doc_4.1/PGandLR/tasks/tpsub20.htm http://www-01.ibm.com/support/knowledgecenter/SS6SG3_4.1.0/c...
- meagain20000 11y agoOk, 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.