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

#117 add: Iterable.modifyWhere() and Iterable.modifyFirstWhere() #152

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

VKBobyr
Copy link

@VKBobyr VKBobyr commented Jan 2, 2022

Resolves #117

@VKBobyr VKBobyr changed the title Adding iterable modify where #117 Iterable modifyWhere() and modifyFirstWhere() Jan 2, 2022
@VKBobyr VKBobyr changed the title #117 Iterable modifyWhere() and modifyFirstWhere() #117 Iterable.modifyWhere() and Iterable.modifyFirstWhere() Jan 2, 2022
@VKBobyr VKBobyr changed the title #117 Iterable.modifyWhere() and Iterable.modifyFirstWhere() #117 add: Iterable.modifyWhere() and Iterable.modifyFirstWhere() Jan 3, 2022
@passsy
Copy link
Collaborator

passsy commented Apr 12, 2022

The two lambdas that needs to be passed in modifyWhere feel redundant. Why not have one? Two versions in the kotlin world popped into my mind:

inline fun <T> MutableList<T>.mutate(transform: (T) -> T): MutableList<T> {
    return mutateIndexed { _, t -> transform(t) }
}

inline fun <T> MutableList<T>.mutateIndexed(transform: (Int, T) -> T): MutableList<T> {
    val iterator = listIterator()
    var i = 0
    while (iterator.hasNext()) {
        iterator.set(transform(i++, iterator.next()))
    }
    return this
}

or

fun <T> MutableList<T>.mapInPlace(mutator: (T) -> (T)) {
    this.forEachIndexed { i, value ->
        val changedValue = mutator(value)

        if (value != changedValue) {
            this[i] = changedValue
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: modifyWhere and modifyFirstWhere
2 participants