lambda

C# 3.0 Where extension method with lambda vs LINQtoObjects to filter in memory collections.

I am prototyping some C# 3 collection filters and came across this. I have a collection of products: public class MyProduct { public string Name { get; set; } public Double Price { get; set; } public string Description { get; set; } } var MyProducts = new List<MyProduct> { new MyProduc...

Can using lambdas as event handlers cause a memory leak?

Say we have the following method: private MyObject foo = new MyObject(); // and later in the class public void PotentialMemoryLeaker(){ int firedCount = 0; foo.AnEvent += (o,e) => { firedCount++;Console.Write(firedCount);}; foo.MethodThatFiresAnEvent(); } If the class with this method is instantiated and the PotentialMemoryLea...

What is a lambda (function)?

Hey guys, I see this term being used a bit, and a Google search didn't quite yield the most clarity, so help me out: for a person without a comp-sci background, what is a lambda in the world of Computer Science? UPDATE: marxidad, thanks for the reply--it seems to be climbing up in everyone's favor, so I'll likely accept it soon. Do you...

How do I marshall a lambda (Proc) in Ruby?

Joe Van Dyk asked the Ruby mailing list: Hi, In Ruby, I guess you can't marshal a lambda/proc object, right? Is that possible in lisp or other languages? What I was trying to do: l = lamda { ... } Bj.submit "/path/to/ruby/program", :stdin => Marshal.dump(l) So, I'm sending BackgroundJob a lambda object, which cont...

What is a good use of lambda expressions, and how can I learn to use them?

I've read that Lambda Expressions are an incredibly powerful addition to C#, yet I find myself mystified by them. How can they improve my life or make my code better? Can anyone point to a good resource for learning such expressions? They seem cool as hell, but how do they relate to my day-to-day life as an asp.net developer? Edit: Tha...

Getting all types that implement an interface with C# 3.5

How can I do what's in the title, with the minimum amount of code, using whatever c# 3.5 syntax (I'm guessing lambda expressions would fit, but I still don't understand them fully)? In short, I want to iterate through all types that implement a particular interface. Edit: I wasn't clear. Let me re-phrase my question: How can I, using r...

Coolest C# LINQ/Lambdas trick you've ever pulled?

Saw a post about hidden features in C# but not a lot of people have written linq/lambdas example so... I wonder... What's the coolest (as in the most elegant) use of the C# LINQ and/or Lambdas/anonymous delegates you have ever saw/written? Bonus if it has went into production too! ...

Shorthand conditional in C# similar to SQL 'in' keyword

In C# is there a shorthand way to write this: public static bool IsAllowed(int userID) { return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...); } Like: public static bool IsAllowed(int userID) { return (userID in Personnel.JohnDoe, Personnel.JaneDoe ...); } I know I could also use switch, but there are pro...

Pass functions in F#

Is it possible to pass a reference to a function to another function in F#? Specifically, I'd like to pass lambda functions like foo(fun x -> x ** 3) More specifically, I need to know how I would refer to the passed function in a function that I wrote myself. ...

How do I group in memory lists?

I have a list of Foo. Foo has properties Bar and Lum. Some Foos have identical values for Bar. How can I use lambda/linq to group my Foos by Bar so I can iterate over each grouping's Lums? ...

Help convert this delegate to an anonymous method or lambda

I am new to all the anonymous features and need some help. I have gotten the following to work: public void FakeSaveWithMessage(Transaction t) { t.Message = "I drink goats blood"; } public delegate void FakeSave(Transaction t); public void SampleTestFunction() { ... Expect.Call(delegate { _dao.Save(t); }).Do(new FakeSave(Fake...

Recursive lambda expression to traverse a tree in C#

Can someone show me how to implement a recursive lambda expression to traverse a tree structure in C#. ...

Is it possible to cache a value evaluated in a lambda expression? (C# + Linq)

In the ContainsIngredients method in the following code, is it possible to cache the p.Ingredients value instead of explicitly referencing it several times? This is a fairly trivial example that I just cooked up for illustrative purposes, but the code I'm working on references values deep inside p eg. p.InnerObject.ExpensiveMethod().Val...

Is it possible to advance an enumerator and get its value in a lambda?

If I have an IEnumerator variable is it possible to have a lambda function that takes it, advances it with MoveNext() and returns the Current value every single time its called? ...

What is the difference between lambdas and delegates in the .NET Framework?

I get asked this question a lot and I thought I'd solicit some input on how to best describe the difference. ...

Rhino Mocks: What scope do actions inside Do() execute in?

I notice that if I write Expect.Call(delegate { obj.Method(null); }).IgnoreArguments().Do( new Action(() => {Console.Write("executed");throw new Exception(); })); Provided that the method does run, MbUnit will recieve the "executed" message but will not detect an exception being thrown. Does anyone know why that is so? Is this som...

Good explanation of "Combinators" (For non mathematicians)

Anyone got a good explanation of "combinators" (Y-combinators etc. and NOT the company) I'm looking for one for the practical programmer who understands recursion and higher-order functions, but doesn't have a strong theory or math background. (Note that I'm talking about these things : http://en.wikipedia.org/wiki/Y_combinator ) ...

Understanding the behaviour of inject used with a lambda in Ruby

I often plug pre-configured lambdas into enumerable methods like 'map', 'select' etc. but the behavior of 'inject' seems to be different. e.g. with mult4 = lambda {|item| item * 4 } then (5..10).map &mult4 gives me [20, 24, 28, 32, 36, 40] However, if I make a 2-parameter lambda for use with an inject like so, multL = lambda {|...

Anonymous methods/delegates and lambda expressions

With the advent of new features like lambda expressions (inline code), does it mean we dont have to use delegates or anonymous methods anymore? In almost all the samples I have seen, it is for rewriting using the new syntax. Any place where we still have to use delegates and lambda expressions won't work? ...

LINQ to entities - Building where clauses to test collections within a many to many relationship.

So, I am using the Linq entity framework. I have to entities "Content" and "Tag" They are in a many-to-many relationship with one another. Content can have many tags and Tags can have many Contents. So I am trying to write a query to select all contents where any tags names are equal to "blah." The entities both have a collection of the...