I have a really hard time recognizing when I shoul...
# getting-started
h
I have a really hard time recognizing when I should be uses sequences vs when i shouldn't. Fundamentally, I understand that sequences are lazy and execute each element at a time through to the end, and you can conditionally escape the sequence to make things more efficient.... ....but there's actually few common use cases (in my experience) in which they would benefit me. How am I supposed to even keep them in mind?
👍 1
r
Stick with collections until you have a reason not too (e.g. they don't cover the use case, they are slow AND sequences will solve it (don't assume just because collections are slow sequences will solve it, you'll need to test it and see), etc.)
h
righto.
r
It's a good rule of thumb in my opinion: go with the common solution until that solution is wrong, then find the right one. It sounds obvious, but it's easy to mess up, and messing it up leads to things like (inappropriate) premature optimization.
Sometimes switching a collection for a sequence is they way to go, sometimes you just want a different implementation of your collection, sometimes you need to rethink the operations your performing on the collection, and sometimes what you're doing is just complicated and slow and there's no way around that.
h
yeah, programming can get complicated, huh!
r
Ain't that the truth 🙂
🙃 2
t
I often switch to a sequence when I have to apply more than 2 operators on it (
map
,
filter
, etc.), especially with large collections. One use case where sequences are really useful in my opinion is when you need to
skip
or
take
a given number of elements. In most cases, @Ruckus's answer is the way to go.