1
\$\begingroup\$

I wanted to make a library thing which provides specific extensions for collections/collection-like containers in kotlin:

fun <T> Collection<T>.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }

fun String.extendEntries(length: Int) = this.toList().extendEntries(length)
fun <T> Array<T>.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun CharArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun BooleanArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun DoubleArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun IntArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun LongArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun FloatArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun ByteArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }
fun ShortArray.extendEntries(length: Int) = this.flatMap { x -> List(length) { x } }

Is the above a good way to do it if I want to cover arrays?

Exampe usage:

val a = intArrayOf(1, 2, 3)
println(a.extendEntries(5))

Outputs:

[1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3]
\$\endgroup\$
1
  • \$\begingroup\$ What if you instead did a.flatMap { it.repeat(5) } ? it's only 8 characters longer, but much shorter then flatMap{ x -> List(5) { x } } \$\endgroup\$ Commented Aug 19, 2020 at 21:59

1 Answer 1

2
\$\begingroup\$

Sadly, yes. If you want users of your library to write intArrayOf(1, 2, 3).extendEntries(5) then this is how it is done in Kotlin.

In fact, the Kotlin source code contains methods similar to it, with a specific function for each type of array: (kotlin.collections._Arrays.kt)

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array.
 */
public inline fun <R> ShortArray.flatMap(transform: (Short) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array.
 */
public inline fun <R> IntArray.flatMap(transform: (Int) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array.
 */
public inline fun <R> LongArray.flatMap(transform: (Long) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

However, Kotlin uses a generator to generate such files, see the README at https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib

In your case, you might consider just implementing Iterable<T>.extendEntries as all of these specific types of arrays can be transformed to an Iterable<T> using the Kotlin stdlib .asIterable function.

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.