When comparing to a minimum or maximum of two numbers/functions, does C# short-circuit if the case is true for the first one and would imply truth for the second? Specific examples of these cases are
if(x < Math.Max(y, z()))
and
if(x > Math.Min(y, z()))
Since Math.Max(y, z()) will return a value at least as large as y, if x < y then there is no need to evaluate z(), which could take a while. Similar situation with Math.Min.
I realize that these could both be rewritten along the lines of
if(x < y || x < z())
in order to short-circuit, but I think it's more clear what the comparison is without rewriting. Does this short-circuit?
As others have pointed out, the compiler knows nothing about the semantics of Min or Max that would allow it to break the rule that arguments are evaluated before the method is called.
If you wanted to write your own, you could do so easily enough:
static bool LazyLessThan(int x, int y, Func<int> z)
{
return x < y || x < z();
}
and then call it
if (LazyLessThan(x, y, z))
or
if (LazyLessThan(x, y, ()=>z()))
Or for that matter:
static bool LazyRelation<T>(T x, T y, Func<T> z, Func<T, T, bool> relation)
{
return relation(x, y) || relation(x, z());
}
...
if (LazyRelation(x, y, ()=>z, (a,b)=> a < b)))
No, it doesn't short circuit and z() will always be evaluated. If you want the short circuiting behavior you should rewrite as you have done.
Math.Min() and Math.Max() are methods just like any other. They have to be evaluated in order to return the value which will be used as the second argument in the comparison. If you want short-circuiting then you will have to write the condition using the || operator as you have demonstrated.
(Nothing particularly new to add, but I figured I'd share the results of a test I ran on it.)
Math.Max() could easily be inlined by the CLR's just-in-time compiler and from there I was curious whether it might further optimize the code in such a way that it is short-circuited.
So I whipped up a microbenchmark that evaluates the two expressions 1,000,000 times each.
For z(), I used a function that calculates Fib(15) using the recursive method. Here are the results of running the two:
x < Math.Max(y, z()) : 8097 ms
x < y || x < z() : 29 ms
I'm guessing the CLR won't transform the code in any way that prevents method calls from executing, because it doesn't know (and doesn't check to see if) the routine has any side effects.
No, it doesnt short circuit, at least at the C# compiler level. Math.Min or Math.Max are two ordinary static method calls and the compiler will not optimize in that sense.
The order of evaluation of the code will be: z(), Math.Max, x > ...
If you really want to make sure, check out the IL code.
Related
I'm currently following a Unity course. In one of the lessons, the lecturer uses an anonymous function that sorts the results by actionValue.
Here's the relevant code:
public EnemyAIAction GetBestEnemyAIAction()
{
List<EnemyAIAction> enemyAIActionList = new List<EnemyAIAction>();
List<GridPosition> validActionGridPositionList = GetValidActionGridPositionList();
foreach(GridPosition gridPosition in validActionGridPositionList)
{
EnemyAIAction enemyAIAction = GetEnemyAIAction(gridPosition);
enemyAIActionList.Add(enemyAIAction);
}
enemyAIActionList.Sort(
(EnemyAIAction a, EnemyAIAction b) => b.actionValue - a.actionValue
);
}
The lecturer doesn't bother explaining why this approach sorts the list by actionValue. I'm having trouble understanding how, exactly, subtracting the inputs from each other sorts the list by that input value.
The Sort method is declared as
public void Sort (Comparison<T> comparison);
Comparison<T> is declared as
public delegate int Comparison<in T>(T x, T y);
According to the documentation, it returns A signed integer that indicates the relative values of x and y, as shown in the following table.
Value
Meaning
Less than 0
x is less than y.
0
x equals y.
Greater than 0
x is greater than y.
I.e., the Sort method expects a delegate. You can think of a delegate as the address of a function. In this specific case the function accepts two items of the list as input parameters. The return value is a negative int when x is less than y, 0 when both items are considered as equal, and a positive int when x is greater than y.
Now you could declare your own method like this to sort in ascending order:
int EnemyAIActionComparison(EnemyAIAction x, EnemyAIAction y)
{
if (x.actionValue > y.actionValue) return +1;
if (x.actionValue < y.actionValue) return -1;
return 0; // both are equal
}
Since it does not matter how large the result is (only the sign matters), you could simply write
int EnemyAIActionComparison(EnemyAIAction x, EnemyAIAction y)
{
return x.actionValue - y.actionValue;
}
Then call the Sort method like this:
enemyAIActionList.Sort(EnemyAIActionComparison);
Note that no braces must follow EnemyAIActionComparison() because we are not calling the method here, we are passing the method itself as a parameter to Sort. Sort then calls this method on many pairs of list items according to a sorting algorithm (e.g., Quick Sort) until the list is sorted.
Now, there is a shortcut in defining this method: you can use a lambda expression. A lambda expression is a very concise syntax for declaring an anonymous method on the fly.
So (x, y) => x.actionValue - y.actionValue is equivalent to the method above. The type of the parameters is inferred from the declaration int Comparison<in T>(T x, T y) and T is given in the declaration of the list. So, you do not need to specify it as in the example you have given. (Note that the names you give to the parameters does not matter. Specifically, they do not need to be the same as in the declaration of Comparison.)
If you want to sort in descending order, just swap the signs, i.e., swap the values in the subtraction.
Let's assume that we have the following expensive functions:
bool ExpensiveOp1() { ... }
bool ExpensiveOp2() { ... }
bool ExpensiveOp3() { ... }
Also, to keep it simple, assume that none of them have side effects.
I know that C# can short-circuit if ExpensiveOp1 or ExpensiveOp2 returns false in the following expression:
return ExpensiveOp1() && ExpensiveOp2() && ExpensiveOp3();
However, is the compiler smart enough to (for lack of a better term) inline the function calls and take advantage of short-circuiting if I write the code this way?
var x = ExpensiveOp1();
var y = ExpensiveOp2();
var z = ExpensiveOp3();
return x && y && z;
No, and for a good reason. The compiler doesn't know if any of your operations have side effects, so if you run them outside of a boolean short circuiting situation it runs them in case there are side effects you want.
I have been learning about the lambda expression, I got happy when I finally can read/understand the => operator, it kind of means "where" to me
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b = a.FindAll(x => x>=5);
foreach (int x in b)
Console.WriteLine(x);
Reading the above line, personally makes sense to read it as "Find all x's from this list WHERE x is greater than or equal 5", very good.
But then I come across a different use of the lambda expression with the Select method.
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b1 = a.Select(x => x*2);
foreach (int x in b)
Console.WriteLine(x);
With this one, the previous way of reading this operator doesn't make sense, as to me this code does "For each x return x*2", which is very different "function" to what the same operator does in the previous case.
I understand that the difference is between .FindAll and .Select, different way of dealing with input and output parameters, but I am talking about the use of the operator => in the lambda expression.
There's no question in this question, so let's make one up.
Characterizing the lambda operator as "where" works when the lambda returns a bool and is used as a predicate to test a value. Is there a more general characterization of the lambda operator that makes sense in other contexts, such as projection?
Yes. Read the lambda operator as "goes to".
a.Select(x => x * 2);
"each x goes to x times two"
You can use that for predicates as well:
a.Where(x => x > 2);
"each x goes to 'is x greater than two?'"
But that's awkward. As you note, it is easier to think of this as "where" or "such that"
"each x such that x is greater than two"
Similarly
a.OrderBy(x => x.LastName)
"order each x by x goes to last name" is awkward. Just say "order each x by last name".
In short: the English language interpretation of the operator depends on the context. The formal interpretation of the operator is always the same: it simply describes a function.
The => operator has exactly the same meaning in both cases: it creates a function whose parameter is the thing on the left, and whose return value is the thing on the right.
You wrote in a comment that the x in the first case is not a parameter as you understand it. That's not correct; it is a parameter in both cases here.
Here's your first example, again:
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b = a.FindAll(x => x>=5);
foreach (int x in b)
Console.WriteLine(x);
If you wanted to write this without using lambda notation, you would define a function somewhere, like this...
static bool MyCondition(int x)
{
return x >= 5;
}
...and then use that function as the argument to FindAll:
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b = a.FindAll(MyCondition);
foreach (int x in b)
Console.WriteLine(x);
The lambda notation is a shorter notation which allows you to define the function right where you use it.
Likewise, if you wanted to write your second example without using lambda notation, you'd define a function elsewhere, like this...
static int MyOperation(int x)
{
return x * 2;
}
...and pass your function as the argument to Select, like this:
List<int> a = new List<int>(){0,1,2,1,3,4,5,6,7,8,9};
IEnumerable<int> b1 = a.Select(MyOperation);
foreach (int x in b)
Console.WriteLine(x);
Think of it this way:
Mathematics: f(x) = x + x
This is a mathematical function f that takes a number x and spits out its double.
Lambda: f = x => x + x C#'s way of defining the same function f.
Another example:
Mathematics: g(x, y) = x > y
g is a function that takes two numbers x and y and returns wether the former is greater than the latter.
Lambda: g = (x, y) => x > y C#'s way of defining the same function g.
Clearer now?
P.D: I've omitted talking about type inference and the type of the lambda's themselves; its an unnecessary distraction considering the context of this question.
I am coming from a OOP, non-functional background, so I am having trouble fully visualizing several online examples regarding continuation passing. Also, functional languages like Scheme don't have to specify types of arguments or return values, so I am unsure whether I got the idea correctly.
Since C# supports lambdas, I took the first example from the Wikipedia article and tried to port it to C# with strong typing, to see how the pattern would apply:
// (Scheme)
// direct function
(define (pyth x y)
(sqrt (+ (* x x) (* y y))))
// rewriten with CPS
(define (pyth& x y k)
(*& x x (lambda (x2)
(*& y y (lambda (y2)
(+& x2 y2 (lambda (x2py2)
(sqrt& x2py2 k))))))))
// where *&, +& and sqrt& are defined to
// calculate *, + and sqrt respectively and pass the result to k
(define (*& x y k)
(k (* x y)))
So, rewriting the CPS pyth& version in C# resulted in:
// (C#6)
// continuation function signature
delegate double Cont(double a);
// *&, +& and sqrt& functions
static double MulCont(double a, double b, Cont k) => k(a * b);
static double AddCont(double a, double b, Cont k) => k(a + b);
static double SqrtCont(double a, Cont k) => k(Math.Sqrt(a));
// sqrt(x*x + y*y), cps style
static double PythCont(double x, double y, Cont k) =>
MulCont(x, x, x2 =>
MulCont(y, y, y2 =>
AddCont(x2, y2, x2py2 =>
SqrtCont(x2py2, k))));
I could have used generics instead of double, but signatures would be longer. Anyway, what I am not sure is:
Is the Cont signature above correct (i.e. Func<double, double>)? Should the continuation fn. accept the parameter, process it, and then return the value of the same type back?
When I first started reading about continuations, I got the feeling that this continuation function will get invoked for each step in the call stack, but in the example above it's only passed to sqrt&, and all other calls get lambdas which don't really "pass" intermediate values to the original continuation. The code above in the function above is basically analogue to k(Math.Sqrt(x * x + y * y)), so does this mean my assumption about intermediate "hooks" is wrong?
Yes, unless you want to do anything non-numerical with the outermost continuation, it is correct.
You would only need more "Cont"s when your original expression involves more types, e.g.
(define (foo x) (if (= x 0) 1 0))
in which case it might look like this (sorry I write in scheme for brevity):
(define (foo& x k)
(=& x 0 (lambda (r1)
(if r1 (k 1) (k 0)))))
-- now the outermost continuation has a number (let's say an int) as input, while the one provided to "=&" has bool->int type.
You are almost right (up to duality) -- each step on the call stack is now a call to some continuation.
In general you might be confusing first-class continuations with cps -- the former is a language feature (as in scheme where you can access the current continuation with call/cc operator), the latter is a technique you can use anywhere.
You actually can convert expressions to cps without even having higher-order functions in your language (by just representing them somehow).
Another thing you asked is how cps relates to control flow. Well, notice that in applicative, functional language (like scheme) the only thing you have specified is that in case of application, you first evaluate the operands and the operator, and then apply the latter to the former. It does not matter in what order you evaluate the operands -- you might do it left-to-right, right-to-left [or perhaps in some crazy way]. But what if you're not using purely functional style, and the operands cause some side effects? They might e.g. print something to stdout, and later return some value. In that case, you would like to have control over the order. If I remember well, programs compiled with gambit-C evaluate arguments right-to-left, while interpreted with gambit's interpreter left-to-right -- so the problem really exists ;). And precisely then the cps might save you [actually there are other means as well, but we're about cps right now!].
In the scheme example you posted it is forced that the arguments of "+" are evaluated left-to-right.
You might alter that easily:
(define (pyth& x y k)
(*& y y (lambda (y2)
(*& x x (lambda (x2)
(+& x2 y2 (lambda (x2py2)
(sqrt& x2py2 k))))))))
And that's the thing.
Of some further applications, as guys already said in the comments, transformation to CPS moves every application to tail-position, so the call stack is being replaced with lambdas, and further if you defunctionalize them, what you get is a data structure representing the control flow -- a neat form to be converted to, say C, or some other imperative language. Fully automagicaly!
Or, if you'd like to implement some monad mumbo-jumbo, say Maybe monad, in CPS it's easy, just prepend to each continuation-lambda the test on whether the received value is "Just something" (in which case do the job and push the result to your continuation), or "Nothing", in which case you just push Nothing (to the continuation-lambda).
Of course rather by another program or macro, not by hand, as it might be tedious -- the most magic behing cps is that it's so easy to automate the transformation to cps.
Hope I didn't make it unnecessarily complicated.
I have created a very comprehensive introduction to the Continuation monad that you can Find Here Discovering the Continuation Monad in C#
Also you can find a.Net Fiddle here
I Repeat it in summary here
Starting from an initial Function
int Square(int x ){return (x * x);}
Use Callback and remove return type
public static void Square(int x, Action<int> callback)
{
callback(x * x);
}
Curry the Callback
public static Action<Action<int>> Square(int x)
{
return (callback) => { callback(x * x); };
}
Generalize the returned Continuation
public static Func<Func<int,T>,T> Square<T>(int x)
{
return (callback) => { callback(x * x); };
}
Extract the Continuation Structure Also Known As the Return Method of the monad
delegate T Cont<U, T>(Func<U, T> f);
public static Cont<U, T> ToContinuation<U, T>(this U x)
{
return (callback) => callback(x);
}
square.ToContinuation<Func<int, int>, int>()
Add The bind Monadic method and thus Complete the Monad
public static Cont<V, Answer> Bind<T, U, V, Answer>(
this Cont<T, Answer> m,
Func<T, Cont<U, Answer>> k,
Func<T, U, V> selector)
{
return (Func<V, Answer> c) =>
m(t => k(t)(y => c(selector(t, y))));
}
I don't think it's possible to use operators as a parameters to methods in C# 3.0 but is there a way to emulate that or some syntactic sugar that makes it seem like that's what's going on?
I ask because I recently implemented the thrush combinator in C# but while translating Raganwald's Ruby example
(1..100).select(&:odd?).inject(&:+).into { |x| x * x }
Which reads "Take the numbers from 1 to 100, keep the odd ones, take the sum of those, and then answer the square of that number."
I fell short on the Symbol#to_proc stuff. That's the &: in the select(&:odd?) and the inject(&:+) above.
Well, in simple terms you can just use a lambda:
public void DoSomething(Func<int, int, int> op)
{
Console.WriteLine(op(5, 2));
}
DoSomething((x, y) => x + y);
DoSomething((x, y) => x * y);
// etc
That's not very exciting though. It would be nice to have all those delegates prebuilt for us. Of course you could do this with a static class:
public static class Operator<T>
{
public static readonly Func<T, T, T> Plus;
public static readonly Func<T, T, T> Minus;
// etc
static Operator()
{
// Build the delegates using expression trees, probably
}
}
Indeed, Marc Gravell has done something very similar in MiscUtil, if you want to look. You could then call:
DoSomething(Operator<int>.Plus);
It's not exactly pretty, but it's the closest that's supported at the moment, I believe.
I'm afraid I really don't understand the Ruby stuff, so I can't comment on that...
The following is direct, literal (as much as possible) C# translation:
(Func<int>)(x => x * x)(
Enumerable.Range(1, 100)
.Where(x => x % 2 == 1)
.Aggregate((x, y) => x + y))
Specifically:
blocks: {||} - become lambdas: =>
select becomes Where
inject becomes Aggregate
into becomes a direct call on a lambda instance