lambda

Get method name and type using lambda expression

I am trying to write a function that will pull the name of a property and the type using syntax like bellow private class SomeClass { Public string Col1; } PropertyMapper<Somewhere> propertyMapper = new PropertyMapper<Somewhere>(); propertyMapper.MapProperty(x => x.Col1) Is there any why to pass the property through to the functi...

How do I pronounce "=>" as used in lambda expressions in .Net

I very rarely meet any other programmers! My thought when I first saw the token was "implies that" since that's what it would read it as in a mathematical proof but that clearly isn't its sense. So how do I say or read "=>" as in:- IEnumerable<Person> Adults = people.Where(p => p.Age > 16) Or is there even an agreed way of saying it...

Lambdas in Boo?

How do you deal with lambdas in boo? Is "callable" the same thing? How do you define a method that takes a lambda as a parameter? ...

Extract Expression Tree From Action

I'm just starting out playing around with Linq Expressions and I've hit a wall. I need to create an Expression Tree from an Action. Unfortunetly I can't get the Action as an Expression, this is basically what I've got to work with: public void Something(Action action){} I need access to the body of the Action to extract variables an...

How do I create an expression tree to represent 'String.Contains("term")' in C#?

I am just getting started with expression trees so I hope this makes sense. I am trying to create an expression tree to represent: t => t.SomeProperty.Contains("stringValue"); So far I have got: private static Expression.Lambda<Func<string, bool>> GetContainsExpression<T>(string propertyName, string propertyValue) { v...

Most efficient way to test equality of lambda expressions

Given a method signature: public bool AreTheSame<T>(Expression<Func<T, object>> exp1, Expression<Func<T, object>> exp2) What would be the most efficient way to say if the two expressions are the same? This only needs to work for simple expressions, by this I mean all that would be "supported" would be simple MemberExpressions, eg c =>...

Dictonaries and Lambda inside a class?

How can i do something like this: class Foo(): do_stuff = { "A" : lambda x: self.do_A(x), "B" : lambda x: self.do_B(x) } def __init__(self): print "hi" def run(self): muh = ['A', 'B', 'A'] for each in muh: self.do_stuff[each](each) def do_A(self, moo): print "A" def do_B(self, boo): print "B" if(__name__ == '__main_...

Lambda expression exercise

I have been trying to learn more about lambda expressions lately, and thought of a interesting exercise... is there a way to simplify a c++ integration function like this: // Integral Function double integrate(double a, double b, double (*f)(double)) { double sum = 0.0; // Evaluate integral{a,b} f(x) dx for(int n = 0 ; n <...

Simple text menu in C++

I am writing a silly little app in C++ to test one of my libraries. I would like the app to display a list of commands to the user, allow the user to type a command, and then execute the action associated with that command. Sounds simple enough. In C# I would end up writing a list/map of commands like so: class MenuItem { ...

C#: delegate keyword vs. lambda notation

Once it is compiled, is there a difference between: delegate { x = 0; } and () => { x = 0 } ? ...

Can you use LINQ or lambdas to perform matrix operations?

I know how to do this using for loops. Is it possible to do something like this using LINQ or lambdas? int[] a = { 10, 20, 30 }; int[] b = { 2, 4, 10 }; int[] c = a * b; //resulting array should be { 20, 80, 300 } ...

boost lambda for_each / transform puzzle

Does anybody know why vector<int> test(10); int a=0; for_each(test.begin(),test.end(),(_1+=var(a),++var(a))); for_each(test.begin(),test.end(),(cout << _1 << " ")); cout << "\n" Gives : "0 1 2 3 4 5 6 7 8 9" but transform(test.begin(),test.end(),test.begin(), (_1+=var(a),++var(a))); ...(as before) Gives : "1 2 3 ...

What's so great about Func<> delegate?

Hi, Sorry if this is basic but I was trying to pick up on .Net 3.5. Question: Is there anything great about Func<> and it's 5 overloads? From the looks of it, I can still create a similar delgate on my own say, MyFunc<> with the exact 5 overloads and even more. eg: public delegate TResult MyFunc<TResult>() and a combo of various overl...

How do I generate a compiled lambda with method calls?

I'm generating compiled getter methods at runtime for a given member. Right now, my code just assumes that the result of the getter method is a string (worked good for testing). However, I'd like to make this work with a custom converter class I've written, see below, "ConverterBase" reference that I've added. I can't figure out how t...

Generate lambda with Reflection Info

I have Enitity Type, Name of Primary Key and Guid of Primary Id. I want to get element of such Id in LinqToSql. model.GetTable<T>().Where(t => here equality ); I think I need to generate that Expression myself, but I dont know how :( ...

How do I set a field value in an C# Expression tree?

Given: FieldInfo field = <some valid string field on type T>; ParameterExpression targetExp = Expression.Parameter(typeof(T), "target"); ParameterExpression valueExp = Expression.Parameter(typeof(string), "value"); How do I compile a lambda expression to set the field on the "target" parameter to "value"? ...

Primitive recursion

how will i define the function 'simplify' using primitive recursion? simplify :: Expr -> Expr ... simplify Simplify an expression using basic arithmetic, e.g. simplify (Plus (Var "x") (Const 0)) = Var "x" ...

Implementing event conditions in a C++ state machine

I'm using an hierarchical FSM for an embedded C++ application interface. I'd like to use small functions to determine whether certain inter-state events can be triggered, as well as use them to effect changes in the database: however, making a new class with different event functions for each state is daunting, as well as setting pointer...

C# Syntax - Example of a Lambda Expression - ForEach() over Generic List

First, I know there are methods off of the generic List<> class already in the framework do iterate over the List<>. But as an example, what is the correct syntax to write a ForEach method to iterate over each object of a List<>, and do a Console.WriteLine(object.ToString()) on each object. Something that takes the List<> as the first a...

Lambdas with captured variables.

Consider the following line of code: private void DoThis() { int i = 5; var repo = new ReportsRepository<RptCriteriaHint>(); // This does NOT work var query1 = repo.Find(x => x.CriteriaTypeID == i).ToList<RptCriteriaHint>(); // This DOES work var query1 = repo.Find(x => x.CriteriaTypeID == 5).ToList<RptCr...