Linq point free syntax for multiple predicates #2737
Replies: 7 comments
-
I'd have loved to read Eric Lippert's take on this, while he was on the C# language design team. I suspect it would have been something like "A single suitable method group can be inferred to a predicate, but this would require that any general expression involving compatible method groups also be so inferable (?) - such would involve creating a new internal algebra, where the primitives are method groups, and the operations are any combination of operators, not just boolean ones. For example, why not "Foo > Bar", where Foo(f) and Bar(f) return numeric values? That's a lot of work!" |
Beta Was this translation helpful? Give feedback.
-
I think it could be confusing - looking at your proposed syntax, |
Beta Was this translation helpful? Give feedback.
-
You could solve this by declaring a function like
|
Beta Was this translation helpful? Give feedback.
-
Here, I wrote you an extension method: public static class LinqExtensions {
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
params Func<TSource, bool>[] predicates)
{
IEnumerable<TSource> result = source;
foreach (var predicate in predicates)
{
result = result.Where(predicate);
}
return result;
}
} Usage: Enumerable.Range(1, 20).Where(Foo, Bar).PrintAll(); |
Beta Was this translation helpful? Give feedback.
-
I think the original suggestion was for something more general, that could handle cases like Where(Foo && (Bar || Baz)) etc., with Foo, Bar, Baz all being method groups. |
Beta Was this translation helpful? Give feedback.
-
I think I see the point now. It's like function composition in F#. Perhaps we can have something like this: Func<int, bool> Foo = x => x % 2 == 0;
Func<int, bool> Bar = x => x % 3 == 0;
Func<int, int> Square = x => x * x;
// Pipelining
var F1 = Square |> Foo;
// F1 should be equivalent to:
Func<int, bool> F1 = x => Foo.Invoke(Square.Invoke(x));
// or even better with Expression rewrite:
Func<int, bool> F1 = x => (x * x) % 2 == 0;
// Binary
var F2 = Foo && Bar;
// F2 should be equivalent to:
Func<int, bool> F2 = x => Foo.Invoke(x) && Bar.Invoke(x);
// or with Expression rewrite:
Func<int, bool> F2 = x => (x % 2 == 0) && (x % 3 == 0);
// Unary
var F3 = !Bar;
// F3 should be equivalent to:
Func<int, bool> F3 = x => !Bar.Invoke(x);
// or with Expression rewrite:
Func<int, bool> F3 = x => !(x % 3 == 0);
// Then we can write this:
Enumerable.Range(1, 20).Where(Foo && Bar).PrintAll();
Enumerable.Range(1, 20).Where((Square |> Foo) && !Bar).PrintAll(); |
Beta Was this translation helpful? Give feedback.
-
This old post could help you: https://blogs.msdn.microsoft.com/meek/2008/05/02/linq-to-entities-combining-predicates/ With this, you can have something like Enumerable.Range(1, 20).Where(Foo.And(Bar)).PrintAll(); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Given the following LINQ:
If I only had one function in the Where I could write
Is their any reason why if I have multiple functions and they all take just one parameter, and it's the type of the item in the collection, I could not have to write
f=> Foo(f) && Bar(f)
e.g.
?
If this makes no sense as a language suggestion or is impossible, feel free to close.
Thanks :)
Beta Was this translation helpful? Give feedback.
All reactions