Hot answers tagged

116

There are a number of criteria that should make you consider not using a lambda: Size The larger a lambda gets, the more difficult it makes it to follow the logic surrounding it. Repetition It's better to create a named function for repeated logic, although it's okay to repeat very simple lambdas that are spread apart. Naming If you can think of a great ...


86

There's certainly a noticeable trend towards functional programming, or at least certain aspects of it. Some of the popular languages that at some point adopted anonymous functions are C++ (C++11), PHP (PHP 5.3.0), C# (C# v2.0), Delphi (since 2009), Objective C (blocks) while Java 8 will bring support for lambdas to the language . And there are popular ...


77

Look up "bootstrapping". Basically you start with a very minimal process/set of functions that can be used to compile the code that defines a slightly more functional compiler. This creates your next compiler which then can then be used to build code that can do even more. You repeat this process until you have a full blown compiler that can compile all the ...


47

Yes, it's simply a recurring phrase in the title of several papers, starting from a couple in the 70s, in which Sussman and Steele demonstrate the use of lambda calculus for programming, by means of a minimalist Lisp dialect named "Scheme" they devised for the purpose. You can find the papers themselves here; they're interesting and surprisingly relevant. I'...


47

There's a fair bit of incorrect information in ratchet freak's answer and in its comment thread. I'll respond here in an answer, since a comment is too small. Also, since this an answer after all, I'll attempt to answer the original question too. (Note however that I am not an expert on type systems.) First, the short answers to the original question are ...


47

tl;dr: while it's mostly syntactic sugar, that nicer syntax makes lots of things practical that used to end in endless, unreadable lines of braces and parentheses. Well, it's actually the other way around as lambdas are much older than Java. Anonymous inner classes with a single method are (were) the closest Java came to lambdas. It's an approximation that ...


45

Streams provide much better abstraction for composition of different operations you want to do on top of collections or streams of data coming in. Especially when you need to map elements, filter and convert them. Your example is not very practical. Consider the following code from Oracle site. List<Transaction> groceryTransactions = new Arraylist<...


44

