Rewrite Aggregates.kt to the new template DSL

This commit is contained in:
Ilya Gorbunov
2017-11-02 07:29:25 +03:00
parent dd0e04edd5
commit 012d3804f0
2 changed files with 293 additions and 235 deletions
@@ -19,7 +19,7 @@ fun main(args: Array<String>) {
// Snapshots, // Snapshots,
// Mapping, // Mapping,
// SetOps, // SetOps,
// Aggregates, Aggregates,
// Guards, // Guards,
// Generators, // Generators,
// StringJoinOps, // StringJoinOps,
@@ -1,16 +1,42 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package templates package templates
import templates.Family.* import templates.Family.*
import templates.SequenceClass.* import templates.SequenceClass.*
fun aggregates(): List<GenericFunction> { object Aggregates : TemplateGroupBase() {
val templates = arrayListOf<GenericFunction>()
templates add f("all(predicate: (T) -> Boolean)") { init {
inline(true) defaultBuilder {
doc { f -> "Returns `true` if all ${f.element.pluralize()} match the given [predicate]." } if (sequenceClassification.isEmpty()) {
sequenceClassification(terminal)
}
}
}
val f_all = fn("all(predicate: (T) -> Boolean)") {
includeDefault()
include(Maps, CharSequences)
} builder {
inline()
doc { "Returns `true` if all ${f.element.pluralize()} match the given [predicate]." }
returns("Boolean") returns("Boolean")
body { f -> body {
""" """
${when (f) { ${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return true" Iterables -> "if (this is Collection && isEmpty()) return true"
@@ -21,15 +47,17 @@ fun aggregates(): List<GenericFunction> {
return true return true
""" """
} }
include(Maps, CharSequences)
} }
templates add f("none(predicate: (T) -> Boolean)") { val f_none_predicate = fn("none(predicate: (T) -> Boolean)") {
inline(true) includeDefault()
include(Maps, CharSequences)
} builder {
inline()
doc { f -> "Returns `true` if no ${f.element.pluralize()} match the given [predicate]." } doc { "Returns `true` if no ${f.element.pluralize()} match the given [predicate]." }
returns("Boolean") returns("Boolean")
body { f -> body {
""" """
${when (f) { ${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return true" Iterables -> "if (this is Collection && isEmpty()) return true"
@@ -40,33 +68,39 @@ fun aggregates(): List<GenericFunction> {
return true return true
""" """
} }
include(Maps, CharSequences)
} }
templates add f("none()") { val f_none = fn("none()") {
doc { f -> "Returns `true` if the ${f.collection} has no ${f.element.pluralize()}." } includeDefault()
include(Maps, CharSequences)
} builder {
doc { "Returns `true` if the ${f.collection} has no ${f.element.pluralize()}." }
returns("Boolean") returns("Boolean")
body { body {
"return !iterator().hasNext()" "return !iterator().hasNext()"
} }
body(Iterables) { specialFor(Iterables) {
""" body {
if (this is Collection) return isEmpty() """
return !iterator().hasNext() if (this is Collection) return isEmpty()
""" return !iterator().hasNext()
"""
}
} }
body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) { specialFor(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
"return isEmpty()" body { "return isEmpty()" }
} }
include(Maps, CharSequences)
} }
templates add f("any(predicate: (T) -> Boolean)") { val f_any_predicate = fn("any(predicate: (T) -> Boolean)") {
inline(true) includeDefault()
include(Maps, CharSequences)
} builder {
inline()
doc { f -> "Returns `true` if at least one ${f.element} matches the given [predicate]." } doc { "Returns `true` if at least one ${f.element} matches the given [predicate]." }
returns("Boolean") returns("Boolean")
body { f -> body {
""" """
${when (f) { ${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return false" Iterables -> "if (this is Collection && isEmpty()) return false"
@@ -77,11 +111,13 @@ fun aggregates(): List<GenericFunction> {
return false return false
""" """
} }
include(Maps, CharSequences)
} }
templates add f("any()") { val f_any = fn("any()") {
doc { f -> "Returns `true` if ${f.collection} has at least one ${f.element}." } includeDefault()
include(Maps, CharSequences)
} builder {
doc { "Returns `true` if ${f.collection} has at least one ${f.element}." }
returns("Boolean") returns("Boolean")
body { body {
"return iterator().hasNext()" "return iterator().hasNext()"
@@ -92,18 +128,19 @@ fun aggregates(): List<GenericFunction> {
return iterator().hasNext() return iterator().hasNext()
""" """
} }
body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) { body(Maps, CharSequences, ArraysOfObjects, ArraysOfPrimitives) { "return !isEmpty()" }
"return !isEmpty()"
}
include(Maps, CharSequences)
} }
templates add f("count(predicate: (T) -> Boolean)") {
inline(true)
doc { f -> "Returns the number of ${f.element.pluralize()} matching the given [predicate]." } val f_count_predicate = fn("count(predicate: (T) -> Boolean)") {
includeDefault()
include(Maps, CharSequences)
} builder {
inline()
doc { "Returns the number of ${f.element.pluralize()} matching the given [predicate]." }
returns("Int") returns("Int")
body { f -> body {
""" """
${when (f) { ${when (f) {
Iterables -> "if (this is Collection && isEmpty()) return 0" Iterables -> "if (this is Collection && isEmpty()) return 0"
@@ -115,34 +152,38 @@ fun aggregates(): List<GenericFunction> {
return count return count
""" """
} }
include(Maps, CharSequences)
} }
templates add f("count()") { val f_count = fn("count()") {
doc { f -> "Returns the number of ${f.element.pluralize()} in this ${f.collection}." } includeDefault()
include(Collections, Maps, CharSequences)
} builder {
doc { "Returns the number of ${f.element.pluralize()} in this ${f.collection}." }
returns("Int") returns("Int")
body { f -> body {
""" """
${if (f == Iterables) "if (this is Collection) return size" else "" } ${if (f == Iterables) "if (this is Collection) return size" else ""}
var count = 0 var count = 0
for (element in this) count++ for (element in this) count++
return count return count
""" """
} }
inline(CharSequences, Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only } specialFor(CharSequences, Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { inlineOnly() }
doc(CharSequences) { "Returns the length of this char sequence."} specialFor(CharSequences) {
body(CharSequences) { doc { "Returns the length of this char sequence." }
"return length" body { "return length" }
} }
body(Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) { specialFor(Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) {
"return size" body { "return size" }
} }
} }
templates add f("sumBy(selector: (T) -> Int)") { val f_sumBy = fn("sumBy(selector: (T) -> Int)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
doc { f -> "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." } } builder {
inline()
doc { "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." }
returns("Int") returns("Int")
body { body {
""" """
@@ -155,10 +196,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("sumByDouble(selector: (T) -> Double)") { val f_sumByDouble = fn("sumByDouble(selector: (T) -> Double)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
doc { f -> "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." } } builder {
inline()
doc { "Returns the sum of all values produced by [selector] function applied to each ${f.element} in the ${f.collection}." }
returns("Double") returns("Double")
body { body {
""" """
@@ -172,72 +215,73 @@ fun aggregates(): List<GenericFunction> {
} }
templates addAll listOf("min", "max").flatMap { op -> val f_minMax = run {
val genericSpecializations = PrimitiveType.numericPrimitives.filterNot { it.isIntegral() } + listOf(null) val genericSpecializations = PrimitiveType.numericPrimitives.filterNot { it.isIntegral() }.toSet() + setOf(null)
listOf( listOf("min", "max").map { op ->
Iterables to genericSpecializations, fn("$op()") {
Sequences to genericSpecializations, include(Iterables, genericSpecializations)
ArraysOfObjects to genericSpecializations, include(Sequences, genericSpecializations)
ArraysOfPrimitives to (PrimitiveType.defaultPrimitives - PrimitiveType.Boolean), include(ArraysOfObjects, genericSpecializations)
CharSequences to setOf(null) include(ArraysOfPrimitives, PrimitiveType.defaultPrimitives - PrimitiveType.Boolean)
).map { (f, primitives) -> primitives.map { primitive -> include(CharSequences)
f("$op()") { } builder {
val isFloat = primitive?.isIntegral() == false val isFloat = primitive?.isIntegral() == false
val isGeneric = f in listOf(Iterables, Sequences, ArraysOfObjects) val isGeneric = f in listOf(Iterables, Sequences, ArraysOfObjects)
only(f)
typeParam("T : Comparable<T>") typeParam("T : Comparable<T>")
if (primitive != null) { if (primitive != null) {
onlyPrimitives(f, primitive)
if (isFloat && isGeneric) if (isFloat && isGeneric)
since("1.1") since("1.1")
} }
doc { f -> doc {
"Returns the ${if (op == "max") "largest" else "smallest"} ${f.element} or `null` if there are no ${f.element.pluralize()}." + "Returns the ${if (op == "max") "largest" else "smallest"} ${f.element} or `null` if there are no ${f.element.pluralize()}." +
if (isFloat) "\n\n" + "If any of ${f.element.pluralize()} is `NaN` returns `NaN`." else "" if (isFloat) "\n\n" + "If any of ${f.element.pluralize()} is `NaN` returns `NaN`." else ""
} }
returns("T?") returns("T?")
body { body {
if (f == ArraysOfObjects || f == ArraysOfPrimitives || f == CharSequences) { """
""" val iterator = iterator()
if (isEmpty()) return null if (!iterator.hasNext()) return null
var $op = this[0] var $op = iterator.next()
${if (isFloat) "if ($op.isNaN()) return $op" else "\\"} ${if (isFloat) "if ($op.isNaN()) return $op" else "\\"}
for (i in 1..lastIndex) { while (iterator.hasNext()) {
val e = this[i] val e = iterator.next()
${if (isFloat) "if (e.isNaN()) return e" else "\\"} ${if (isFloat) "if (e.isNaN()) return e" else "\\"}
if ($op ${if (op == "max") "<" else ">"} e) $op = e if ($op ${if (op == "max") "<" else ">"} e) $op = e
}
return $op
"""
} }
else { return $op
""" """
val iterator = iterator() }
if (!iterator.hasNext()) return null body(ArraysOfObjects, ArraysOfPrimitives, CharSequences) {
var $op = iterator.next() """
${if (isFloat) "if ($op.isNaN()) return $op" else "\\"} if (isEmpty()) return null
var $op = this[0]
${if (isFloat) "if ($op.isNaN()) return $op" else "\\"}
while (iterator.hasNext()) { for (i in 1..lastIndex) {
val e = iterator.next() val e = this[i]
${if (isFloat) "if (e.isNaN()) return e" else "\\"} ${if (isFloat) "if (e.isNaN()) return e" else "\\"}
if ($op ${if (op == "max") "<" else ">"} e) $op = e if ($op ${if (op == "max") "<" else ">"} e) $op = e
} }
return $op return $op
""" """
}.replace(Regex("""^\s+\\\n""", RegexOption.MULTILINE), "") // trim lines ending with \ }
body {
body!!.replace(Regex("""^\s+\\\n""", RegexOption.MULTILINE), "") // remove empty lines ending with \
} }
} }
}} }
}.flatten() }
templates add f("minBy(selector: (T) -> R)") { val f_minBy = fn("minBy(selector: (T) -> R)") {
inline(true) includeDefault()
include(Maps, CharSequences)
doc { f -> "Returns the first ${f.element} yielding the smallest value of the given function or `null` if there are no ${f.element.pluralize()}." } } builder {
inline()
doc { "Returns the first ${f.element} yielding the smallest value of the given function or `null` if there are no ${f.element.pluralize()}." }
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
returns("T?") returns("T?")
body { body {
@@ -275,11 +319,16 @@ fun aggregates(): List<GenericFunction> {
return minElem return minElem
""" """
} }
body(Maps) { "return entries.minBy(selector)" } body(Maps) {
"return entries.minBy(selector)"
}
} }
templates add f("minWith(comparator: Comparator<in T>)") { val f_minWith = fn("minWith(comparator: Comparator<in T>)") {
doc { f -> "Returns the first ${f.element} having the smallest value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." } includeDefault()
include(Maps, CharSequences)
} builder {
doc { "Returns the first ${f.element} having the smallest value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." }
returns("T?") returns("T?")
body { body {
""" """
@@ -308,10 +357,13 @@ fun aggregates(): List<GenericFunction> {
body(Maps) { "return entries.minWith(comparator)" } body(Maps) { "return entries.minWith(comparator)" }
} }
templates add f("maxBy(selector: (T) -> R)") { val f_maxBy = fn("maxBy(selector: (T) -> R)") {
inline(true) includeDefault()
include(Maps, CharSequences)
} builder {
inline()
doc { f -> "Returns the first ${f.element} yielding the largest value of the given function or `null` if there are no ${f.element.pluralize()}." } doc { "Returns the first ${f.element} yielding the largest value of the given function or `null` if there are no ${f.element.pluralize()}." }
typeParam("R : Comparable<R>") typeParam("R : Comparable<R>")
returns("T?") returns("T?")
body { body {
@@ -349,25 +401,30 @@ fun aggregates(): List<GenericFunction> {
return maxElem return maxElem
""" """
} }
inline(Maps) { Inline.Only } specialFor(Maps) {
body(Maps) { "return entries.maxBy(selector)" } inlineOnly()
body { "return entries.maxBy(selector)" }
}
} }
templates add f("maxWith(comparator: Comparator<in T>)") { val f_maxWith = fn("maxWith(comparator: Comparator<in T>)") {
doc { f -> "Returns the first ${f.element} having the largest value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." } includeDefault()
include(Maps, CharSequences)
} builder {
doc { "Returns the first ${f.element} having the largest value according to the provided [comparator] or `null` if there are no ${f.element.pluralize()}." }
returns("T?") returns("T?")
body { body {
""" """
val iterator = iterator() val iterator = iterator()
if (!iterator.hasNext()) return null if (!iterator.hasNext()) return null
var max = iterator.next() var max = iterator.next()
while (iterator.hasNext()) { while (iterator.hasNext()) {
val e = iterator.next() val e = iterator.next()
if (comparator.compare(max, e) < 0) max = e if (comparator.compare(max, e) < 0) max = e
} }
return max return max
""" """
} }
body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) { body(CharSequences, ArraysOfObjects, ArraysOfPrimitives) {
""" """
@@ -381,15 +438,18 @@ fun aggregates(): List<GenericFunction> {
return max return max
""" """
} }
inline(Maps) { Inline.Only } specialFor(Maps) {
body(Maps) { "return entries.maxWith(comparator)" } inlineOnly()
body { "return entries.maxWith(comparator)" }
}
} }
templates add f("foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") { val f_foldIndexed = fn("foldIndexed(initial: R, operation: (index: Int, acc: R, T) -> R)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
doc { f -> } builder {
inline()
doc {
""" """
Accumulates value starting with [initial] value and applying [operation] from left to right Accumulates value starting with [initial] value and applying [operation] from left to right
to current accumulator value and each ${f.element} with its index in the original ${f.collection}. to current accumulator value and each ${f.element} with its index in the original ${f.collection}.
@@ -409,11 +469,11 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R)") { val f_foldRightIndexed = fn("foldRightIndexed(initial: R, operation: (index: Int, T, acc: R) -> R)") {
inline(true) include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
} builder {
only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) inline()
doc { f -> doc {
""" """
Accumulates value starting with [initial] value and applying [operation] from right to left Accumulates value starting with [initial] value and applying [operation] from right to left
to each ${f.element} with its index in the original ${f.collection} and current accumulator value. to each ${f.element} with its index in the original ${f.collection} and current accumulator value.
@@ -449,11 +509,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("fold(initial: R, operation: (acc: R, T) -> R)") { val f_fold = fn("fold(initial: R, operation: (acc: R, T) -> R)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
doc { f -> "Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each ${f.element}." } } builder {
inline()
doc { "Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each ${f.element}." }
typeParam("R") typeParam("R")
returns("R") returns("R")
body { body {
@@ -465,11 +526,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("foldRight(initial: R, operation: (T, acc: R) -> R)") { val f_foldRight = fn("foldRight(initial: R, operation: (T, acc: R) -> R)") {
inline(true) include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
} builder {
inline()
only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) doc { "Accumulates value starting with [initial] value and applying [operation] from right to left to each ${f.element} and current accumulator value." }
doc { f -> "Accumulates value starting with [initial] value and applying [operation] from right to left to each ${f.element} and current accumulator value." }
typeParam("R") typeParam("R")
returns("R") returns("R")
body { body {
@@ -496,11 +558,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduceIndexed(operation: (index: Int, acc: T, T) -> T)") { val f_reduceIndexed = fn("reduceIndexed(operation: (index: Int, acc: T, T) -> T)") {
inline(true) include(ArraysOfPrimitives, CharSequences)
only(ArraysOfPrimitives, CharSequences) } builder {
inline()
doc { f -> doc {
""" """
Accumulates value starting with the first ${f.element} and applying [operation] from left to right Accumulates value starting with the first ${f.element} and applying [operation] from left to right
to current accumulator value and each ${f.element} with its index in the original ${f.collection}. to current accumulator value and each ${f.element} with its index in the original ${f.collection}.
@@ -509,7 +572,7 @@ fun aggregates(): List<GenericFunction> {
""" """
} }
returns("T") returns("T")
body { f -> body {
""" """
if (isEmpty()) if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -523,11 +586,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduceIndexed(operation: (index: Int, acc: S, T) -> S)") { val f_reduceIndexedSuper = fn("reduceIndexed(operation: (index: Int, acc: S, T) -> S)") {
inline(true) include(ArraysOfObjects, Iterables, Sequences)
only(ArraysOfObjects, Iterables, Sequences) } builder {
inline()
doc { f -> doc {
""" """
Accumulates value starting with the first ${f.element} and applying [operation] from left to right Accumulates value starting with the first ${f.element} and applying [operation] from left to right
to current accumulator value and each ${f.element} with its index in the original ${f.collection}. to current accumulator value and each ${f.element} with its index in the original ${f.collection}.
@@ -538,7 +602,7 @@ fun aggregates(): List<GenericFunction> {
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { f -> body {
""" """
val iterator = this.iterator() val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -551,7 +615,7 @@ fun aggregates(): List<GenericFunction> {
return accumulator return accumulator
""" """
} }
body(ArraysOfObjects) { f -> body(ArraysOfObjects) {
""" """
if (isEmpty()) if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -565,11 +629,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduceRightIndexed(operation: (index: Int, T, acc: T) -> T)") { val f_reduceRightIndexed = fn("reduceRightIndexed(operation: (index: Int, T, acc: T) -> T)") {
inline(true) include(CharSequences, ArraysOfPrimitives)
} builder {
inline()
only(CharSequences, ArraysOfPrimitives) doc {
doc { f ->
""" """
Accumulates value starting with last ${f.element} and applying [operation] from right to left Accumulates value starting with last ${f.element} and applying [operation] from right to left
to each ${f.element} with its index in the original ${f.collection} and current accumulator value. to each ${f.element} with its index in the original ${f.collection} and current accumulator value.
@@ -578,7 +643,7 @@ fun aggregates(): List<GenericFunction> {
""" """
} }
returns("T") returns("T")
body { f -> body {
""" """
var index = lastIndex var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -594,11 +659,12 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduceRightIndexed(operation: (index: Int, T, acc: S) -> S)") { val f_reduceRightIndexedSuper = fn("reduceRightIndexed(operation: (index: Int, T, acc: S) -> S)") {
inline(true) include(Lists, ArraysOfObjects)
} builder {
inline()
only(Lists, ArraysOfObjects) doc {
doc { f ->
""" """
Accumulates value starting with last ${f.element} and applying [operation] from right to left Accumulates value starting with last ${f.element} and applying [operation] from right to left
to each ${f.element} with its index in the original ${f.collection} and current accumulator value. to each ${f.element} with its index in the original ${f.collection} and current accumulator value.
@@ -609,7 +675,7 @@ fun aggregates(): List<GenericFunction> {
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { f -> body {
""" """
var index = lastIndex var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -640,13 +706,14 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduce(operation: (acc: T, T) -> T)") { val f_reduce = fn("reduce(operation: (acc: T, T) -> T)") {
inline(true) include(ArraysOfPrimitives, CharSequences)
only(ArraysOfPrimitives, CharSequences) } builder {
inline()
doc { f -> "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." } doc { "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." }
returns("T") returns("T")
body { f -> body {
""" """
if (isEmpty()) if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -660,15 +727,16 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduce(operation: (acc: S, T) -> S)") { val f_reduceSuper = fn("reduce(operation: (acc: S, T) -> S)") {
inline(true) include(ArraysOfObjects, Iterables, Sequences)
only(ArraysOfObjects, Iterables, Sequences) } builder {
inline()
doc { f -> "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." } doc { "Accumulates value starting with the first ${f.element} and applying [operation] from left to right to current accumulator value and each ${f.element}." }
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { f -> body {
""" """
val iterator = this.iterator() val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") if (!iterator.hasNext()) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -680,7 +748,7 @@ fun aggregates(): List<GenericFunction> {
return accumulator return accumulator
""" """
} }
body(ArraysOfObjects) { f -> body(ArraysOfObjects) {
""" """
if (isEmpty()) if (isEmpty())
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -694,13 +762,14 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduceRight(operation: (T, acc: T) -> T)") { val f_reduceRight = fn("reduceRight(operation: (T, acc: T) -> T)") {
inline(true) include(CharSequences, ArraysOfPrimitives)
} builder {
inline()
only(CharSequences, ArraysOfPrimitives) doc { "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." }
doc { f -> "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." }
returns("T") returns("T")
body { f -> body {
""" """
var index = lastIndex var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -715,15 +784,15 @@ fun aggregates(): List<GenericFunction> {
} }
} }
templates add f("reduceRight(operation: (T, acc: S) -> S)") { val f_reduceRightSuper = fn("reduceRight(operation: (T, acc: S) -> S)") {
inline(true) include(Lists, ArraysOfObjects)
} builder {
only(Lists, ArraysOfObjects) inline()
doc { f -> "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." } doc { "Accumulates value starting with last ${f.element} and applying [operation] from right to left to each ${f.element} and current accumulator value." }
typeParam("S") typeParam("S")
typeParam("T: S") typeParam("T: S")
returns("S") returns("S")
body { f -> body {
""" """
var index = lastIndex var index = lastIndex
if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.") if (index < 0) throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
@@ -752,66 +821,63 @@ fun aggregates(): List<GenericFunction> {
} }
} }
val f_onEach = fn("onEach(action: (T) -> Unit)") {
templates addAll listOf(Iterables, Maps, CharSequences).map { f -> f("onEach(action: (T) -> Unit)") { include(Iterables, Maps, CharSequences, Sequences)
only(f) } builder {
since("1.1") since("1.1")
inline(true)
doc { f -> "Performs the given [action] on each ${f.element} and returns the ${f.collection} itself afterwards." }
val collectionType = when(f) {
Maps -> "M"
CharSequences -> "S"
else -> "C"
}
customReceiver(collectionType)
returns(collectionType)
typeParam("$collectionType : SELF")
body { specialFor(Iterables, Maps, CharSequences) {
""" inline()
return apply { for (element in this) action(element) } doc { "Performs the given [action] on each ${f.element} and returns the ${f.collection} itself afterwards." }
""" val collectionType = when(f) {
} Maps -> "M"
}} CharSequences -> "S"
else -> "C"
templates add f("onEach(action: (T) -> Unit)") { }
only(Sequences) receiver(collectionType)
since("1.1") returns(collectionType)
returns("SELF") typeParam("$collectionType : SELF")
doc { f ->
""" body { "return apply { for (element in this) action(element) }" }
Returns a sequence which performs the given [action] on each ${f.element} of the original sequence as they pass though it. }
"""
} specialFor(Sequences) {
sequenceClassification(intermediate, stateless) returns("SELF")
body(Sequences) { doc { "Returns a sequence which performs the given [action] on each ${f.element} of the original sequence as they pass though it." }
""" sequenceClassification(intermediate, stateless)
return map { body {
action(it) """
it return map {
action(it)
it
}
"""
} }
"""
} }
} }
templates add f("forEach(action: (T) -> Unit)") { val f_forEach = fn("forEach(action: (T) -> Unit)") {
inline(true) includeDefault()
include(Maps, CharSequences)
} builder {
inline()
doc { f -> "Performs the given [action] on each ${f.element}." } doc { "Performs the given [action] on each ${f.element}." }
annotations(Iterables, Maps) { "@kotlin.internal.HidesMembers" } specialFor(Iterables, Maps) { annotation("@kotlin.internal.HidesMembers") }
returns("Unit") returns("Unit")
body { body {
""" """
for (element in this) action(element) for (element in this) action(element)
""" """
} }
include(Maps, CharSequences)
} }
templates add f("forEachIndexed(action: (index: Int, T) -> Unit)") { val f_forEachIndexed = fn("forEachIndexed(action: (index: Int, T) -> Unit)") {
inline(true) includeDefault()
include(CharSequences) include(CharSequences)
doc { f -> } builder {
inline()
doc {
""" """
Performs the given [action] on each ${f.element}, providing sequential index with the ${f.element}. Performs the given [action] on each ${f.element}, providing sequential index with the ${f.element}.
@param [action] function that takes the index of ${f.element.prefixWithArticle()} and the ${f.element} itself @param [action] function that takes the index of ${f.element.prefixWithArticle()} and the ${f.element} itself
@@ -825,12 +891,4 @@ fun aggregates(): List<GenericFunction> {
""" """
} }
} }
}
templates.forEach {
if (it.sequenceClassification.isEmpty()) {
it.sequenceClassification(terminal)
}
}
return templates
}