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

A rule for when multiple collection or iterable operations are chained to suggest that a sequence be used instead #4838

Closed
mjsztainbok opened this issue May 18, 2022 · 4 comments
Assignees

Comments

@mjsztainbok
Copy link

The rule or changes you're looking for might have already been suggested!
Please search in the issues before creating a new one.

Expected Behavior of the rule

When operations such as map, filter, etc. are chained on an Iterable or Collection, the rule would say that asSequence() should be called before the operations to optimize performance.

Context

Kotlin provides operations like map, filter, etc. directory on Collection and Iterable objects through extension methods. The problem with using these is that if you chain the methods e.g. do a filter then a map multiple iterations are done over the collection/iterable.
However, if a Kotlin sequence is used then only a single iteration is done over the data as the operations are composed and lazily evaluated in a similar way to how Java streams operate
https://kotlinlang.org/docs/sequences.html talks about this

@mjsztainbok mjsztainbok changed the title A rule for when multiple collection or iterable operations are used to suggest that a sequence be used instead A rule for when multiple collection or iterable operations are chained to suggest that a sequence be used instead May 18, 2022
@BraisGabin
Copy link
Member

That sounds like good rule. Do you want to give it a try? We can help you with it. I assume that UnnecessaryFilter could give you a good start point.

@themkat
Copy link
Contributor

themkat commented May 22, 2022

If @mjsztainbok don't want to, then I sure would love to try 🙂 I already started looking a bit into it, but can step aside if @mjsztainbok would prefer looking at it

@BraisGabin
Copy link
Member

I'm using a FIFO here. And you have extra points if you looked a bit into it already ;) Thanks!

@BraisGabin
Copy link
Member

Closed by #4855. Thanks!

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

3 participants