operatorfun<T> Iterable<T>.plus(element: T): List<T> { if (thisis Collection) returnthis.plus(element) val result = ArrayList<T>() result.addAll(this) result.add(element) return result }
inlinefun<T, R> Iterable<T>.map( transform: (T) -> R ): List<R> { val size = if (thisis Collection<*>) this.size else10 val destination = ArrayList<R>(size) for (item inthis) destination.add(transform(item)) return destination }
而不是不可变集合:
1 2 3 4 5 6 7 8 9
// This is not how map is implemented inlinefun<T, R> Iterable<T>.map( transform: (T) -> R ): List<R> { var destination = listOf<R>() for (item inthis) destination += transform(item) return destination }