Lambda expression

  • Thread starter Thread starter Peb
  • Start date Start date
P

Peb

Hi All

I need to retrieve the max count of left or right curve in a list
I use this line of code but i would like to reduce the code (one call to an
extension method)
int count = Math.Max(Curves.Count<Curve>(curve => curve.Position ==
Position.Left), Curves.Count<Curve>(curve => curve.Position ==
Position.Right))

if Somebody has an idea

Thank you

Polo
 
Peb said:
I need to retrieve the max count of left or right curve in a list
I use this line of code but i would like to reduce the code (one call to an
extension method)
int count = Math.Max(Curves.Count<Curve>(curve => curve.Position ==
Position.Left), Curves.Count<Curve>(curve => curve.Position ==
Position.Right))
I don't see any way to reduce the amount of code here. Reducing the amount
of calls is possible, but if that's your goal I'd use a plain old foreach:

int lc = 0, rc = 0;
foreach (Curve curve in Curves) {
if (curve.Position == Position.Left) ++lc;
if (curve.Position == Position.Right) ++rc;
}
int count = Math.Max(lc, rc);

This only enumerates the collection once. It is in fact possible to write
the loop in LINQ as a single call to .Aggregate(), but it's not worth it --
the result is neither clearer nor more efficient.
 
Peb said:
Hi All

I need to retrieve the max count of left or right curve in a list
I use this line of code but i would like to reduce the code (one call to
an extension method)
int count = Math.Max(Curves.Count<Curve>(curve => curve.Position ==
Position.Left), Curves.Count<Curve>(curve => curve.Position ==
Position.Right))

if Somebody has an idea

here's one call to an extension method:

int count = Curves.Count(c => c.Position == Position.Left);
count = Math.Max(count, Curves.Count - count);

Michael
 
Michael said:
here's one call to an extension method:

int count = Curves.Count(c => c.Position == Position.Left);
count = Math.Max(count, Curves.Count - count);
Unfortunately, "Curves" is of type IEnumerable and Position.Bananas is
unaccounted for...
 
Jeroen Mostert said:
Unfortunately, "Curves" is of type IEnumerable

Only the OP could know that.
and Position.Bananas is unaccounted for...

And how do you know there is a Position.Bananas? If there is then the OP
will need to make both checks, whatever form those checks take.

Michael
 
Michael said:
Only the OP could know that.
Oh, actually, I was wrong, "Curves" cannot be of type IEnumerable because
there is no Count<> extension method for IEnumerable; the only extension
methods on IEnumerable are .AsQueryable(), .Cast<>() and .OfType<>().

"Curves" must be of type IEnumerable<Curve> for the code to work (assuming
C# 3.0, not the unreleased C# 4.0), so the explicit instantiation in the
OP's post is unnecessary and your invocation of .Count said:
And how do you know there is a Position.Bananas? If there is then the OP
will need to make both checks, whatever form those checks take.
My point was that you made no provisions for the possibility, not that your
suggestion was wrong in all cases. In particular, making provisions will
almost certainly involve more than one extension method call.
 
Jeroen Mostert said:
Oh, actually, I was wrong, "Curves" cannot be of type IEnumerable because
there is no Count<> extension method for IEnumerable; the only extension
methods on IEnumerable are .AsQueryable(), .Cast<>() and .OfType<>().

You seem to be assuming it can only be of one type, it can be IEnumerable,
IEnumerable<Curve> plus any other interface. I was assuming it was a
collection with its own Count property which implemented IEnumerable said:
My point was that you made no provisions for the possibility,

That is true, I was assuming only Left and Right existed.
not that your suggestion was wrong in all cases. In particular, making
provisions will almost certainly involve more than one extension method
call.

Not necessarily, we could define a GroupAndGetMaxCount extension method, or
something of the like.

Michael
 
Peb said:
Hi All

I need to retrieve the max count of left or right curve in a list
I use this line of code but i would like to reduce the code (one call to an
extension method)
int count = Math.Max(Curves.Count<Curve>(curve => curve.Position ==
Position.Left), Curves.Count<Curve>(curve => curve.Position ==
Position.Right))

if Somebody has an idea

Thank you

Polo

You can do that if you make your own extension:

static class ListExtensions {

public static int CountMax<T>(this IEnumerable<T> list, Func<T, bool>
predicate1, Func<T, bool> predicate2) {
int cnt1 = 0, cnt2 = 0;
foreach (T item in list) {
if (predicate1(item)) cnt1++;
if (predicate2(item)) cnt2++;
}
return Math.Max(cnt1, cnt2);
}

}

Now you can do this:

int count = Curves.CountMax(c => c.Position == Position.Left, c =>
c.Position == Position.Right);
 
Back
Top