Add onEachIndexed similar to forEachIndexed #KT-37161

This commit is contained in:
Abduqodiri Qurbonzoda
2020-04-01 02:52:25 +03:00
parent 57d390bab0
commit 4a7b1b210a
14 changed files with 368 additions and 17 deletions
@@ -213,6 +213,17 @@ public inline fun <K, V, M : Map<out K, V>> M.onEach(action: (Map.Entry<K, V>) -
return apply { for (element in this) action(element) }
}
/**
* Performs the given [action] on each entry, providing sequential index with the entry,
* and returns the map itself afterwards.
* @param [action] function that takes the index of an entry and the entry itself
* and performs the action on the entry.
*/
@SinceKotlin("1.4")
public inline fun <K, V, M : Map<out K, V>> M.onEachIndexed(action: (index: Int, Map.Entry<K, V>) -> Unit): M {
return apply { entries.forEachIndexed(action) }
}
/**
* Creates an [Iterable] instance that wraps the original map returning its entries when being iterated.
*/