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

Added modifiableForEach extension method that allows for in place ListModification during traversal #768

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

tunjid
Copy link

@tunjid tunjid commented Sep 11, 2019

📢 Type of change

  • Bugfix
  • New feature
  • Enhancement
  • Refactoring

📜 Description

Added modifiableForEach extension method that allows for in place List modification during traversal.

💡 Motivation and Context

Adresses #767

If the SlideInItemAnimator is running during an orientation change, a ConcurrentModificationException will be thrown. This change allows for traversal with an iterator, allowing for in place modifications to the list.

💚 How did you test it?

I used the animator in another project where the SlideInItemAnimator runs continuously, and triggered Activity destruction by rotating the phone. Source for that class is here: https://github.com/tunjid/android-bootstrap/blob/develop/app/src/main/java/com/tunjid/androidbootstrap/fragments/ShiftingTileFragment.kt

📝 Checklist

  • I ran ./gradlew spotlessApply before submitting the PR
  • I reviewed submitted code
  • I added tests to verify changes
  • All tests passing

🔮 Next steps

📸 Screenshots / GIFs

@keyboardsurfer
Copy link
Collaborator

keyboardsurfer commented Nov 25, 2019

Over to Nick since he's been working on the Animator.

Copy link
Owner

@nickbutcher nickbutcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! Added a few review comments.

* A for each loop where traversal is backed by an [Iterator] allowing for list modification
* in place.
*/
inline fun <T> Iterable<T>.modifiableForEach(action: (T) -> Unit) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the name here, to me 'modifiable', implies that this modifies each element, not that the underling collection can be modified.

* in place.
*/
inline fun <T> Iterable<T>.modifiableForEach(action: (T) -> Unit) {
iterator().apply {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The apply block feels less idiomatic here. Why not just val iterator = iterator() like most of the stdlib does?

*/
inline fun <T> Iterable<T>.modifiableForEach(action: (T) -> Unit) {
iterator().apply {
while (hasNext()) next().apply(action)
Copy link
Owner

@nickbutcher nickbutcher Nov 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again the apply feels unnecessary, why not just while (iterator.hasNext()) action(next())

iterator().apply {
while (hasNext()) next().apply(action)
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline

@keyboardsurfer keyboardsurfer removed their assignment Nov 25, 2019
@tunjid
Copy link
Author

tunjid commented Nov 25, 2019

@nickbutcher thanks for the comments! Are rebases preferred to merges when there are changes upstream? I checked the contributing doc, but it doesn't seem to mention it.

@nickbutcher
Copy link
Owner

@tunjid rebase please. Thanks!

@tunjid tunjid force-pushed the tj/modifiableForEach branch from d4a10c3 to 9dc1a51 Compare November 26, 2019 04:29
@tunjid
Copy link
Author

tunjid commented Nov 26, 2019

I'm not sure of a better name than removableForEach, very much open to suggestions. I also narrowed the type on the extension to a MutableIterable to better communicate intent.

@nickbutcher
Copy link
Owner

Hmm, I can't think of a great name either and worry that removableForEach implies that the action may remove an item. What about onEach? It's similar enough to forEach to convey what it does without suggesting extra behavior?

@tunjid
Copy link
Author

tunjid commented Nov 26, 2019

That works! What do you think about adding an iterator prefix so the mutable iterator backing is communicated? So iteratorOnEach?

@codingjeremy codingjeremy changed the base branch from master to main September 29, 2020 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants