StdLib: Rename method parameters (generated code)

This commit is contained in:
Ilya Gorbunov
2015-11-19 21:11:51 +03:00
parent 7e7a55bbe4
commit 0f70def3db
12 changed files with 713 additions and 700 deletions
@@ -111,34 +111,34 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("sumBy(transform: (T) -> Int)") {
templates add f("sumBy(selector: (T) -> Int)") {
inline(true)
include(CharSequences, Strings)
deprecate(Strings) { forBinaryCompatibility }
doc { f -> "Returns the sum of all values produced by [transform] function applied to each ${f.element} in the ${f.collection}." }
doc { f -> "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." }
returns("Int")
body {
"""
var sum: Int = 0
for (element in this) {
sum += transform(element)
sum += selector(element)
}
return sum
"""
}
}
templates add f("sumByDouble(transform: (T) -> Double)") {
templates add f("sumByDouble(selector: (T) -> Double)") {
inline(true)
include(CharSequences, Strings)
deprecate(Strings) { forBinaryCompatibility }
doc { f -> "Returns the sum of all values produced by [transform] function applied to each ${f.element} in the ${f.collection}." }
doc { f -> "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." }
returns("Double")
body {
"""
var sum: Double = 0.0
for (element in this) {
sum += transform(element)
sum += selector(element)
}
return sum
"""
@@ -177,7 +177,7 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("minBy(f: (T) -> R)") {
templates add f("minBy(selector: (T) -> R)") {
inline(true)
doc { f -> "Returns the first ${f.element} yielding the smallest value of the given function or `null` if there are no ${f.element}s." }
@@ -190,10 +190,10 @@ fun aggregates(): List<GenericFunction> {
if (!iterator.hasNext()) return null
var minElem = iterator.next()
var minValue = f(minElem)
var minValue = selector(minElem)
while (iterator.hasNext()) {
val e = iterator.next()
val v = f(e)
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
@@ -208,10 +208,10 @@ fun aggregates(): List<GenericFunction> {
if (isEmpty()) return null
var minElem = this[0]
var minValue = f(minElem)
var minValue = selector(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = f(e)
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
@@ -222,7 +222,7 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("minBy(f: (T) -> R)") {
templates add f("minBy(selector: (T) -> R)") {
inline(true)
only(Maps)
@@ -235,10 +235,10 @@ fun aggregates(): List<GenericFunction> {
if (!iterator.hasNext()) return null
var minElem = iterator.next()
var minValue = f(minElem)
var minValue = selector(minElem)
while (iterator.hasNext()) {
val e = iterator.next()
val v = f(e)
val v = selector(e)
if (minValue > v) {
minElem = e
minValue = v
@@ -283,7 +283,7 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("maxBy(f: (T) -> R)") {
templates add f("maxBy(selector: (T) -> R)") {
inline(true)
doc { f -> "Returns the first ${f.element} yielding the largest value of the given function or `null` if there are no ${f.element}s." }
@@ -296,10 +296,10 @@ fun aggregates(): List<GenericFunction> {
if (!iterator.hasNext()) return null
var maxElem = iterator.next()
var maxValue = f(maxElem)
var maxValue = selector(maxElem)
while (iterator.hasNext()) {
val e = iterator.next()
val v = f(e)
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
@@ -314,10 +314,10 @@ fun aggregates(): List<GenericFunction> {
if (isEmpty()) return null
var maxElem = this[0]
var maxValue = f(maxElem)
var maxValue = selector(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = f(e)
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
@@ -328,7 +328,7 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("maxBy(f: (T) -> R)") {
templates add f("maxBy(selector: (T) -> R)") {
inline(true)
only(Maps)
@@ -341,10 +341,10 @@ fun aggregates(): List<GenericFunction> {
if (!iterator.hasNext()) return null
var maxElem = iterator.next()
var maxValue = f(maxElem)
var maxValue = selector(maxElem)
while (iterator.hasNext()) {
val e = iterator.next()
val v = f(e)
val v = selector(e)
if (maxValue < v) {
maxElem = e
maxValue = v
@@ -482,30 +482,30 @@ fun aggregates(): List<GenericFunction> {
}
}
templates add f("forEach(operation: (T) -> Unit)") {
templates add f("forEach(action: (T) -> Unit)") {
inline(true)
doc { f -> "Performs the given [operation] on each ${f.element}." }
doc { f -> "Performs the given [action] on each ${f.element}." }
returns("Unit")
body {
"""
for (element in this) operation(element)
for (element in this) action(element)
"""
}
deprecate(Strings) { forBinaryCompatibility }
include(Maps, CharSequences, Strings)
}
templates add f("forEachIndexed(operation: (Int, T) -> Unit)") {
templates add f("forEachIndexed(action: (Int, T) -> Unit)") {
inline(true)
deprecate(Strings) { forBinaryCompatibility }
include(CharSequences, Strings)
doc { f -> "Performs the given [operation] on each ${f.element}, providing sequential index with the ${f.element}." }
doc { f -> "Performs the given [action] on each ${f.element}, providing sequential index with the ${f.element}." }
returns("Unit")
body {
"""
var index = 0
for (item in this) operation(index++, item)
for (item in this) action(index++, item)
"""
}
}
@@ -480,7 +480,7 @@ fun generators(): List<GenericFunction> {
}
}
templates add f("zip(array: Array<out R>, transform: (T, R) -> V)") {
templates add f("zip(other: Array<out R>, transform: (T, R) -> V)") {
exclude(Sequences)
doc {
"""
@@ -493,22 +493,22 @@ fun generators(): List<GenericFunction> {
inline(true)
body {
"""
val arraySize = array.size
val arraySize = other.size
val list = ArrayList<V>(Math.min(collectionSizeOrDefault(10), arraySize))
var i = 0
for (element in this) {
if (i >= arraySize) break
list.add(transform(element, array[i++]))
list.add(transform(element, other[i++]))
}
return list
"""
}
body(ArraysOfObjects, ArraysOfPrimitives) {
"""
val size = Math.min(size, array.size)
val size = Math.min(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], array[i]))
list.add(transform(this[i], other[i]))
}
return list
"""
@@ -516,7 +516,7 @@ fun generators(): List<GenericFunction> {
}
templates add f("zip(array: SELF, transform: (T, T) -> V)") {
templates add f("zip(other: SELF, transform: (T, T) -> V)") {
only(ArraysOfPrimitives)
doc {
"""
@@ -528,17 +528,17 @@ fun generators(): List<GenericFunction> {
inline(true)
body() {
"""
val size = Math.min(size, array.size)
val size = Math.min(size, other.size)
val list = ArrayList<V>(size)
for (i in 0..size-1) {
list.add(transform(this[i], array[i]))
list.add(transform(this[i], other[i]))
}
return list
"""
}
}
templates add f("zip(sequence: Sequence<R>, transform: (T, R) -> V)") {
templates add f("zip(other: Sequence<R>, transform: (T, R) -> V)") {
only(Sequences)
doc {
"""
@@ -550,7 +550,7 @@ fun generators(): List<GenericFunction> {
returns("Sequence<V>")
body {
"""
return MergingSequence(this, sequence, transform)
return MergingSequence(this, other, transform)
"""
}
}
@@ -614,7 +614,7 @@ fun generators(): List<GenericFunction> {
}
}
templates add f("zip(array: Array<out R>)") {
templates add f("zip(other: Array<out R>)") {
infix(true)
exclude(Sequences)
doc {
@@ -626,12 +626,12 @@ fun generators(): List<GenericFunction> {
returns("List<Pair<T, R>>")
body {
"""
return zip(array) { t1, t2 -> t1 to t2 }
return zip(other) { t1, t2 -> t1 to t2 }
"""
}
}
templates add f("zip(array: SELF)") {
templates add f("zip(other: SELF)") {
infix(true)
only(ArraysOfPrimitives)
doc {
@@ -642,17 +642,17 @@ fun generators(): List<GenericFunction> {
returns("List<Pair<T, T>>")
body {
"""
return zip(array) { t1, t2 -> t1 to t2 }
return zip(other) { t1, t2 -> t1 to t2 }
"""
}
}
templates add f("zip(sequence: Sequence<R>)") {
templates add f("zip(other: Sequence<R>)") {
infix(true)
only(Sequences)
doc {
"""
Returns a sequence of pairs built from elements of both collections with same indexes.
Returns a sequence of pairs built from elements of both sequences with same indexes.
Resulting sequence has length of shortest input sequence.
"""
}
@@ -660,7 +660,7 @@ fun generators(): List<GenericFunction> {
returns("Sequence<Pair<T, R>>")
body {
"""
return MergingSequence(this, sequence) { t1, t2 -> t1 to t2 }
return MergingSequence(this, other) { t1, t2 -> t1 to t2 }
"""
}
}
@@ -284,29 +284,29 @@ fun mapping(): List<GenericFunction> {
}
}
templates add f("groupBy(toKey: (T) -> K)") {
templates add f("groupBy(selector: (T) -> K)") {
inline(true)
deprecate(Strings) { forBinaryCompatibility }
include(CharSequences, Strings)
doc { f -> "Returns a map of the ${f.element}s in original ${f.collection} grouped by the result of given [toKey] function." }
doc { f -> "Returns a map of the ${f.element}s in original ${f.collection} grouped by the key returned by the given [selector] function." }
typeParam("K")
returns("Map<K, List<T>>")
body { "return groupByTo(LinkedHashMap<K, MutableList<T>>(), toKey)" }
body { "return groupByTo(LinkedHashMap<K, MutableList<T>>(), selector)" }
}
templates add f("groupByTo(map: MutableMap<K, MutableList<T>>, toKey: (T) -> K)") {
templates add f("groupByTo(map: MutableMap<K, MutableList<T>>, selector: (T) -> K)") {
inline(true)
deprecate(Strings) { forBinaryCompatibility }
include(CharSequences, Strings)
typeParam("K")
doc { f -> "Appends ${f.element}s from original ${f.collection} grouped by the result of given [toKey] function to the given [map]." }
doc { f -> "Appends ${f.element}s from original ${f.collection} grouped by the key returned by the given [selector] function to the given [map]." }
returns("Map<K, MutableList<T>>")
body {
"""
for (element in this) {
val key = toKey(element)
val key = selector(element)
val list = map.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
@@ -50,11 +50,12 @@ fun sets(): List<GenericFunction> {
body(Sequences) { "return this.distinctBy { it }" }
}
templates add f("distinctBy(keySelector: (T) -> K)") {
templates add f("distinctBy(selector: (T) -> K)") {
exclude(Strings)
doc { f ->
"""
Returns a ${f.mapResult} containing only distinct ${f.element}s from the given ${f.collection} according to the [keySelector].
Returns a ${f.mapResult} containing only ${f.element}s from the given ${f.collection}
having distinct keys returned by the given [selector] function.
The ${f.element}s in the resulting ${f.mapResult} are in the same order as they were in the source ${f.collection}.
"""
@@ -68,7 +69,7 @@ fun sets(): List<GenericFunction> {
val set = HashSet<K>()
val list = ArrayList<T>()
for (e in this) {
val key = keySelector(e)
val key = selector(e)
if (set.add(key))
list.add(e)
}
@@ -80,7 +81,7 @@ fun sets(): List<GenericFunction> {
returns(Sequences) { "Sequence<T>" }
body(Sequences) {
"""
return DistinctSequence(this, keySelector)
return DistinctSequence(this, selector)
"""
}
@@ -5,18 +5,18 @@ import templates.Family.*
fun snapshots(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
templates add f("toCollection(collection: C)") {
templates add f("toCollection(destination: C)") {
deprecate(Strings) { forBinaryCompatibility }
include(CharSequences, Strings)
doc { f -> "Appends all ${f.element}s to the given [collection]." }
doc { f -> "Appends all ${f.element}s to the given [destination] collection." }
returns("C")
typeParam("C : MutableCollection<in T>")
body {
"""
for (item in this) {
collection.add(item)
destination.add(item)
}
return collection
return destination
"""
}
}
@@ -244,15 +244,16 @@ fun specialJVM(): List<GenericFunction> {
"""
}
// TODO: Use own readonly kotlin.AbstractList
body(ArraysOfPrimitives) {
"""
return object : AbstractList<T>(), RandomAccess {
override val size: Int get() = this@asList.size
override fun isEmpty(): Boolean = this@asList.isEmpty()
override fun contains(o: T): Boolean = this@asList.contains(o)
override fun contains(element: T): Boolean = this@asList.contains(element)
override fun get(index: Int): T = this@asList[index]
override fun indexOf(o: T): Int = this@asList.indexOf(o)
override fun lastIndexOf(o: T): Int = this@asList.lastIndexOf(o)
override fun indexOf(element: T): Int = this@asList.indexOf(element)
override fun lastIndexOf(element: T): Int = this@asList.lastIndexOf(element)
}
"""
}