Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to create a single multi-type collection of multi-type lambda functions in C# 4.0?

For instance, something like:

Dictionary<string, Func<T1, T2, bool>> comparisons;
    comparisons.add("<", (x, y) => x < y);
    comparisons.add("==", (x, y) => x == y);
    comparisons.add(">", (x, y) => x > y);

At this point, I don't know enough about C# lambdas and multi-type generic containers to be able to put this together correctly. Is this even possible?

like image 827
Brett Rossier Avatar asked May 03 '11 20:05

Brett Rossier


People also ask

Can you have multiple lambda functions?

Serverless applications usually consist of multiple Lambda functions. Each Lambda function can use only one runtime but you can use multiple runtimes across multiple functions. This enables you to choose the best runtime for the task of the function.

Can a lambda statement have more than one statement?

The body of a statement lambda can consist of any number of statements; however, in practice there are typically no more than two or three.

Can lambda have multiple inputs?

Create a Lambda function lambda : The key word of any lambda function. arguments : Input arguments of the function. The function allows multiple input arguments. expression : What the function will do in a single expression.

How many lambda functions can you have?

The default concurrency limit per AWS Region is 1,000 invocations at any given time. The default burst concurrency quota per Region is between 500 and 3,000, which varies per Region. There is no maximum concurrency limit for Lambda functions.


1 Answers

Yes, it's perfectly valid to have something like this:

Dictionary<string, Func<int, int, bool>> comparisons = new Dictionary<string, Func<int, int, bool>>();

comparisons.Add("<", (x, y) => x < y);
comparisons.Add("==", (x, y) => x == y);
comparisons.Add(">", (x, y) => x > y);

In your example though, you need to be using Func<int, int, bool>, since you take two parameters and return a boolean value.

You could also put this in a generic implementation, but then you'd need some way of constraining it so that anything must implement <, ==, and > operators.

like image 77
Tejs Avatar answered Oct 30 '22 22:10

Tejs