lambda

Write a function that accepts a lambda expression as argument

Hi all, I have a method like this template<typename T, typename U> map<T,U> mapMapValues(map<T,U> old, T (f)(T,U)) { map<T,U> new; for(auto it = old.begin(); it != old.end(); ++it) { new[it->first] = f(it->first,it->second); } return new; } and the idea is that you'd call it like this BOOST_AUTO_TEST_CA...

C# MVC: Func<Table1, "runtime type"> How do I get a dynamic type?

Hey, I'm trying to sort a custom data grid of columns based on what the user clicked. The variable "sort" is being passed to the controller but is just a string saying what column is to be sorted. I need to get the Type of that column to be used in a LambdaExpression... heres the code ParameterExpression param = Expression.Parameter(ty...

Dynamic Expression API (Dynamic.cs) Not properly parsing expression in .net 3.5

Hi I'm having trouble getting this API working in .net 3.5 (works fine in 4.0). Basically I have following code List<ParameterExpression> parameters = new List<ParameterExpression>(); parameters.Add(Expression.Parameter(typeof(double), "R0C6")); parameters.Add(Expression.Parameter(typeof(double), "R0C7")); parameters.Add(Expres...

How to Transform a LINQ Expression when you do not have one of the parameters when you define it.

I'm trying to build more generic query functionality into my application. What I'd like to do is define objects which given an predicate expression can apply that to an iqueryable with a value that will be passed in later. I believe the code below should demonstrate what I'm trying to do well enough to understand the problem. Please let...

lambda expression (MSVC++ vs g++)

I have the following code #include <algorithm> #include <iostream> #include <vector> #include <functional> int main() { typedef std::vector<int> Vector; int sum=0; Vector v; for(int i=1;i<=10;++i) v.push_back(i); std::tr1::function<double()> l=[&]()->double{ std::for_each(v.begin(),v.end(),[&](int n){sum += n; /...

LINQ Help for boolean function for a list

How could I construct a LINQ expression to remove values from one list that meet the criteria of a function that returns a boolean? string[] message = "days of the week" message.ToList().RemoveAll(c=>checkShortWord(c)); public static bool checkShortWord(string word) { if ((word.Length > 3) && (!...

Null-coalescing operator and lambda expression

Hello, take a look at the following code I attempted to write inside a constructor: private Predicate<string> _isValid; //... Predicate<string> isValid = //...; this._isValid = isValid ?? s => true; The code doesn't compile - just "invalid expression term"s and so one. In contrast that does compile and I could just use it: this._...

Is there any way to use C# methods directly as delegates?

This is more of a C# syntax question rather than an actual problem that needs solving. Say I have a method that takes a delegate as parameter. Let's say I have the following methods defined: void TakeSomeDelegates(Action<int> action, Func<float, Foo, Bar, string> func) { // Do something exciting } void FirstAction(int arg) { /* som...

C++0x Lambda overhead

Is there any overhead associated with using lambda expressions in C++0x (under VS2010)? I know that using function objects incurs overhead, but I'm referring to expressions that are passed to STL algorithms, for example. Does the compiler optimize the expression, eliminating what seems to appear like a function call? I started to really...

How to make just part of a macro hygienic

I'd like to have a version of lambda, called lambda-r, from within which you can return. An example: (+ ((lambda-r () (return 1) 2)) 5) This would give the value 6. Although you might expect the value to be 7, it's 6 because 1 is returned from the lambda-expression before 2 is reached. Here's an example of the kind of transfo...

Unable to cast the type 'System.String' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

I am using EF 4 but it is giving me error when I try to order my list. Unable to cast the type 'System.String' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types. This is my code to get the experssion by entering the property name, example below get the Customer Name var param = ...

How to determine the number of accepted arguments in a lambda in ruby

Is there any method or something x in which, provided z = lambda {|x, y, z| nil} we can say z.x #=> 3 Of course, syntax can differ if it gets the job done. Thank you! ...

LINQ .OrderBy single letter then by another letter

Hello Im trying to make a special orderby with Linq. It has to order by S then P then NB then V and then NC. Im not sure if its the best way to de this but this is what i have: repeater.DataSource = query.OrderBy(p => p.Title ?).ThenBy(?).ThenBy(?); ...

ASP.NET How to retrieve a list of the empty labels on a page?

Hi, I have some labels on my Page (e.g. Label1...Label100). I don't want to loop through all Labels to check if Text property is = "" (or string.Empty, whatever), so this's my question - is it possible to use LINQ or Lambda Expression to get all "empty" labels ? ...

Best practices of using lambda expressions for event handlers

After discovering lambda expressions, and their use as anonymous functions, I've found myself writing a lot of more trivial events such as these: txtLogin.GotFocus += (o, e) => { txtLogin.Text = string.Empty; txtLogin.ForeColor = SystemColors.ControlText; }; txtLogin.LostFocus += (o, e) => { txtLogin.Text = "Login..."; t...

How to get the full name of the property from a lambda

I use the following method to create a SelectListItem object from any other object: public static SelectListItem ToSelectListItem<T, TResult, TResult2>(T obj, Expression<Func<T, TResult>> value, Expression<Func<T, TResult2>> text) { string strValue = String.Empty; string strText = String.Empty; ...

Type Casting Part of a string to int in where clause LinQ C#

I have a collection of string names, few names starts with X100,x200,x121 which has numeric values. I'm using LinQ to loop thro and also using a where clause to filter those names that has integer values like x"100." Which is the best way to do accompolish this?. is it possible to use Func or Action on the items for checking each string ...

C++0x Lambda Support in GCC for the iPhone

Can anyone tell me if C++ lambda expressions will be supported by GCC for the iPhone in the future? Obviously Apple have their custom 'block' support so I wondered what this may eventually mean in terms of portable C++0x code to the iPhone platform? ...

C++ for_each calling a vector of callback functions and passing each one an argument

I'm fairly green when it comes to c++0x, lambda, and such so I hope you guys can help me out with this little problem. I want to store a bunch of callbacks in a vector and then use for_each to call them when the time is right. I want the callback functions to be able to accept arguments. Here's my code right now. The trouble is in vo...

Creating Python function with partial parameters.

I want to pass a Python function to another function with some of its parameters "filled out" ahead of time. This is simplification what I am doing: def add(x, y): return x + y def increment_factory(i): # create a function that increments by i return (lambda y: add(i, y)) inc2 = increment_factory(2) print inc2(3) # prints 5...