Can anyone think of a way to make this more elegan...
# announcements
j
Can anyone think of a way to make this more elegant?
Copy code
fun <T> pair(collection: List<T>): List<Pair<T, T>> {
	if (collection.isEmpty()) {
		return emptyList()
	}
	val cap = collection.size - 1
	val first = collection.slice(0 .. cap step 2)
	val second = collection.slice(1 .. cap step 2)
	if (first.size != second.size) {
		val fill = first.last()
		return first.zip(second) + listOf(fill to fill)
	}
	return first.zip(second)
}

fun main(args: Array<String>) {
	println(pair(listOf(1,2,3,4)))
	// [(1,2),(3,4)]
	println(pair(listOf(1,2,3,4,5)))
	// [(1,2),(3,4),(5,5)]
}