Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# feature: Add an eval clause to LINQ #6311

Closed
gabrielh opened this issue Oct 25, 2015 · 4 comments
Closed

C# feature: Add an eval clause to LINQ #6311

gabrielh opened this issue Oct 25, 2015 · 4 comments

Comments

@gabrielh
Copy link

When writing LINQ queries one often wants to evaluate the result immediately (especially in an multithreaded environments) rather than lazily, so quite often one uses a ToList() or a ToArray():

    (from i in numbers
     where i < 0
     select i*2).ToArray();

Since this requires the use of brackets around the query, the readability advantages of LINQ are reduced and often people drop LINQ and instead use chained extension methods:

     numbers.Where(i => i < 0).Select(i => i*2).ToArray();

The same happens with all methods that exit the monad, e.g. First/Last/Single, Any/All/Contains, Aggregate, Average/Min/Max/Sum, Count, etc.

So wouldn’t it be nice to support an extra clause in LINQ that would let one apply any of these methods to the query?

    from i in numbers
    where i < 0
    select i*2
    eval ToArray();

I am using the eval keyword here as it does actually result in the evaluation of the query.

Similarly for queries on Option/Maybe

    from i in 1.AsOption()
    from j in -2.AsOption()
    select Sqrt(i*j)
    eval Or(0);

More details and a demo are available here

@dsaf
Copy link

dsaf commented Oct 26, 2015

I see some relation to the forward pipe operator proposal #5445 and also #100.

@JamesTryand
Copy link

I think I've just been sick in my mouth.

@svick
Copy link
Contributor

svick commented Oct 26, 2015

This is a duplicate of #3571 with different name and slightly different syntax.

I think that eval is not a great name, because it's also a name of a well-known function in many other languages which does something completely different.

Also, eval may not actually evaluate the sequence, you could write for example eval AsEnumerable().

@gabrielh
Copy link
Author

Duplicate of #3571

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants