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

[KT-11215] missing partitionIndexed #5255

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
200 changes: 199 additions & 1 deletion libraries/stdlib/common/src/generated/_Arrays.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down Expand Up @@ -22879,6 +22879,204 @@ public inline fun CharArray.partition(predicate: (Char) -> Boolean): Pair<List<C
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun <T> Array<out T>.partitionIndexed(predicate: (index: Int, T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun ByteArray.partitionIndexed(predicate: (index: Int, Byte) -> Boolean): Pair<List<Byte>, List<Byte>> {
val first = ArrayList<Byte>()
val second = ArrayList<Byte>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun ShortArray.partitionIndexed(predicate: (index: Int, Short) -> Boolean): Pair<List<Short>, List<Short>> {
val first = ArrayList<Short>()
val second = ArrayList<Short>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun IntArray.partitionIndexed(predicate: (index: Int, Int) -> Boolean): Pair<List<Int>, List<Int>> {
val first = ArrayList<Int>()
val second = ArrayList<Int>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun LongArray.partitionIndexed(predicate: (index: Int, Long) -> Boolean): Pair<List<Long>, List<Long>> {
val first = ArrayList<Long>()
val second = ArrayList<Long>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun FloatArray.partitionIndexed(predicate: (index: Int, Float) -> Boolean): Pair<List<Float>, List<Float>> {
val first = ArrayList<Float>()
val second = ArrayList<Float>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun DoubleArray.partitionIndexed(predicate: (index: Int, Double) -> Boolean): Pair<List<Double>, List<Double>> {
val first = ArrayList<Double>()
val second = ArrayList<Double>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun BooleanArray.partitionIndexed(predicate: (index: Int, Boolean) -> Boolean): Pair<List<Boolean>, List<Boolean>> {
val first = ArrayList<Boolean>()
val second = ArrayList<Boolean>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Splits the original array into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Arrays.Transformations.partitionIndexedArrayOfPrimitives
*/
public inline fun CharArray.partitionIndexed(predicate: (index: Int, Char) -> Boolean): Pair<List<Char>, List<Char>> {
val first = ArrayList<Char>()
val second = ArrayList<Char>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Returns a list of pairs built from the elements of `this` array and the [other] array with the same index.
* The returned list has length of the shortest collection.
Expand Down
24 changes: 23 additions & 1 deletion libraries/stdlib/common/src/generated/_Collections.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down Expand Up @@ -3192,6 +3192,28 @@ public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<Lis
return Pair(first, second)
}

/**
* Splits the original collection into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* @sample samples.collections.Iterables.Operations.partitionIndexed
*/
public inline fun <T> Iterable<T>.partitionIndexed(predicate: (index: Int, T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Returns a list containing all elements of the original collection and then the given [element].
*/
Expand Down
2 changes: 1 addition & 1 deletion libraries/stdlib/common/src/generated/_Comparisons.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
2 changes: 1 addition & 1 deletion libraries/stdlib/common/src/generated/_Maps.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
2 changes: 1 addition & 1 deletion libraries/stdlib/common/src/generated/_Ranges.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down
26 changes: 25 additions & 1 deletion libraries/stdlib/common/src/generated/_Sequences.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down Expand Up @@ -2712,6 +2712,30 @@ public inline fun <T> Sequence<T>.partition(predicate: (T) -> Boolean): Pair<Lis
return Pair(first, second)
}

/**
* Splits the original sequence into pair of lists,
* where *first* list contains elements for which [predicate] yielded `true`,
* while *second* list contains elements for which [predicate] yielded `false`.
* @param [predicate] function that takes the index of an element and the element itself
* and returns the result of predicate evaluation on the element.
*
* The operation is _terminal_.
*
* @sample samples.collections.Sequences.Transformations.partitionIndexed
*/
public inline fun <T> Sequence<T>.partitionIndexed(predicate: (index: Int, T) -> Boolean): Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
for ((idx, element) in this.withIndex()) {
if (predicate(idx, element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}

/**
* Returns a sequence containing all elements of the original sequence and then the given [element].
*
Expand Down
2 changes: 1 addition & 1 deletion libraries/stdlib/common/src/generated/_Sets.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/

Expand Down