lambda

Is there a way to add an Extension Method to a Lambda?

In this example I want to add a .loop({quantity},{sleepvalue}) to a method I got it to work with this: this.loop(count, 500, ()=>{ var image = Screenshots.getScreenshotOfDesktop(); pictureBox.load(image); images.Add(image); updateTrackBar(); }); using this extension method: public static void loop(this Object _object...

c# exit generic ForEach that use lambda

Does anyone know if it is possible to exit a generic ForEach that uses lambda? e.g. someList.ForEach(sl => { if (sl.ToString() == "foo") break; // continue processing sl here // some processing code } ); This code itself won't compile. I know I could use a regular foreach but for consistency I want to use lamb...

c++ transform with if_then_else control structure

I'm trying to change the integer values in a vector using transform and an if_then_else control structure from Boost Lambda. However my compiler is not appreciating my efforts. The code I am attempting is: transform(theVec.begin(), theVec.end(), theVec.begin(), if_then_else(bind(rand) % ratio == 0, _1 = bind(rand) % maxSize, ...

extracting method name from linq expression

Hi All, How can I get the name of the first method called from an expression in C#? Something like the fictional MethodUtils.NameFromExpression() below: Expression<Action<string>> expr = s => s.Trim(); Assert.AreEqual("Trim", MethodUtils.NameFromExpression(expr)); Ideally any util method would be written/overloaded in such a way tha...

Ambiguous Call with a Lambda in C# .NET

I have a class with an overloaded method: MyClass.DoThis(Action<Foo> action); MyClass.DoThis(Action<Bar> action); I want to pass a lambda expression to the Action version: MyClass.DoThis( foo => foo.DoSomething() ); Unfortunately, Visual Studio rightly cannot tell the difference between the Action<Foo> and Action<Bar> versions, due...

linq to entity - include with lambda expression

I have a lite problem i don't really know how to fix. In my example below i would like to select a list of ProductCategories with ProductItems that are active. public IEnumerable<ProductCategory> ListProductCategories() { return _entities.ProductCategorySet.Include("ProductItems").Where(x => x.ProductItems.Active == ...

python mapping with strings

I implemented a version of the str_replace function available in php using python. Here is my original code that didn't work def replacer(items,str,repl): return "".join(map(lambda x:repl if x in items else x,str)) test = "hello world" print test test = replacer(test,['e','l','o'],'?') print test but this prints out hello world ...

C# Lambdas: How *Not* to Defer "Dereference"?

I'm trying to implement undo functionality with C# delegates. Basically, I've got an UndoStack that maintains a list of delegates implementing each undo operation. When the user chooses Edit:Undo, this stack pops off the first delegate and runs it. Each operation is responsible for pushing an appropriate undo delegate unto the stack. So ...

Use property type in expression

I have a function which creates a different type of expression depending on the value of the variable passed in. Protected Function BuildSortPredicate(ByVal tableType As Object, ByRef expr As Expression) Dim sortExpression As Expression If tableType Is "Job" Then sortExpression = Expression.Lambda(Of Func(Of Job, String)...

Lambda Expression: == vs. .Equals()

This is a purely academic question, but what's the difference between using == and .Equals within a lambda expression and which one is preferred? Code examples: int categoryId = -1; listOfCategories.FindAll(o => o.CategoryId == categoryId); or int categoryId = -1; listOfCategories.FindAll(o => o.CategoryId.Equals(categoryId)); ...

How to create a lambda expression for a nested generic in C#?

I have a data structure defined as: Dictionary<Guid, List<string>> _map = new Dictionary<Guid, List<string>>(); I'm trying to create a lambda expression that given a string, returns a IEnumerable of Guids associated with any List<string> containing that string. Is this reasonable/possible or should I use a more appropriate data...

Lexical scoping in C# lambda/anonymous delegates

I want to check whether a simple mathematical expression would overflow (using checked and catch(OverflowException)), but without the need to use a try-catch block every time. So the expression (not the result!) should be passed to a function checkOverflow, which then acts accordingly in case of an overflow. This is what I attempted, bu...

Fun with Lambdas

Not having them used them all that much I'm not quite sure all that lambdas/blocks can be used for (other than map/collect/do/lightweight local function syntax). If some people could post some interesting but somewhat understandable examples (with explanation). preferred languages for examples: python, smalltalk, haskell ...

Python Lambda behaviour

I'm trying to get my head around lambda expressions, closures and scoping in Python. Why does the program not crash on the first line here? >>> foo = lambda x: x + a >>> foo(2) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <lambda> NameError: global name 'a' is not defined >>> a = ...

Deferred evaluation in python

I have heard of deferred evaluation in python (for example here), is it just referring to how lambdas are evaluated by the interpreter only when they are used? Or is this the proper term for describing how, due to python's dynamic design, it will not catch many errors until runtime? Or am I missing something entirely? ...

What do (lambda) function closures capture in Python?

Recently I started playing around with Python and I came around something peculiar in the way closures work. Consider the following code: adders= [0,1,2,3] for i in [0,1,2,3]: adders[i]=lambda a: i+a print adders[1](3) It builds a simple array of functions that take a single input and return that input added by a number. The funct...

Weak reference callback is not called because of circular references

I'm trying to write a finalizer for Python classes that have circular references. I found out that weak reference callbacks are the way to go. Unfortunately, it seems the lambda I use as a callback is never called. For example, running this code: def del_A(name): print('An A deleted:' + name) class A(object): def __init__(self,...

Is it possible to refactor the following Expression

I am fairly new to using the Expression namespace and was wondering if anyone can share their knowledge. I have created a generic class RuleSet which has the following method. I am using the method to register a function against a property that belongs to type T, this check isn't in place, after extracting the property i invoke the rul...

Linq Expression Syntax - How to make it more readable?

I am in the process of writing something that will use Linq to combine results from my database, via Linq2Sql and an in-memory list of objects in order to find out which of my in-memory objects match something on the database. I've come up with the query in both expression and query syntax. Expression Syntax var query = order.Items.Jo...

Dynamically Build Linq Lambda Expression

Okay, my guess is this is answered somewhere already, and I'm just not quite familiar enough with the syntax yet to understand, so bear with me. The users of my web app need to filter a long list of items in a gridview, accessed via a linqdatasource. I am using the OnSelecting Event to further filter items. I want to filter those item...