-
Notifications
You must be signed in to change notification settings - Fork 416
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
Add IsEmpty that judges that sequence is empty or not #561
Add IsEmpty that judges that sequence is empty or not #561
Conversation
You can use "AtMost(0)" or "Exactly (0)" for clarity |
I think that Though there are
The method name Someone understand all of MoreLinq method, but the other one understand only some methods in MoreLinq.
Please see #561 (comment) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, we try to avoid trivial one-line implementations in MoreLINQ for the sole purpose of increased readability. They are best left as local extension methods in a project, and this one seems to fit that bill:
public static bool IsEmpty<TSource>(this IEnumerable<TSource> source) =>
!source.Any();
See #556 for some discussion and thoughts on “why” for another case and I'll repeat part of the closing comment:
Also something that might not be obvious is the maintainer's dilemma: the trouble of allowing one-liners for purpose of readability means that many such requests will start pouring in and I fear that discussions will end up getting very subjective. The primary objectivity I'd like to maintain is therefore that a suggestion must be operationally intelligent or do some amount of non-trivial work. And even with that, we've struggled with the naming of many operators in MoreLINQ because one name might speak more to some people than another.
Never fun to not merge and I hope this doesn't discourage future contributions. I would recommend opening an issue to discuss before going too far with efforts to implement, test and document. |
I'm sorry to open PR without issue. Next time, I will open issue before PR. Thank you. |
I have a question. According to your next comment,
In MoreLinq, will one-liner method be never accepted? According README.md
I belive that By the way I think that principale, core rule and core concepts are so important for Library.
I do not agree it, but I understand it. In MoreLinq, will one-liner method be never accepted? |
That's a very old motto (from @jskeet) that's been there since the inception of the project but I do think it's still applicable. From discussions, I have found that some people believe that the spirit of LINQ is to make code more readable. While that's true, it's also a very shallow observation.
I respect you for holding your view with grace. 👍
Most probably but I won't say never.
Like your LinqAlias project, I think there's a home for such “one-liners for sake of improving readability” but which don't necessarily add operational intelligence. If you start such a project, I'd be happy to direct people there. |
Thank you for your answer. OK. I understand! By the way, my aim was not add only 'one liner methods'. For example,
Many of them are in MoreLINQ. So I am happy if I think MoreLINQ is so nice library! And I want to send idea or issue in the future. By the way, I will start project that provides useful collection methods that is in many other programming language but is not in C# LINQ to Objects. Thank you for your answer. |
I look forward to that. 👍 |
Summary
Add
IsEmpty
method.IEnumerable<T>
is empty.IsEmpty
method helps them.Background
Someone uses "Count() == 0" incorrectly to judge that
IEnumerable<T>
is empty or not. This is bad usage. Next code is an example.Next code read all line twice. To judge
target.txt
is empty, we do not need read all lines. Iftarget.txt
is huge, it is so bad.Good practice is to use
Any
method and!
to judgeis empty
. This can judge that file is empty or not without reading all lines.By the way, I do NOT think this is BEST way.
Solution
I suggest
IsEmpty
method to MoreLinq.IsEmpty
judeges that Array,IEnumerable<T>
andList<T>
etc are empty or not.This is more clear and explicit than
Any
and!
usage. And beginner can notice theIsEmpty
method with IntelliSense.In other languages.
Next languages have
IsEmpty
orisEmpty
methods.F#
https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/list.isempty%5B't%5D-function-%5Bfsharp%5D
https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/array.isempty%5b't%5d-function-%5bfsharp%5d
https://msdn.microsoft.com/en-us/visualfsharpdocs/conceptual/seq.isempty%5b't%5d-function-%5bfsharp%5d
Java
https://docs.oracle.com/javase/10/docs/api/java/util/Collection.html#isEmpty()
Kotlin
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/is-empty.html
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html
Scala
https://www.scala-lang.org/api/current/scala/collection/GenTraversableOnce.html#isEmpty:Boolean
Discussion
How about use
!enumrable.Any()
?I think
enumerable.IsEmpty()
is better than!enumerable.Any()
with next reasons.IsEmpty()
is more clear and more explicit to check thatenumerable
is empty than!enumerable.Any()
.!enumerable.Any()
to check empty is not familiar, not clear and not easy to find.I think everyone who participates in this issue is familiar to C# and LINQ. So we know how to use
!enumerable.Any()
and reason to use!enumerable.Any()
.But I think that C# beginner is not same. Please imagine C# beginner. It is a little difficult to find usage of
!enumerable.Any()
to check empty for them. I thinkIsEmpty
is so easy to find and useful to them.