No, C has no support for lambda expressions.
If you're willing to use C++, Boost has a library that emulates lambdas. Also, C++0x will have built-in support for lambda expressions.
There wasn't a huge demand for lambda expression support in C at the time, so the language didn't support it.
Answer from In silico on Stack OverflowDoes C use lambda expressions? - Stack Overflow
Newest Questions - Stack Overflow
Lambda Expression
[SOLVED] Conditional statement with a Lambda function variable
No, C has no support for lambda expressions.
If you're willing to use C++, Boost has a library that emulates lambdas. Also, C++0x will have built-in support for lambda expressions.
There wasn't a huge demand for lambda expression support in C at the time, so the language didn't support it.
C does not support lambda expressions, nor any other ways (within the language's standard) to dynamically create functions -- all functions, per the standard, are created at compile time. I guess the reason is to keep the language small, simple, lean, and very fast, with hardly any "runtime library" support necessary -- crucial for a language that's so widely used in programming operating systems, device drivers, embedded applications, and so forth.
It looks like you’ve already got it in Lambda form, almost. You just didn’t use the lambda operator “=>”.
It’s an unnamed, inline, expression.
lambdas are usually small things you only use once, and as such you don’t need to declare and name them as functions for use in multiple places.
A lot of the time, you can place them completely on a single line, if that’s your coding style and the function is small enough.
The only things that would make your expression MORE lambda-esque is to place it on a single line and use the lambda operator:
string _result => foreach (char c in inputString) {if (c == 122) _result += "a"; else _result += Convert.ToChar(c+1); } return _result;
But that would be poor practice in this instance.
I’d go with something like this:
string _result => foreach (char c in inputString)
{
if (c == 122)
_result += "a";
else
_result += Convert.ToChar(c+1);
}return _result;
https://www.codeproject.com/Tips/298963/Understand-Lambda-Expressions-in-Minutes
I am still trying to wrap my brain around the lambda expression in C#.
I have the following code:
string _result = “”;
foreach (char c in inputString)
{
if (c == 122)
_result += “a”;
else
_result += Convert.ToChar(c+1);
}
return _result;
I understand this code, and can follow the logic. However, I would like to see this in a lambda expression, hoping this will help me understand.
Can someone please convert this to a lambda expression?
If these are two distinct (reference) types then you can test them using the is or as keywords:
i => {
var x = i as TypeThatHasNameProperty;
return (x != null) ? x.name : i.inner.name;
}
If you can't test for specific types then you can use reflection to test for the name property itself:
i => {
var pi = i.GetType().GetProperty("name");
return (pi != null) ? pi.GetValue(i, null) : i.inner.name;
}
Yes, the conditional operator ("ternary operator") does what you want:
(i => i.name != null ? i.name : i.inner.name)
Assuming, of course, that you can detect the "existence" of the name by checking for null.
Edit: In that case, Kirschstein's answer is better, of course.
Is there any real reason why we cant use lambda-expressions in if-else-statements?
I've seen issues/discussions on github but the main argument made there was that Lambda-expressions are bad because there are ways to archieve the same goals...
Like, this:
if(IsTrue) => DoSomething() else => DoSomeOtherThing();
Is "bad".
But then situations like these exist:
private bool DoSomething() => true
Or
public bool MyProperty => MyField
And those are somehow perfectly fine? Why is it forbidden in an if-statement but permitted in literally ANY OTHER situation?
How is it that switch-statements, which are basically nested if-statements, can work with lambda-expressions, but the most BASIC evaluation process CANT?
If you want to check if any car does not have a door then simply use Enumerable.Any - it determines whether any element of a sequence satisfies a condition:
if (cars.Any(c => c.door == null))
// then ...
Just for fun: you should execute lambda to get boolean result in if condition (but for this case use Any)
Func<bool> anyCarDoesNotHaveDoor = () => {
foreach(var car in cars)
if (car.door == null)
return true;
return false;
};
if (anyCarDoesNotHaveDoor())
// then ...
I introduced local variable to make things more clear. But of course you can make this puzzle more complicated
if (new Func<bool>(() => {
foreach(var car in cars)
if (car.door == null)
return true;
return false; })())
// then ...
Well, the error says it all. An if statement is expecting a boolean expression which a delegate is not. If you were to call the delegate (assuming it returned a bool), you would be fine. However, if does not know to call it.
The easy way to do this is with the Any LINQ extension method:
if (cars.Any(car => car.door == null))
The Any method knows to actually invoke the lambda expression on each member of the collection, and returns a bool. This makes it a valid boolean expression for the if statement.
It would not seem to hard to implement to allow a programmer to use a construct similar to:
int (*add)(int, int) = (int(int x, int y)){return x+y;};
This would simplify code that requires callback functions such as qsort or bsearch or various UI libraries that use callbacks to define, for example, a buttons behavior when pressed. Is there any specific reason they elected not to support this, and require us to define named static functions instead?