Questions tagged [lambda]

DO NOT USE FOR THE AWS SERVICE (use [aws-lambda] for those questions!) Lambdas are anonymous functions or closures in programming languages such as Lisp, C#, C++, Lua, Python, Ruby, JavaScript, Java, Excel or Google sheets. (Also, lambda expression.)

This term originated with the lambda calculus, a Turing-complete model of computation which uses only functions, called lambda expressions. They are of the form λ<argument name(s)>.<expression>; the point is that occurrences of the argument <argument name(s)> inside the expression <expression> are substituted with the values of the arguments. An example is λx.x, the identity function.

In programming languages such as , , , and , lambda is an operator used to denote anonymous functions or closures, following the usage of lambda calculus. An anonymous function enables definition of a function without binding to an identifier. Lambda expressions are supported in since version 8, in since version 11.

Android does not currently use Java 8, but Android Studio and other IDEs (IntelliJ Idea, etc.) automagically collapses "Closures" (anonymous classes implementing one method) into lambda expressions.

References

29774 questions
14
votes
4 answers

How do I return a delegate function or a lambda expression in c#?

I am trying to write a method to return an instance of itself. The pseudo code is Func> MyFunc(T input) { //do some work with input return MyFunc; } seems simple enough. But I am having problem defining the return type. The…
Wei Ma
  • 3,125
  • 6
  • 44
  • 72
14
votes
1 answer

Leave short lambda used as a middle argument for a function intact using clang-format

I would like to format the code with clang-format and leave lambda used as a middle function argument intact (as is): void f() { func(0, [] {}, 0); } Everything I was trying in clang-format 9.0 (and 11.0.0-2663a25f as well) wraps arguments to…
αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
14
votes
1 answer

implicit parameter of enclosing lambda is shadowed

val listPlans: List = newPlans.mapTry { it.data.map { Plan(it.id, it.name, it.phone, it.desc, it.email) }.toList() } Kotlin newbie writing code and IntelliJ highglights it in the Plan(it.id,…
Raj
  • 3,637
  • 8
  • 29
  • 52
14
votes
1 answer

Generic lambda vs generic function give different behaviour

Take following code as an example #include namespace baz { template void sort(T&&){} } namespace boot { const auto sort = [](auto &&){}; } void foo (){ using namespace std; using namespace baz; …
bartop
  • 9,971
  • 1
  • 23
  • 54
14
votes
1 answer

template with lambda as unique default parameter on each instantiation

I'm looking for a way to automatically make default template parameter be unique each time a template is instantiated. Since unnamed function objects created by lambda expressions have different types I thought of adopting them somehow. With recent…
user7860670
  • 35,849
  • 4
  • 58
  • 84
14
votes
3 answers

Count the number of arguments in a lambda

I need to know the exact number of arguments that a lambda has. I do not care for their types, I just need a count. auto lambda0 = [&]() { ... }; auto lambda1 = [&](int32_t a) { ... }; auto lambda2 = [&](int32_t a, auto b) { ...…
Rick de Water
  • 2,388
  • 3
  • 19
  • 37
14
votes
6 answers

Check the keys in the map matching with the List content in Java

I have a List of Strings and a Map. Every key in the map needs to present in the list else I need to throw an exception. As of now I am looping the list and checking the key and throw exception if the map doesn't contains the key. Below is the…
sparker
  • 1,666
  • 4
  • 21
  • 35
14
votes
3 answers

LINQ where clause with lambda expression having OR clauses and null values returning incomplete results

the problem in short we have a lambda expression used in the Where clause, which is not returning the "expected" result. quick summary in the analysisObjectRepository object, there are certain objects which also contain the parent relationship in a…
Cihan Kurt
  • 153
  • 1
  • 1
  • 5
14
votes
2 answers

C++0x: Capture By Value for Lambda, always a copy?

Is the compiler allowed to eliminate the copy that is required for the by-value capture? vector movie1; apply( [=movie1](){ return movie1.size(); } ); Is there any circumstance that the compiler does not need to copy movie1? Maybe if the…
towi
  • 21,587
  • 28
  • 106
  • 187
14
votes
1 answer

No suitable method found for ArrayList .toArray(String[]::new) in return statement

I am working on the site Codingbat, specifically this method in AP-1 public String[] wordsWithout(String[] words, String target) { ArrayList al = new ArrayList<>(Arrays.asList(words)); al.removeIf(s -> s.equals(target)); return…
Alex Pickering
  • 151
  • 2
  • 6
14
votes
1 answer

Passing a lambda with moved capture to function

I recently struggled with a bug hard to find for me. I tried to pass a lambda to a function taking a std::function object. The lambda was capturing a noncopyable object. I figured out, obviously some copy must happen in between all the passings. I…
JulianW
  • 897
  • 9
  • 23
14
votes
1 answer

Expanding parameter pack as part of lambda capture in fold expression - gcc vs clang

Consider the following code snippet: template void foo() { ([i = Is]{}(), ...); } clang++ (trunk) successfully compiles the code with -std=c++17 g++ (trunk) fails to compile with the following error: : In function 'void…
Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
14
votes
2 answers

Python: pass "not" as a lambda function

I need to pass a function as a parameter that works as the boolean "not". I tried something like this but it didn't work because not isn't a function. theFunction(callback=not) # Doesn't work :( I need to do the following, but I wonder if there…
germanfr
  • 547
  • 5
  • 19
14
votes
1 answer

CloudWatch logs delayed for a lambda function

It seems that I have a delay with the CloudWatch log of one of my lambda function's. I have a lambda function that gets triggered by a Kinesis stream. The lambda function writes records into a DynamoDB table. I know for sure that the lambda…
kord
  • 979
  • 14
  • 24
14
votes
9 answers

Java 8, Lambda: Sorting within grouped Lists and merging all groups to a list

Based on the following answer: https://stackoverflow.com/a/30202075/8760211 How to sort each group by stud_id and then return a List with all Students as result of the grouping by stud_location and then sorting by stud_id)? It would be great to have…
ThomasMuller
  • 279
  • 1
  • 3
  • 12
1 2 3
99
100