lambda

Haskell: Scope of variable when using lambda expression with bind functions

The following line works as expected, but I am a little concerned why: getLine >>= \x-> getLine >>= \y-> return [x, y] Consider the addition of parenthesis to scope the lambda expressions: getLine >>= (\x-> getLine) >>= (\y-> return [x, y]) The second line is errorneous because x is not in scope when used in the return, and I am ha...

BinaryExpression to Lambda

This may be familiar to some. I have a wrapper class Ex that wraps an expression tree with a bunch of implicit conversions and operators. Here is simplified version public class Ex { Expression expr; public Ex(Expression expr) { this.expr = expr; } public static implicit operator Expression(Ex rhs) { return...

Getting object array containing the values of parameters from a Lambda Expression

I'm writing a function that will be passed a lambda expression, and I want to convert the parameters that the lambda takes to an object array. The only way I've been able to do it is with code I borrowed from here, and my function looks something like this: public class MyClassBase<T> where T : class { protected void DoStuff(Expre...

In C#, why Expression Trees and when do you need to use them?

When I need the Expression Trees ? And please provide us with a real world sample if available Thanks in advance ...

Looking inside a lambda / expression tree

Given either of the variables below, how can I 'inspect' them to obtain the parameter value passed in? i.e. 12345. System.Func<DateTime> f1 = () => DateTime.Now.AddDays(12345); System.Linq.Expressions.Expression<System.Func<DateTime>> f2 = () => DateTime.Now.AddDays(12345); Edit: The two answers below by Mika...

Using boost to create a lambda function which always returns true

Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)> predicate ); If I want to call it with a predicate that always returns true, I can define a helper function: bool AlwaysTrue( int, int, int ) { return true; } ... Foo( boost::bind( AlwaysTrue ) ); But is there anyway to call t...

Problem accessing association from the result of a lambda query

Has anyone had problems gettting associations to load using LINQ to SQL when your child record was loaded via a lambda query? For example: var orderLine = db.OrderLines. Where(ol => ol.ID == orderLineID select ol). First(); // navigate to order via the association var order = orderLine.GetOrder(); What I get basically is a nul...

Select a model property using a lambda and not a string property name.

I'm building a list of properties of a type to include in an export of a collection of that type. I'd like to do this without using strings for property names. Only certain properties of the type are to be included in the list. I'd like to do something like: exportPropertyList<JobCard>.Add(jc => jc.CompletionDate, "Date of Completion...

Lambda casting to base interface when in generic class

In my generic abstract class class SomeClass<T> where T : ISomeInterface I have a method that calls my repository and pass a parameter Expression<Func<T, bool>> parameter to tell him the 'where' condition. As T implements ISomeInterface, the DebugView of the lambda passed brings me something like ...((ISomeInterface)$p).SomeInterfac...

Using boost to create a lambda function which always throws

Is it possible to create an inline lambda using boost which always throws an exception? (this question follows on from "Using boost to create a lambda function which always returns true"). Suppose I have a function which takes some form of predicate: void Foo( boost::function<bool(int,int,int)> predicate ); If I want to call it with...

Can lambda functions be templated?

In c++0x is there a way to template a lambda function? Or is it inherently too specific to be templated? I understand that I can define a classic templated class/functor instead but the question is more like : does the language allow templating lambda functions? ...

Is is feasible to try to convert expression trees between business and data domains?

I have a repository layer that deals with LINQ to SQL autogenerated entities. These eventually get mapped into domain-friendly types on the surface. I'd now like to provide some more sophisticated querying capabilities for the client code, and that client code knows only about the domain object types. I'd like to implement this with t...

Variable parameters in C# Lambda

Is it possible to have a C# lambda/delegate that can take a variable number of parameters that can be invoked with a Dynamic-invoke? All my attempts to use the 'params' keyword in this context have failed. ** UPDATE WITH WORKING CODE FROM ANSWER ** delegate void Foo(params string[] strings); static void Main(string[] args) ...

MVC2 issues when edit an item on an textboxfor

Hello, i have this model on mvc: public class User { public string Name { get; set; } public IList<string>RelatedTags { get; set; } } And the following typed view (user) to edit an add a user (AddEdit.aspx view): <div> <%: Html.LabelFor(e => e.Name)%> <%: Html.TextBoxFor(e => e....

Create a python function at runtime to match a variable number of dictionary entries

I'm making a program to calculate latency from a tcpdump/pcap file and I want to be able to specify rules on the command line to correlate packets -- i.e. find the time taken between sending a packet matching rule A to receiving a packet matching rule B (concrete example would be a FIX NewOrderSingle being sent and a corresponding FIX Ex...

Passing properties as parameters

I want to create a generalized helper method for LoadFromXML loading and validation. If the XML I'm loading from is incomplete, I do want it to fail completely without throwing an exception. Currently, my code looks like this (more or less) public override bool Load(XElement source) { return new List<Func<XElement, bool>> { ...

Any way to return an object which does not exist yet but will later from a C# method?

Disclaimer 1: Crazy pedantic language-bending drivel ahead. Disclaimer 2: To any clients present or future - I am not billing you for this. Alright, so this is not really necessary but I'm messing around with creating plugins for xunit.net and an interesting scenario comes up Currently, the example of the SubSpec extension that shi...

How i can make dynamic lambda Expression from string?

Dear all, I need use Lambda Expression in my method public static class QueryableDynamicExtension { public static IQueryable<T> DynamicEquals<T>( this IQueryable<T> query, string field, object value) { Expression<Func<T, bool>> expr = ??? return query.Where(expr); } }...

How to use Linq/Lambda with ObservableCollection<T>

I have the following code that simply loops looking for a condition and places all matches into a new collection: ObservableCollection<Device> allDevices = GetAllDevices(); ObservableCollection<Device> matchingDevices = new ObservableCollection<Device>(); foreach (Device device in allDevices ) { if (device.ID != 5) matchingD...

How to use MethodCallExpression with method arguments

Hello, I'm using MethodCallExpression to record method calls. public void RegisterInvocation<TSource>(TSource target, Expression<Action<TSource>> selector) { ... } Somewhen later I execute the expression like this: selector.Compile().Invoke(); And here I have a strange effekt (perhaps I missunderstand something with Method Call...