lambda

Need somebody to help explain the following code (lambda expressions)

int sum0 = 0; for (int i = 0; i < 10; i++) { sum0 += i; } int sum1 = Enumerable.Range(0, 10).Sum(); int sum2 = Enumerable.Range(0, 10).Aggregate((x, y) => x + y); int sum3 = Enumerable.Range(0, 10).Aggregate(0, (x, y) => x + y); All of the above 4 expressions are doing the same thing: find sum from 0 to 10. I understand the calcul...

Help with OR logic in Linq lambda query

I'm a bit stuck on the following linq query. I'm trying to find all orders which have a state other than "Completed", or which have been completed within the last month. _ordersRepository.GetAllByFilter( o => o.OrderStatus .OrderByDescending(os => os.StatusUpdate.Date) .First() // so at this point I ha...

Python's lambda iteration not working as intended

In the code below I intend to have two buttons, and when each is pressed '0' and '1' are to be printed to stdout, respectively. However when the program is run, they both print '1', which is the last value i had in the for iteration. Why? import Tkinter as tk import sys root = tk.Tk() for i in range(0,2): cmd = lambda: sys.stdout...

Should lambda decay to function pointer in templated code?

I read somewhere that a lambda function should decay to function pointer if the capture list is empty. The only reference I can find now is n3052. With g++ (4.5 & 4.6) it works as expected, unless the lambda is declared within template code. For example the following code compiles: void foo() { void (*f)(void) = []{}; } But it do...

How can I use linq to return integers in one array that do not match up with an integer property of another array?

I have the following method signature: internal static int[] GetStudentIDsThatAreNotLinked(PrimaryKeyDataV1[] existingStudents, IQueryable<Student> linkedStudents) PrimaryKeyData is a class that has ServerID and LocalID integers as properties. Student is a class that (among other properties) has an integer one called StudentID...

What is the type of a lambda function?

In C++0x, I'm wondering what the type is of a lambda function. Specifically: #include<iostream> type1 foo(int x){ return [x](int y)->int{return x * y;}; } int main(){ std::cout<<foo(3)(4);//would output 12 type2 bar = foo(5); std::cout<<bar(6);//would output 30 return 0; } What do I need to replace type1/type2 with to get the...

In C# is there a way retrieve only built-in data type properties using reflection

Using reflection I'd like to retrieve only the built-in data type properties from a C# object. Is there a better way to do that then using a bunch of || (ors) in a Where method specifying the types I am interested in? Type sourceType = typeof(TSource); var props = sourceType.GetProperties() .Where(pi => pi.PropertyType == typeof(in...

C++0x - lambda expression does look same as Java's anonymous inner class?

Is my interpretation of lambda expression in the context of c++ and Java is correct? ...

Optimising Lamda Linq to SQL query with OrderBy

I have the following lamda expression: IEnumerable<Order> query = _ordersRepository.GetAllByFilter( o => o.OrderStatus.OrderByDescending(os => os.Status.Date).First() .Status.StatusType.DisplayName != "Completed" || o.OrderStatus.OrderByDescending...

C#: using the iterator variable of foreach loop in a lambda expression - why fails?

Consider the following code: public class MyClass { public delegate string PrintHelloType(string greeting); public void Execute() { Type[] types = new Type[] { typeof(string), typeof(float), typeof(int)}; List<PrintHelloType> helloMethods = new List<PrintHelloType>(); foreach (var type in types) ...

Why won't `let` work for naming internal recursive procedures?

Consider the following implementation of a function to compute factorial: [1] (define fac-tail (lambda (n) (define fac-tail-helper (lambda (n ac) (if (= 0 n) ac (fac-tail-helper (- n 1) (* n ac))))) (fac-tail-helper n 1))) I attempted to rewrite using let for the inner define: (define f...

lambda expression for exists within list

if i want to filter a list of object against a specific id, i can do this: list.Where(r => r.Id == idToCompare); what if, instead of a single idToCompare, i have a list of Ids to compare against. what is the syntax for comparing against a predefined list. something like int[] listofIds = GetListofIds(); list.Where(r => r.Id "in...

Is there a bug with nested invoke of LambdaExpression?

I tried to compile and calculate LambdaExpression like: Plus(10, Plus(1,2)) But result is 4, not 13. Code: using System; using System.Linq.Expressions; namespace CheckLambdaExpressionBug { class Program { static void Main(string[] _args) { ParameterExpression p1 = Expression.Parameter(typeof (...

SubSonic SimpleRepository Find Method with nullable DateTime

using SubSonic3 SimpleRepository; I have a table used for an email queue which has a SentOn DATETIME column that allows nulls. Using the following Lambda expressions have yielded me errors, does anyone have any ideas how to select a list from the table were the column is null. IList<Email> emails = _repo.Find<Email>(x => x.SentOn == n...

How to raise PropertyChanged event without using string name

It would be good to have ability to raise 'PropertyChanged' event without explicit specifying the name of changed property. I would like to do something like this: public string MyString { get { return _myString; } set { ChangePropertyAndNotify<string>(val=>_myString=val, value); } ...

How do I neatly remove nodes from an XDocument using LINQ and Lamda?

What's the most elegant way of going from this: <?xml version="1.0" encoding="UTF-8"?> <foo> <bar baz="wii" date="2009-01-01"> <baz value="0" /> <baz value="1" /> </bar> <bar baz="wii" date="2009-01-02"> <baz value="0" /> <baz value="1" /> </bar> <bar baz="xbox" date="2009-01-01"> <baz value="0" /> <baz value="1"...

Filter Custom Dictionary with LINQ ToDictionary - "Unable to cast object of type 'System.Collections.Generic.Dictionary`2"

I have created a Dictionary class (MyDictionary for the example). I am currently trying to pass MyDictionary into a function, filter it into a new instance of MyDictionary and pass this new instance into another method. When I am attempting to create the second instance from the filtered first instance of MyDictionary via Lambda Expres...

Linq expression replace parameter type

I have a predicate, that was made from lambda expression after all extension methods were served. For example: (new List<string>).Where(i => i.Contains("some")).Where(i => i.Contains("second_some")); "List<>" is only example, there could be my custom data context or object collection. So, I have an "Expression<...>", and it's base typ...

What to do with template typenames for optional arguments?

First of all, the code: template<typename Func, typename Func2> void ForEachField(Func normalHandler, Func2 arrayHandler = NULL, bool skipUnknowns = true) { for(int i = 0; i < mFields.size(); ++i) { Field *f = mFields[i]; if(skipUnknowns && f->IsUnknown()) continue; if(f->GetCount() == 1 || ...

How to get last x records from a list with Lamdba

I have as List of strings with where i remove each duplicates, now I want to filter it even more to get the last 5 records. How can I do this? What I got so far List<string> query = otherlist.Distinct().Select(a => a).ToList(); ...