lambda

lambda versus list comprehension performance

Hi, I recently posted a question using a lambda function and in a reply someone had mentioned lambda is going out of favor, to use list comprehensions instead. I am relatively new to Python. I ran a simple test: import time S=[x for x in range(1000000)] T=[y**2 for y in range(300)] # # time1 = time.time() N=[x for x in S for y in T if...

Python, lambda, find minimum

I have foreach function which calls specified function on every element which it contains. I want to get minimum from thise elements but I have no idea how to write lambda or function or even a class that would manage that. Thanks for every help. I use my foreach function like this: o.foreach( lambda i: i.call() ) or o.foreach( I.ca...

OrderBy descending in Lambda expression?

I know in normal linq grammar, "orderby xxx descending" is very easy, but how do I do this in Lambda expression? ...

Help with linq query with subqueries

Hi there, can anyone help?, i am stuck with a linq query.. basically i have a standard linq query that returns fields, 1 field (insurances) is actually another linq query like so // original from this in etc not included to keep msg short> select new Models.Custom.House.Insurance() { Id = v.IdH...

Linq continuous date takewhile

Main problem: how do i group elements by their Date, only if continuous and only if they match some properties? Details: Given this type of object: public class MyObj { public DateTime Date { get; set; } public string ConditionalValue1 { get; set; } public int ConditionalValue2 { get; set; } public int OtherValue { g...

How do lambda expressions work internally?

While looking up the answer to this question: "Why is an out parameter not allowed within an anonymous method?" I've got a little lost about how do lambda expression and anonymous methods actually work. In the comments JaredPar states that "Imagine for instance that the out parameter referred to a local variable on the stack. The lambda...

Use Lambda Expression within ASP.NET MVC View using VB.NET

With ASP.NET MVC 1.0, .NET 3.5 and C# you can easily pass a method a lambda expression that will do a "Response.Write" some content when it's executed within the method: <% Html.SomeExtensionMethod( () => { <% <p>Some page content<p> %> } ) %> The method signature is similar to this: public void SomeExtensionMethod(t...

Builder or some other pattern to always create instance in valid state

I have a very complicated setup of objects and each selection along the way limits or expands options available. I would hate to throw exceptions or to create invalid instance of the object. So, I want to limit options (methods available to invoke) when building an instance based on the previous method input paramters. For example if I h...

Using an Anonymous method to populate a property in an object initializer

Assuming sr is an IEnumerable, I want to use code like this to do an inline calculation using two of the items from sr.Lines(). The problem is that the lambda is of type "lambda expression" and not a Decimal, which shares is expecting. Is there any way to do this type of inline method in an object initializer? var trades = from line...

C# Lambda puzzling behaviour

Func<Classification, string> test1 = c => c.Id = "x"; Func<Classification, string> test2 = c => { return c.Id = "x";}; I've worked with lambda's for nearly a year or so now and fairly reasonable with them, but today I was looking at NBuilder and seen a weird Func that didn't seem to match the examples. I had play anyway and it checks ...

C# Lambda Expression Help

If I use a lambda expression like the following // assume sch_id is a property of the entity Schedules public void GetRecord(int id) { _myentity.Schedules.Where(x => x.sch_id == id)); } I'm assuming (although non-tested) I can rewrite that using an anonymous inline function using something like _jve.Schedules.Where(delegate(Mode...

Create a delegate of a generic function

I'm writing some unit tests and I have a lot of functions of the form public void SomeTestHelperMethod<TKey, TValue>(TKey key, TValue value) which I'm calling repeatedly with various arguments like this SomeTestHelperMethod<int, int>(0, 1); SomeTestHelperMethod<int, object>(1, new Nullable<double>(16.5)); SomeTestHelperMethod<int, st...

AsQueriable() or Expression<T>.Compile()?

Edit2: After finally being able to profile the two against each other, it appears that in my situation .AsQueryable() is slightly faster than Expression.Compile(). Original question: I have implemented a cache of some database tables (as List<T>) that I need to query with the same Expression<Func<T, bool>> as I would use when querying a...

Can't use Lambda expressions in Windows services?

I've developed a server software for .net 3.5 which I tried to convert to a windows service application. So I created a new Windows service project and added all my classes. But I can't compile it because it doesn't understand the lambda expressions i've used. I've checked so that all references are imported. But it seems like it isn't p...

Converting a Scheme expression to a string

Given an expression '(lambda (x) x) How can I translate this into a string. I thought symbol->string will do the job but no it cant not a symbol. e.g for a macro to-string: (to-string (lambda(x) x)) this should return >> "(lambda (x) x)" Any ideas folks Thanks ...

lambda calculus for functional programming

in lambda calculus (λ x. λ y. λ s. λ z. x s (y s z)) is used for addition of two Church numerals how can we explain this, is there any good resource the lambda calculus for functional programming ? your help is much appreciated ...

Is converting a NameValueCollection to a querystring using a c# lamdba efficient?

In researching how to convert a NameValueCollection to a querystring, I have come across different methods. I am curious if the shorter lambda syntax is as efficient as it could be. How to convert NameValueCollection to a (Query) String using a iterating function. public static String ConstructQueryString(NameValueCollection parameter...

C#: Getting Names of properties in a chain from lambda expression

I'm developing a API that uses lambda expressions to specify properties. I'm using this famous piece of code similar to this one (this is simplified and incomplete, just to make clear what I'm talking about): public void Foo<T, P>(Expression<Func<T, P>> action) { var expression = (MemberExpression)action.Body; string propertyNam...

dynamically create lambdas expressions + linq + OrderByDescending

how can I create a dynamic lambda expression to pass to use in my orderby function inside linq? I basically want transform "queryResults.OrderByDescending();" in "queryResults.OrderByDescending(myCustomGeneratedLambdaExp);" where myCustomGeneratedLambdaExp shall be a string containning "x => x.name" Thanks ...

jQuery lambda functions

jQuery uses lambda functions extensively wherever a function is to be passed into another function. But Visual Studio 2008 would not honor break points that are set inside a JavaScript lambda function. Anybody knows any workaround for this other than giving an explicit name for the function and passing this to the jQuery functions? ...