Check if two Python functions are equal

The one thing you could test for is code object equality: >>> x = lambda x: x >>> y = lambda y: y >>> x.__code__.co_code ‘|\x00\x00S’ >>> x.__code__.co_code == y.__code__.co_code True Here the bytecode for both functions is the same. You’ll perhaps need to verify more aspects of the code objects (constants and closures spring … Read more

Local variable and expression trees

Capturing a local variable is actually performed by “hoisting” the local variable into an instance variable of a compiler-generated class. The C# compiler creates a new instance of the extra class at the appropriate time, and changes any access to the local variable into an access of the instance variable in the relevant instance. So … Read more

FindAll vs Where extension-method

Well, FindAll copies the matching elements to a new list, whereas Where just returns a lazily evaluated sequence – no copying is required. I’d therefore expect Where to be slightly faster than FindAll even when the resulting sequence is fully evaluated – and of course the lazy evaluation strategy of Where means that if you … Read more

Comparator.comparing(…) of a nested field

You can’t nest method references. You can use lambda expressions instead: return Comparator .comparing(l->l.getCourse().getTeacher().getAge(), Comparator.reverseOrder()) .thenComparing(l->l.getCourse().getStudentSize()); Without the need for reverse order it’s even less verbose: return Comparator .comparing(l->l.getCourse().getTeacher().getAge()) .thenComparing(l->l.getCourse().getStudentSize()); Note: in some cases you need to explicitly state the generic types. For example, the code below won’t work without the <FlightAssignment, LocalDateTime> before comparing(…) … Read more

Lifetime of lambda objects in relation to function pointer conversion

§5.1.2/6 says: The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has … Read more

‘Delegate ‘System.Action’ does not take 0 arguments.’ Is this a C# compiler bug (lambdas + two projects)?

FINAL UPDATE: The bug has been fixed in C# 5. Apologies again for the inconvenience, and thanks for the report. Original analysis: I can reproduce the problem with the command-line compiler. It certainly looks like a bug. It’s probably my fault; sorry about that. (I wrote all of the lambda-to-delegate conversion checking code.) I’m in … Read more