Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are lambda expressions/delegates in C# "pure", or can they be?

I recently asked about functional programs having no side effects, and learned what this means for making parallelized tasks trivial. Specifically, that "pure" functions make this trivial as they have no side effects.

I've also recently been looking into LINQ and lambda expressions as I've run across examples many times here on StackOverflow involving enumeration. That got me to wondering if parallelizing an enumeration or loop can be "easier" in C# now.

Are lambda expressions "pure" enough to pull off trivial parallelizing? Maybe it depends on what you're doing with the expression, but can they be pure enough? Would something like this be theoretically possible/trivial in C#?:

  • Break the loop into chunks
  • Run a thread to loop through each chunk
  • Run a function that does something with the value from the current loop position of each thread

For instance, say I had a bunch of objects in a game loop (as I am developing a game and was thinking about the possibility of multiple threads) and had to do something with each of them every frame, would the above be trivial to pull off? Looking at IEnumerable it seems it only keeps track of the current position, so I'm not sure I could use the normal generic collections to break the enumeration into "chunks".

Sorry about this question. I used bullets above instead of pseudo-code because I don't even know enough to write pseudo-code off the top of my head. My .NET knowledge has been purely simple business stuff and I'm new to delegates and threads, etc. I mainly want to know if the above approach is good for pursuing, and if delegates/lambdas don't have to be worried about when it comes to their parallelization.

like image 553
Bob Avatar asked Mar 17 '10 22:03

Bob


People also ask

Is there lambda function in C?

Significance of Lambda Function in C/C++ Lambda Function − Lambda are functions is an inline function that doesn't require any implementation outside the scope of the main program. Lambda Functions can also be used as a value by the variable to store.

How are an anonymous delegate and a lambda expression related?

Anonymous Method is an inline code that can be used wherever a delegate type is expected. Microsoft introduced Anonymous Methods in C# 2.0 somewhere around 2003. Lambda expression is an anonymous method that you can use to create delegates or expression tree types.

Are lambdas immutable?

Lambda functions are pure because they do not rely on a specific class scope. They are immutable because they reference the passed parameter but do not modify the parameter's value to reach their result. Finally, they're first-class functions because they can be anonymous and passed to other functions.

What are built in delegates in C#?

What are Delegates? Delegates are pointers to function. Delegates are used for implementing events and call back methods. Func, Action and Predicate are define in C# 3.0 and these are generic inbuilt delegates.

What is delegate with lambda expressions?

Introduction This article provides the basics of delegates, Lambda expressions and delegate with Lambda Expressions. 2. What is Delegate? Delegates are a class that holds function references for .Net (more specifically, the Common Language Infrastructure (CLI)).

What are lambda expressions in C?

Lambda expressions in C# are used like anonymous functions, with the difference that in Lambda expressions you don’t need to specify the type of the value that you input thus making it more flexible to use. The ‘=>’ is the lambda operator which is used in all lambda expressions.

How do I separate the parameter list from the lambda expression?

Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms: Expression lambda that has an expression as its body: Statement lambda that has a statement block as its body:

What happens if a lambda expression doesn't return a value?

If a lambda expression doesn't return a value, it can be converted to one of the Action delegate types; otherwise, it can be converted to one of the Func delegate types. For example, a lambda expression that has two parameters and returns no value can be converted to an Action<T1,T2> delegate.


1 Answers

First off, note that in order to be "pure" a method must not only have no side effects. It must also always return the same result when given the same arguments. So, for example, the "Math.Sin" method is pure. You feed in 12, it gives you back sin(12) and it is the same every time. A method GetCurrentTime() is not pure even if it has no side effects; it returns a different value every time you call it, no matter what arguments you pass in.

Also note that a pure method really ought not to ever throw an exception; exceptions count as observable side effects for our purposes.

Second, yes, if you can reason about the purity of a method then you can do interesting things to automatically parallelize it. The trouble is, almost no methods are actually pure. Furthermore, suppose you do have a pure method; since a pure method is a perfect candidate for memoization, and since memoization introduces a side effect (it mutates a cache!) it is very attractive to take what ought to be pure methods and then make them impure.

What we really need is some way to "tame side effects" as Joe Duffy says. Some way to draw a box around a method and say "this method isn't side-effect-free, but its side effects are not visible outside of this box", and then use that fact to drive safe automatic parallelization.

I'd love to figure out some way to add these concepts to languages like C#, but this is all totally blue-sky open-research-problem stuff here; no promises intended or implied.

like image 99
Eric Lippert Avatar answered Sep 27 '22 20:09

Eric Lippert