Comparing sequence length

I am trying to figure out the best way to compare sequence length, lets say

mySequence.count() > 1

It seems to me this will likely iterate over the entire sequence. This seems wasteful especially if the sequence is large. Therefore, I came up with

mySequence.windowed(2).any()

Does this make sense?

Sequence doesn’t know its length, this is the main point of sequences that differs them from collections.

2 Likes

I get this, but I sometimes want to verify a minimum length.

It is also my understanding that it processes elements one by one (like java streams) as opposed to map etc methods on collections.

So you need to only check if count() is at least min and you accept iterating through first min items in order to check it? I think the most straightforward is: seq.drop(min - 1).any().

That is exactly right. Would drop perform better then windowed? Also, what would drop do if the sequence is empty?

edit
It seems drop does indeed do nothing on an empty sequence, or just completely empties it if n is much larger then the sequence size. So this would indeed work. So I guess that leaves the question of performance and/or esthetic preference.

1 Like

windowed may be potentially slower, because it has to create the window - it creates a buffer and puts items into it, even if you are entirely not interested in these items. drop simply skips items. In practice, difference is probably neglectable.

edit:
But at least for me windowed is much less readable. It took me some time to understand what you actually did there. It requires understanding windowed logic and that it skips partial windows, etc. If you look for the most readable way, then maybe: seq.take(min).count() == min. Even better, simply create an extension countAtLeast(Int) and then you can use whatever you consider the fastest.

1 Like

Yeah, I was considering maybe creating an extension. I had something much more complicated (tinkering with the iterator) first, but IMO both

seq.windowed(min).any()
seq.drop(min-1).any()
seq.take(min).count() == min

seem too compact to warrant a public reusable extension. Maybe I could use a private one and scope it to the class in question?
That last one is an interesting variant btw. I think all three of these won’t iterate the entire sequence and are all likely better compared to doing that. After all this discussion I’m leaning towards the second (drop) one.

1 Like