The word "lambda" or "lambda expressions" most often refers to anonymous functions. So in that sense a lambda is a kind of function, but not every function is a lambda (i.e. named functions aren't usually referred to as lambdas). Depending on the language, anonymous functions are often implemented differently than named functions (particularly in languages ...


32

I think its interesting how much the popularity of functional programming has paralleled the growth and proliferation of Javascript. Javascript has a lot of radical features along the functional programming spectrum that at the time of its creation (1995) were not very popular among mainstream programming languages (C++/Java). It was injected suddenly into ...


29

It's all about the metafactory First, most method references do not need desugaring by the lambda metafactory, they are simply used as the reference method. Under the section "Lambda body sugaring" of the Translation of Lambda Expressions ("TLE") article: All things being equal, private methods are preferable to nonprivate, static methods preferable to ...


28

Lambda The Ultimate refers to the idea that the lambdas of lambda-calculus can effectively implement every builtin concept in every programming language, past, present, and future. Classes, Modules, Packages, Objects, Methods, Control-Flow, Data Structures, Macros, Continuations, Coroutines, Generators, List Comprehensions, Streams, and so on. As it ...


27

JavaScript and DOM event handlers meant that millions of programmers had to learn at least a little bit about first class functions in order to do any interactivity on the web. From there, it's a relatively short step to anonymous functions. Because JavaScript doesn't close over this, it also strongly encourages you to learn about closures too. And then you'...


23

Looking at the compiled code through ILSpy, there actually is a difference in the two references. For a simplistic program like this: namespace ScratchLambda { using System; using System.Collections.Generic; using System.Linq; using System.Text; internal class Program { private static void Main(string[] args) { ...


22

ChrisF's answer is excellent, but I wanted to add this example that always stuck by me after my computer science course on bootstrapping. Suppose you have a basic C compiler that does not support escape codes for strings yet, and you wanted to add that. You could add a snippet of code similar to this: if( str[i] == 0x5c ) { // ASCII code for ...


20

I think you made a bad assumption: Firstly, A represents an object in the physical world, which is a strong argument for not splitting the class up. I disagree with this. For example, if I had a class that represents a car, I would definitely want to split it up, because I surely want a smaller class to represent the tires. You should split this ...


18

A lambda is simply an anonymous function - a function with no name.


18

Bootstrapping is definitely the standard way to build a compiler today. But remember that you don't need a compiler or interpreter to write a program in a language. For instance, Christopher Strachey wrote a famous AI program that was able to play Checkers in CPL before there was a compiler for CPL. He had to translate the program to machine code "manually", ...


17

It certainly isn't the only factor, but I'll point out the popularity of Ruby. Not saying this is more important than any of the six answers already on the board, but I think that many things happened at once and that it's useful to enumerate them all. Ruby is not a functional language and its lambdas, prods, and blocks seem clunky when you've used ...


17

Type inference with type level lambdas would require higher order unification which is undecidable. This is the motivation for disallowing them. But as has happened with other undecidable features (like type inference for GADTs), it might be possible to require type signatures and allow it. I'm not sure if that's been investigated by anyone.


16

Even more, C++ have such functions, take a look to algorithm (or with C++11 additions) header: std::transform std::for_each std::remove_copy_if They can be easily used with any container. For example your code can be expressed like this (with C++11 lambdas for easy coding): std::vector<int> x = {1, 2, 3, 4, 5}; std::vector<int> y; std::...


16

Another advantage of using the functional streaming API is, that it hides implementation details. It only describes what should be done, not how. This advantage becomes obvious when looking at the change that needs to be done, to change from single threaded to parallel code execution. Just change the .stream() to .parallelStream().


14

For Java, yes, it's nothing more than a better way of creating an anonymous inner class. This is because of the fundamental decision in java that every bit of byte code has to live within a specific class, which cannot be changed now after decades of legacy code to consider. However, that is not what lambda expressions really are about. In formalisms where ...


14

It depends. Whenever you find yourself using the same lambda in different places you should consider implementing a class that implements the interface. But if you would've used an anonymous inner class otherwise I think a lambda is far better.


13

As Yannis pointed out, there are a number of factors that have influenced the adoption of high-order functions in languages that were previously without. One of the important items he only touched lightly on is the proliferation of multi-core processors and, with that, the desire for more parallel and concurrent processing. The map/filter/reduce style of ...


13

I guess it's simply a reference to some papers written by Sussman and Steele between 1975 and 1980 called: Lambda: The Ultimate Imperative Lambda: The Ultimate Declarative Lambda: The Ultimate GOTO LAMBDA: The Ultimate Opcode See Wikipedia article.


13

No, this is not generally a good pattern What you're doing is breaking up a function into smaller functions using lambda. However, there is a much better tool for breaking up functions: functions. Lambdas work, as you have seen, but they mean much much much much much more than simply breaking up a function into local bits. Lambdas do: Closures. You can ...


13

If anything, it is harder to read and understand. That is highly subjective. I find the second version much easier to read and understand. It matches how other languages (e.g. Ruby, Smalltalk, Clojure, Io, Ioke, Seph) do it, it requires fewer concepts to understand (it's just a normal method call like any other, whereas the first example is specialized ...


13

I support Karl Bielefeldt's answer, but want to provide a brief addition. Debugging Some IDE's struggle with scope inside of a lambda, and struggle to display member variables inside the context of a lambda. While hopefully this situation will change down the line, it can be annoying to maintain someone else's code when it is littered with lambdas.


12

When Java was first designed it was considered appropriate to leave out anonymous functions. I can think of two reasons (but they might be different from the official ones): Java was designed as an object-oriented language without functions, so it was not very natural to have anonymous functions in a language without functions. Or at least, this would have ...


12

Well, the other answers cover what \() -> "something" means in Haskell: an unary function that takes () as argument. What is a function without arguments? – A value. Actually, it can occasionally be useful to think of variables as nullary functions that evaluate to their value. The let-syntax for a function without arguments (which doesn't actually exist)...


Only top voted, non community-wiki answers of a minimum length are eligible