Hot answers tagged

122 votes
Accepted

Is using Lambda expressions whenever possible in java good practice?

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 ...
Karl Bielefeldt's user avatar
68 votes
Accepted

Is using lambdas to express intent not pythonic?

You're sort of approaching it like a mathematician, where the purpose of writing the supporting functions is to "prove your work." Software isn't generally read that way. The goal is usually ...
Karl Bielefeldt's user avatar
50 votes
Accepted

Why should I use "functional operations" instead of a for loop?

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 ...
luboskrnac's user avatar
36 votes

Is there a performance benefit to using the method reference syntax instead of lambda syntax in Java 8?

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 ...
h.j.k.'s user avatar
  • 1,727
27 votes

Is using lambdas to express intent not pythonic?

Despite the Zen of Python, there is sometimes more than one obvious way to do it. I agree that your preferred way to phrase this code has a certain functional elegance to it. But it's also plain to ...
amon's user avatar
  • 131k
24 votes

Is using lambdas to express intent not pythonic?

"Pythonic" is not an objective standard. It really means "code that an experienced python programmer likes". Turns out "experienced python programmers" don't all ...
Ben's user avatar
  • 1,017
18 votes
Accepted

Is there a performance benefit to using the method reference syntax instead of lambda syntax in Java 8?

In many scenarios, I think lambda and method-reference is equivalent. But the lambda will wrap the invocation target by the declaring interface type. For example public class InvokeTest { ...
JasonMing's user avatar
  • 296
18 votes

Why should I use "functional operations" instead of a for loop?

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 ...
Stefan Dollase's user avatar
17 votes

Is using Lambda expressions whenever possible in java good practice?

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 ...
Jolleyboy's user avatar
  • 279
15 votes

Why should I use "functional operations" instead of a for loop?

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, ...
Jörg W Mittag's user avatar
15 votes

Is using Lambda expressions whenever possible in java good practice?

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 ...
Tohnmeister's user avatar
9 votes

In Java 8, is it stylistically better to use method reference expressions or methods returning an implementation of the functional interface?

In terms of functional programming, what you and your colleague are discussing is point free style, more specifically eta reduction. Consider the following two assignments: Predicate<Result> ...
Jack's user avatar
  • 4,439
8 votes
Accepted

Using lambdas to improve readability of a C++ function

Does it improve readability ? Your way of using lambdas to break-down a larger function in smaller parts is similar to the nested functions in Pascal, ADA and other languages. It indeed improves the ...
Christophe's user avatar
  • 72.5k
8 votes
Accepted

Is it better to use lambda functions or boolean variables to record state

Using these kinds of object-oriented or functional techniques can be super neat and elegant. If you need a fancy name for what you are doing here, I suggest the State Pattern, with function objects ...
amon's user avatar
  • 131k
7 votes

C++11 Lambda vs Helper Member Functions

Both solutions make sense, and have different tradeoffs. Lambdas The style of using lambdas inside another function is very common across many languages. If it seems alien to you, that's just ...
amon's user avatar
  • 131k
7 votes

Is using Lambda expressions whenever possible in java good practice?

Access to local variables of the enclosing scope The accepted Answer by Karl Bielefeldt is correct. I can add one more distinction: Scope The lambda code nested inside a method inside a class can ...
Basil Bourque's user avatar
6 votes

Redex and reduction strategies

In Call by name: I do not understand the need for the definition of the strategy to include no reduction can occur within a lambda abstraction: is that not implied by the fact that only ...
Aahlad Gogineni's user avatar
6 votes
Accepted

Lambda calculus: Call by value / Call by name (lazy)

You want to apply (λz.zz) to the argument (λb.b) Call by value means: reduce the argument to normal form and then bind the parameter z to it Call by name means: replace each occurrence of the ...
Giorgio's user avatar
  • 19.4k
6 votes
Accepted

Server architecture for short bursts of ~150 parallel CPU-heavy subqueries

Since you are CPU-limited, you need to get your hands on 150 CPU cores, one for each thread. This rules out a single server, since a server of such proportions would be prohibitively expensive – and ...
amon's user avatar
  • 131k
6 votes

Efficiency of nameless functions

Anonymous (unnamed) functions are as efficient as named functions. However, nested functions have a slight overhead if they refer to local variables of an enclosing function (such nested functions are ...
amon's user avatar
  • 131k
6 votes

In Java 8, is it stylistically better to use method reference expressions or methods returning an implementation of the functional interface?

Neither of the current answers actually addresses the core of the question, which is whether the class should have a private boolean isResultInFuture(Result result) method or a private Predicate<...
Reinstate Monica's user avatar
5 votes

In Java 8, is it stylistically better to use method reference expressions or methods returning an implementation of the functional interface?

I don't write Java code anymore, but I write in a functional language for a living, and also support other teams who are learning functional programming. Lambdas have a delicate sweet spot of ...
Karl Bielefeldt's user avatar
5 votes
Accepted

Lambda Return Type Inference

Your problem is that you link method calls before the program is fully type-checked. In a complicated language with subtype polymorphism and parametric polymorphism, you cannot do any kind of linking ...
amon's user avatar
  • 131k
5 votes
Accepted

Efficiency of nameless functions

This isn't directly an answer about the efficiency of anonymous functions, but more about the coding style, since I get the impression that this is what the OP really cares about. (Especially pre-edit,...
Sebastian Redl's user avatar
5 votes

Is using Lambda expressions whenever possible in java good practice?

This might be nit-picking, but to all the other excellent points made in other answers, I would add: Prefer Method References when possible. Compare: employees.stream() .map(Employee::...
Matt McHenry's user avatar
4 votes
Accepted

Java 8: Good practice to pass Streams around in APIs for lazy operations?

Laziness in Java 8 Streams works the same as it used to for Iterables in Guava: you have to pass on the Iterable to stay lazy and evaluation happens, once you build a Collection from the Iterator. ...
Robert Jack Will's user avatar
4 votes

What is the difference between a function and a lambda?

TL;DR As others pointed out: the lambda notation is just a way to define functions without being forced to give them a name. Long version I would like to elaborate a bit on this topic because I find ...
Giorgio's user avatar
  • 19.4k
4 votes
Accepted

Explicit type and final in stream lambdas

The final keyword is unnecessary. Not only from a technical perspective as final complex objects can still be manimulated (e.g. call clear on a final list) but also from a theoretical perspective. In ...
Manziel's user avatar
  • 380
4 votes
Accepted

How do the SOLID principles apply in the context of Lambdas and Streams?

I think streams+lambdas sometimes violate the single responsibility principle and sometimes don't. Sequencing of other operations is a "single thing" for the purposes of SRP. That's what ...
Karl Bielefeldt's user avatar
4 votes
Accepted

Capture by value/reference and early/late binding

The lambda defines a closure, which is an anonymous function together with an environment composed of captured variables. The binding of captured variables happens at the moment the lambda is defined ...
Christophe's user avatar
  • 72.5k

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