Hot answers tagged

77

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 ...


68

I can't find the right post anymore, but Eric Lippert (and possibly several other softies) have opined on several occasions about how Linq is declarative, which, for several classes of problems, is far more intuitive than imperative syntax. Linq enables you to write code that expresses the intent, not the mechanism. You tell me which is easier to read. ...


68

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 ...


62

Co-Worker: Lets be honest here. Linq syntax sucks. It's confusing and non-intuitive. You can't argue with that criticism. For your coworker, it sucks. We failed to design a syntax that, for them, was clear and intuitive. That's our failing, and you can pass on my apologies to your coworker. I am happy to take suggestions on how to make it better; what ...


48

Yes, use them. I am a junior developer, and I know/understand lambdas (and the other concepts you mentioned). There is nothing I could forsee preventing a junior developer from learning all of those concepts in a very short amount of time. Juniors may not have the same amount of experience/expertise when it comes to many gotchas of software development, ...


38

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. ...


36

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 ...


33

Lambda calculus The lambda calculus is a computation model invented by Alonzo Church in the 30s. The syntax and semantics of most functional programming languages are directly or indirectly inspired by the lambda calculus. The lambda calculus in its most basic form has two operations: Abstraction (creating an (anonymous) function) and application (apply ...


31

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 ...


30

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 ...


27

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 ...


26

Guido van van Rossum answered it himself: But such solutions often lack "Pythonicity" -- that elusive trait of a good Python feature. It's impossible to express Pythonicity as a hard constraint. Even the Zen of Python doesn't translate into a simple test of Pythonicity... In the example above, it's easy to find the Achilles heel of the proposed ...


24

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 ...


24

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 ...


21

Like anything else in the programming world, you gotta get used to the syntax, and then it is (potentially) easier to read. Like anything else in the programming world, there is the potential for spaghetti code or other abuses. Like anything else in the programming world, you can either do it this way or another way. Like anything else in the programming ...


21

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 ...


20

If they're a sensible solution to your problem then you should use them. Don't artificially restrict yourself - you're more likely to end up with poor quality code that's hard to maintain. If the code is well written with appropriate comments then those following on should be able to learn from you.


20

The problem seems to be that junior developers and others don't necessarily understand what the function pointer\delegate\lambda function concept is Quite frankly, that's their problem. That's what you ensure they have training for. You can't not use a good technique just because some people might not understand it. If they need help with it, they ...


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 ...


16

Basically, lambda functions are functions you create "on the fly". In C++1x they could be used them to improve on its support for functional programming: std::for_each( begin, end, [](int i){std::cout << i;} ); This will roughly result in code similar to this one: struct some_functor { void operator()(int i) {std::cout << i;} }; ...


16

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", ...


16

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 ...


15

it's perfectly fine to do a multi line lambda in python: see >>> f = lambda x: ( ... x**x) >>> f <function <lambda> at 0x7f95d8f85488> >>> f(3) 27 the real lambda limitation is the fact that lambda must be a single expression; it can't contains keyword (like python2's print or return). GvR choose to do so to limit ...


15

I think the discussion regarding objects vs. functions is a red herring. If the question is, "Is a lambda a function or an object?" the answer should be yes. That's the point of first-class functions: they aren't treated differently than any other type. Java already manages to mostly ignore the differences between Object and primitive types (and Scala ...


14

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; ...


14

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 ...


13

Put a link to the msdn documentation in comments at the top of a block that uses it then go ahead. You shouldn't be scared to use new/complex technology, its part of a developers job to learn things they don't know.


13

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 ...


12

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.


12

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) { ...



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