operator fun <T> Iterable<T>.plus(element: T): List<T> {
if (this is Collection) return this.plus(element)
val result = ArrayList<T>()
result.addAll(this)
result.add(element)
return result
}
inline fun <T, R> Iterable<T>.map(
transform: (T) -> R
): List<R> {
val size = if (this is Collection<*>) this.size else 10
val destination = ArrayList<R>(size)
for (item in this)
destination.add(transform(item))
return destination
}
替代不可变集合的实现:
// 这不是 map 实际如何实现的
inline fun <T, R> Iterable<T>.map(
transform: (T) -> R
): List<R> {
var destination = listOf<R>()
for (item in this)
destination += transform(item)
return destination
}