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]
a.flatMap { it.repeat(5) }
? it's only 8 characters longer, but much shorter thenflatMap{ x -> List(5) { x } }
\$\endgroup\$