Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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
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
  • 134k
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,027
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

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
10 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,499
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
  • 77.9k
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
  • 134k
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
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.7k
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
  • 134k
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
  • 134k
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

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

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
  • 77.9k
4 votes
Accepted

Re-architecting CPU intensive Node application to handle multiple users

Nodejs is good for IO intensive tasks but may not be good for CPU intensive tasks. The reason is that it runs on the event loop, which runs on a single thread. There may be several possible ...
lennon310's user avatar
  • 3,222
4 votes

Is using lambdas to express intent not pythonic?

lambdas remove an indication that the definition is a function. I think this makes it harder to read as you have lost information. IDEs work less well you lose their searching and autocompletion of ...
mmmmmm's user avatar
  • 240
4 votes
Accepted

Quiz Site Software Architecture Question

Would these constant queries slow down my application? How much? I've learned the hard way over many years to measure a performance problem first before trying to solve it. Even if you measure a ...
Greg Burghardt's user avatar
3 votes

Are AWS Lambda functions a good fit for the use case of fetching huge amount of records from database on User request?

I believe there is a 6Mb limit on the request/response, which would make it unsuitable. https://docs.aws.amazon.com/lambda/latest/dg/limits.html Ideally you want a resumable stream of data for large ...
Ewan's user avatar
  • 76.4k
3 votes

Using lambdas to improve encapsulation

In C# 7.0, local functions will be introduced which achieve the same as I outlined in the question, but with a much improved syntax. Sometimes a helper function only makes sense inside of a single ...
Steven Jeuris's user avatar
3 votes
Accepted

Unknown number of arguments in currying

I recently answered a question on S.O regarding this exact situation. You can't do this with traditional functions in Python. You can do this by taking advantage of callables though, overloading the ...
Dimitris Fasarakis Hilliard's user avatar
3 votes

Efficiency of nameless functions

No, it is typically less efficient. When you use an inline function, you end up creating a function object. The construction of that object will typically take up some resources. In fact, there is an ...
Winston Ewert's user avatar
2 votes

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

I would design my interface and task master under two assumptions: Processing power is distributed I don't always have enough processing power available If the jobs are inherently long, and the ...
svidgen's user avatar
  • 14.7k
2 votes

Make lambdas concise using enumerations?

In Java, the idiomatic way to group a bunch of functions is just a plain old class with plain old methods. Then your logFormatList looks something like List.of(LogFormatFunctions::date, ...
Karl Bielefeldt's user avatar

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