Add mapNonNullIndexed API to List and Set (#1063)

This commit is contained in:
Josh B
2025-05-08 06:57:28 -07:00
committed by GitHub
parent 919d63e51a
commit 948a20ad0c
7 changed files with 78 additions and 6 deletions

View File

@@ -2441,6 +2441,18 @@ abstract external class Collection<out Element> extends Any {
@AlsoKnownAs { names { "filterMap" } }
abstract function mapNonNull<Result>(transform: (Element) -> Result): Collection<Result(this != null)>
/// Transforms this collection by applying [transform] to each element and removing resulting [null] elements.
///
/// [transform] takes two arguments: the index of the element, and the element itself.
///
/// Facts:
/// ```
/// List(1, 2, 3, 4).mapNonNullIndexed((i, n) -> if (n.isOdd) null else n * i) == List(2, 12)
/// List(1, 2, 3, 4, null).mapNonNullIndexed((i, n) -> if (n?.isOdd ?? true) null else n * i) == List(2, 12)
/// ```
@Since { version = "0.29.0" }
abstract function mapNonNullIndexed<Result>(transform: (Int, Element) -> Result): Collection<Result(this != null)>
/// Applies a collection-generating [transform] to each element in this collection
/// and concatenates the resulting collections.
///
@@ -2874,6 +2886,9 @@ external class List<out Element> extends Collection<Element> {
external function filterIndexed(predicate: (Int, Element) -> Boolean): List<Element>
external function mapIndexed<Result>(transform: (Int, Element) -> Result): List<Result>
@Since { version = "0.29.0" }
external function mapNonNullIndexed<Result>(transform: (Int, Element) -> Result): List<Result(this != null)>
external function flatMapIndexed<Result>(transform: (Int, Element) -> Collection<Result>): List<Result>
external function filterIsInstance<Type>(clazz: Class<Type>): List<Type>
@@ -3082,6 +3097,9 @@ external class Set<out Element> extends Collection<Element> {
external function filterIndexed(predicate: (Int, Element) -> Boolean): Set<Element>
external function mapIndexed<Result>(transform: (Int, Element) -> Result): Set<Result>
@Since { version = "0.29.0" }
external function mapNonNullIndexed<Result>(transform: (Int, Element) -> Result): Set<Result(this != null)>
external function flatMapIndexed<Result>(transform: (Int, Element) -> Collection<Result>): Set<Result>
external function filterIsInstance<Type>(clazz: Class<Type>): Set<Type>