lambda

Use value of variable in lambda expression

a = [] a.append(lambda x:x**0) a.append(lambda x:x**1) a[0](2), a[1](2), a[2](2)... spits out 1, 2, 4, ... b=[] for i in range(4) b.append(lambda x:x**i) b[0](2), b[1](2), b[2](2)... spits out 8, 8, 8, ... In the for loop, the i is being passed to lambda as a variable, so when I call it, the last value of i is used instead of t...

Determining if a concrete value or expression passed?

Is there anyway to determine if a value is passed as a reference, eg. x.Age or a specific value. eg. 20 like so. value(x => x.Age) or value(x => 20) Cheers ...

Func<TObject, bool> or Predicate<TObject>?

I've seen time and time again API (particularly in the .NET framework) that uses Func<TObject, bool> when Predicate<TObject> is seemingly a perfectly responsible option. What good reasons might an API designer have for doing so? ...

von-Neumann machines and Lambdas

Brian's premise in his argument to the question "Are side effects a good thing?" is interesting: computers are von-Neumann machines that are designed to work well with effects (rather than being designed to work well with lambdas) I am confused by the juxtaposition of the approaches. I cannot see them as black and white. What is th...

What is the difference between new Action() and a lambda?

So when I write something like this Action action = new Action(()=>_myMessage = "hello"); Refactor Pro! Highlights this as a redundant delegate creation and allows me to to shorten it to Action action = () => _myMessage="hello"; And this usually works great. Usually, but not always. For example, Rhino Mocks has an extension metho...

Blocks of code in python

Can you elaborate on the current state of "blocks" (in the Ruby sense) in Python? What are the language constructs that exist in python? How do they compare to other languages (like Ruby, Smalltalk, [insert more])? Or does python lack of such constructs? I have so far understood the lambda thing; it is only one-line, but maybe it comes...

converting a .net Func<T> to a .net Expression<Func<T>>

Going from a lambda to an Expression is easy using a method call... public void GimmeExpression(Expression<Func<T>> expression) { ((MemberExpression)expression.Body).Member.Name; // "DoStuff" } public void SomewhereElse() { GimmeExpression(() => thing.DoStuff()); } But I would like to turn the Func in to an expression, only i...

How can I transform this Lambda Expression into SQL statement?

Hi everyone!! I have a lambda expression that has this: Convert.ToDateTime(a.startTime).TimeOfDay >= Convert.ToDateTime(startTime).TimeOfDay But, I have to create a procedure in SQL Server and how should be the statement above to SQL statement? I've tried to use some kinda 'convert(startime, getdate(),8) but it didn't work. And I f...

Problem declaring an anonymous method with vb.net Action(Of T) and lambda.

Imports System.Reflection Public Class Test Private Field As String End Class Module Module1 Sub Main() Dim field = GetType(Test).GetField("Field", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance) Dim test = New Test Dim GetValue = New Func(Of Test, String)(Function(t As Test) fie...

How to call the method from a MethodCallExpression in c#

Hello, I have a method call expression and try to invoke the method. I figured out a way, but I have problems in retrieving the parameter values since not every argument is described with a ConstantExpression. Expression<Action<T>> = t => t.DoSomething(Par0, Par1, Par2); MethodCallExpression methodCallExpression = selector.Body as Meth...

Is there a generic system delegate that takes 2 types and returns the second

I am looking for something like Action but I want it to be delegate U ReturnAction<T,U>(T param); Is there already a system delegate for this? I just don't want to reinvent the wheel. I did the same thing when I first needed a Predicate and then realized it existed. ...

c# lamba expression - add delegate results to generic list

Question: I have just wrote my first code using c# lamba expressions. It works, but I am not sure if this is the best way to do it. Any recommendations on a better way to do the lambda expression? It seems odd to have numerous lines of code in the expression like I do below. Background: I have a generic list of delegates. Each delega...

Why would you use Expression<Func<T>> rather than Func<T>?

I understand lambdas and the Func and Action delegates. But expressions stump me. In what circumstances would you use an Expression<Func<T>> rather than a plain old Func<T>? ...

Events or Lambdas in C#?

I'm throwing this out there just as a question of curiosity... Assuming you're only expecting/wanting one method to be provided, would this be frowned upon or bad practice? public class Something { public Action OnRemove = () => { }; public Action<object, EventArgs> OnFinishedLoading = (sender, e) => { }; } // then used like....

Is a lambda expression from this possible?

I know from f in list where f.bar == someVar select f can be written as list.Where( f => f.bar == someVar ); Can a similar expression be created from from f in foo from b in f.bar where b.something == someVar select b; ? ...

Is this lambda expression possible?

Thanks to the help with this. Tried this, without luck.. I know from f in list where f.bar == someVar select f can be written as list.Where( f => f.bar == someVar ); Can a similar expression be created from from f in foo from b in f.bar where b.something == someVar select f; ? Edit: Sorry, I forgot f.bar in the second exa...

How to pass a lambda expression to a C# constructor from an IronPython script?

I'm integrating an IronPython scritping engine into my C# raytracer which, so far, has been a breeze even though I'm completely new to Python. There is one particular thing, though, that I need help with. I have a C# class which defines a constructor like this: public CameraAnimation(Action<Camera, float> animation) In C#, I would ins...

Getting a parameterless method to act like a Func<ReturnT>

I'm trying to make a part of my code more fluent. I have a string extension that makes an HTTP request out of the string and returns the response as a string. So I can do something like... string _html = "http://www.stackoverflow.com".Request(); I'm trying to write an extension that will keep trying the request until it succeeds. My ...

linq to xml (c# to vb.net conversion)

What is the VB.net syntax below for? var list = xd.Descendants("product") .Select(element =>new { Title = element.Attribute("title").Value, Duration = element.Element("duration").Value }).ToList(); ...

How to unsubscribe from an event which uses a lambda expression?

I have the following code to let the GUI respond to a change in the collection. myObservableCollection.CollectionChanged += ((sender, e) => UpdateMyUI()); First of all is this a good way to do this? Second: what's the code to unsubscribe from this event? Is it the same but with -= (and then the complete anonymous method again)? ...