AnantLabs / morelinq

Automatically exported from code.google.com/p/morelinq
Apache License 2.0
0 stars 0 forks source link

MoreEnumerable.AtLeast and MoreEnumerable.Exactly #58

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Count() enumerates the entire sequence before returning, AssertCount() 
asserts that there are exactly a specific number of elements and throws an 
exception if not, Any() tells me if there are any elements in the sequence at 
all, but none of these provide the simple behaviour of telling that there are 
or are not at least x number of elements or exactly x number of elements.

AtLeast(int x) should iterate the sequence enough to count that there are at 
least x elements. If there are at least x elements, it returns true, 
otherwise false.

Exactly(int x) should iterate the sequence just enough to count upto x or 
x+1, if x+1 exists. If the sequence is exactly x elements long, it returns 
true, otherwise false.

Original issue reported on code.google.com by jeffma...@gmail.com on 29 Jan 2010 at 9:28

GoogleCodeExporter commented 9 years ago
That behavior can be easily emulated using Take together with Count.
AtLeast(int x)=sequence.Take(x).Count()==x
Exactly(int x)=sequence.Take(x+1).Count()==x

Original comment by CodeInCh...@quantentunnel.de on 2 Mar 2011 at 9:21

GoogleCodeExporter commented 9 years ago
Easily emulated, certainly, but not so nice to read. AtLeast and Exactly 
provide much clearer intent than Take.Count==.

Original comment by jeffma...@gmail.com on 26 Jul 2011 at 5:39

GoogleCodeExporter commented 9 years ago
Hi. I've been playing with a branch, at taking some of my own 
enumeration-focused methods and making those that aren't already covered by 
morelinq match the rest of the library. Hopefully you'll find 
https://code.google.com/r/jon-morelinq/source/browse/MoreLinq/AtLeast.cs 
matches the request here. It also, unlike a combination of .Take() and 
.Count(), won't take a O(n) approach when an O(1) approach is available because 
the source is an ICollection<T>.

Relatedly, 
https://code.google.com/r/jon-morelinq/source/browse/MoreLinq/TakeAtLeast.cs 
might be of some interest; lazily executed, it asserts that there will be 
atleast a certain number of elements and throws an exception otherwise.

Original comment by j...@hackcraft.net on 1 Dec 2013 at 11:09

GoogleCodeExporter commented 9 years ago
@jon@hackcraft.net Have you considered turning on code review on your clone? 
Would help to discuss some things inline.

Original comment by azizatif on 2 Dec 2013 at 9:33

GoogleCodeExporter commented 9 years ago
@azizatif Done. Right now I'm mostly seeing what I have that doesn't duplicate 
things here (e.g. I've an .Eat() that's the same as your .Consume() so there's 
no need for it), tidying things (the methods I'm adding are a bunch of stuff 
that came up over the years, some of which even pre-dates extension methods, 
rather than any organised library) and adding tests where I have none, and I 
wasn't planning to suggest anything to your project until I'd a bit more done 
there. But certainly, reviews are more than welcome, I'm just not familiar with 
code.google so didn't know to turn it on until you mentioned. As well as the 
two cases mentioned above, you might find the buffering of particular interest, 
as it hits two of your TODOs.

Original comment by j...@hackcraft.net on 5 Dec 2013 at 11:53