From ff336cdf840d941ff3b383e8c6f41f4de2166604 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Sat, 4 Nov 2017 06:47:54 +0300 Subject: [PATCH] Rewrite Generators.kt in new DSL --- .../src/generators/GenerateStandardLib.kt | 2 +- .../src/templates/Generators.kt | 830 ++++++++++-------- 2 files changed, 454 insertions(+), 378 deletions(-) diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 61515d699ae..364a4759e52 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -21,7 +21,7 @@ fun main(args: Array) { // SetOps, Aggregates, // Guards, -// Generators, + Generators, // StringJoinOps, // SequenceOps, // RangeOps, diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index ea0ba4d09d4..c72211fb8bd 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -19,33 +19,38 @@ package templates import templates.Family.* import templates.SequenceClass.* -fun generators(): List { - val templates = arrayListOf() +object Generators : TemplateGroupBase() { - templates add f("plusElement(element: T)") { - inline(Inline.Only) + val f_plusElement = fn("plusElement(element: T)") { + include(Iterables, Collections, Sets, Sequences) + } builder { + inlineOnly() - only(Iterables, Collections, Sets, Sequences) doc { "Returns a list containing all elements of the original collection and then the given [element]." } - doc(Sets) { - """ + specialFor(Sets) { + doc { + """ Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. The returned set preserves the element iteration order of the original set. """ + } + } + specialFor(Sequences) { + doc { "Returns a sequence containing all elements of the original sequence and then the given [element]." } } - doc(Sequences) { "Returns a sequence containing all elements of the original sequence and then the given [element]." } sequenceClassification(intermediate, stateless) returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { "return plus(element)" } } - templates add f("plus(element: T)") { + val f_plus = fn("plus(element: T)") { + include(Iterables, Collections, Sets, Sequences) + } builder { operator(true) - only(Iterables, Collections, Sets, Sequences) doc { "Returns a list containing all elements of the original collection and then the given [element]." } sequenceClassification(intermediate, stateless) returns("List") @@ -67,39 +72,43 @@ fun generators(): List { """ } - // TODO: use immutable sets when available - returns("SELF", Sets, Sequences) - doc(Sets) { - """ - Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. + specialFor(Sets, Sequences) { returns("SELF") } + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set and then the given [element] if it isn't already in this set. - The returned set preserves the element iteration order of the original set. - """ - } - body(Sets) { - """ - val result = LinkedHashSet(mapCapacity(size + 1)) - result.addAll(this) - result.add(element) - return result - """ + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(mapCapacity(size + 1)) + result.addAll(this) + result.add(element) + return result + """ + } } - doc(Sequences) { "Returns a sequence containing all elements of the original sequence and then the given [element]." } - body(Sequences) { - """ - return sequenceOf(this, sequenceOf(element)).flatten() - """ + specialFor(Sequences) { + doc { "Returns a sequence containing all elements of the original sequence and then the given [element]." } + body { + """ + return sequenceOf(this, sequenceOf(element)).flatten() + """ + } } } - templates add f("plus(elements: Iterable)") { + val f_plus_iterable = fn("plus(elements: Iterable)") { + include(Iterables, Collections, Sets, Sequences) + } builder { operator(true) - only(Iterables, Collections, Sets, Sequences) - doc { f -> "Returns a list containing all elements of the original ${f.collection} and then all elements of the given [elements] collection." } + doc { "Returns a list containing all elements of the original ${f.collection} and then all elements of the given [elements] collection." } returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { """ if (this is Collection) return this.plus(elements) @@ -124,46 +133,50 @@ fun generators(): List { """ } - // TODO: use immutable set builder when available - doc(Sets) { - """ - Returns a set containing all elements of the original set and the given [elements] collection, - which aren't already in this set. - The returned set preserves the element iteration order of the original set. - """ + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set and the given [elements] collection, + which aren't already in this set. + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(mapCapacity(elements.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2)) + result.addAll(this) + result.addAll(elements) + return result + """ + } } - body(Sets) { - """ - val result = LinkedHashSet(mapCapacity(elements.collectionSizeOrNull()?.let { this.size + it } ?: this.size * 2)) - result.addAll(this) - result.addAll(elements) - return result - """ + specialFor(Sequences) { + doc { + """ + Returns a sequence containing all elements of original sequence and then all elements of the given [elements] collection. + + Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from + the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + """ + } + sequenceClassification(intermediate, stateless) + body { + """ + return sequenceOf(this, elements.asSequence()).flatten() + """ + } } - doc(Sequences) { - """ - Returns a sequence containing all elements of original sequence and then all elements of the given [elements] collection. - - Note that the source sequence and the collection being added are iterated only when an `iterator` is requested from - the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - """ - } - sequenceClassification(intermediate, stateless) - body(Sequences) { - """ - return sequenceOf(this, elements.asSequence()).flatten() - """ - } } - templates add f("plus(elements: Array)") { + val f_plus_array = fn("plus(elements: Array)") { + include(Iterables, Collections, Sets, Sequences) + } builder { operator(true) - only(Iterables, Collections, Sets, Sequences) doc { "Returns a list containing all elements of the original collection and then all elements of the given [elements] array." } returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { """ if (this is Collection) return this.plus(elements) @@ -181,46 +194,51 @@ fun generators(): List { return result """ } - doc(Sets) { - """ - Returns a set containing all elements of the original set and the given [elements] array, - which aren't already in this set. + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set and the given [elements] array, + which aren't already in this set. - The returned set preserves the element iteration order of the original set. - """ + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(mapCapacity(this.size + elements.size)) + result.addAll(this) + result.addAll(elements) + return result + """ + } } - body(Sets) { - """ - val result = LinkedHashSet(mapCapacity(this.size + elements.size)) - result.addAll(this) - result.addAll(elements) - return result - """ - } - doc(Sequences) { - """ - Returns a sequence containing all elements of original sequence and then all elements of the given [elements] array. + specialFor(Sequences) { + doc { + """ + Returns a sequence containing all elements of original sequence and then all elements of the given [elements] array. - Note that the source sequence and the array being added are iterated only when an `iterator` is requested from - the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - """ - } - sequenceClassification(intermediate, stateless) - body(Sequences) { - """ - return this.plus(elements.asList()) - """ + Note that the source sequence and the array being added are iterated only when an `iterator` is requested from + the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + """ + } + sequenceClassification(intermediate, stateless) + body { + """ + return this.plus(elements.asList()) + """ + } } } - templates add f("plus(elements: Sequence)") { + val f_plus_sequence = fn("plus(elements: Sequence)") { + include(Iterables, Sets, Sequences, Collections) + } builder { operator(true) - only(Iterables, Sets, Sequences) doc { "Returns a list containing all elements of the original collection and then all elements of the given [elements] sequence." } returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { """ val result = ArrayList() @@ -238,64 +256,74 @@ fun generators(): List { """ } - // TODO: use immutable set builder when available - doc(Sets) { - """ - Returns a set containing all elements of the original set and the given [elements] sequence, - which aren't already in this set. - The returned set preserves the element iteration order of the original set. - """ - } - body(Sets) { - """ - val result = LinkedHashSet(mapCapacity(this.size * 2)) - result.addAll(this) - result.addAll(elements) - return result - """ + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set and the given [elements] sequence, + which aren't already in this set. + + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(mapCapacity(this.size * 2)) + result.addAll(this) + result.addAll(elements) + return result + """ + } } - doc(Sequences) { - """ - Returns a sequence containing all elements of original sequence and then all elements of the given [elements] sequence. + specialFor(Sequences) { + doc { + """ + Returns a sequence containing all elements of original sequence and then all elements of the given [elements] sequence. - Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from - the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - """ - } - sequenceClassification(intermediate, stateless) - body(Sequences) { - """ - return sequenceOf(this, elements).flatten() - """ + Note that the source sequence and the sequence being added are iterated only when an `iterator` is requested from + the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + """ + } + sequenceClassification(intermediate, stateless) + body { + """ + return sequenceOf(this, elements).flatten() + """ + } } } - templates add f("minusElement(element: T)") { + val f_minusElement = fn("minusElement(element: T)") { + include(Iterables, Sets, Sequences) + } builder { inline(Inline.Only) - only(Iterables, Sets, Sequences) doc { "Returns a list containing all elements of the original collection without the first occurrence of the given [element]." } - doc(Sets) { - """ - Returns a set containing all elements of the original set except the given [element]. + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set except the given [element]. - The returned set preserves the element iteration order of the original set. - """ + The returned set preserves the element iteration order of the original set. + """ + } + } + specialFor(Sequences) { + doc { "Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]." } + sequenceClassification(intermediate, stateless) } - doc(Sequences) { "Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]." } - sequenceClassification(intermediate, stateless) returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { "return minus(element)" } } - templates add f("minus(element: T)") { + val f_minus = fn("minus(element: T)") { + include(Iterables, Sets, Sequences) + } builder { operator(true) - only(Iterables, Sets, Sequences) doc { "Returns a list containing all elements of the original collection without the first occurrence of the given [element]." } returns("List") body { @@ -306,45 +334,50 @@ fun generators(): List { """ } - returns("SELF", Sets, Sequences) - doc(Sets) { - """ - Returns a set containing all elements of the original set except the given [element]. + specialFor(Sets, Sequences) { returns("SELF") } + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set except the given [element]. - The returned set preserves the element iteration order of the original set. - """ - } - body(Sets) { - """ - val result = LinkedHashSet(mapCapacity(size)) - var removed = false - return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } - """ - } - - - doc(Sequences) { "Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]." } - sequenceClassification(intermediate, stateless) - body(Sequences) { - """ - return object: Sequence { - override fun iterator(): Iterator { - var removed = false - return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() - } + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(mapCapacity(size)) + var removed = false + return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true } + """ + } + } + + + specialFor(Sequences) { + doc { "Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element]." } + sequenceClassification(intermediate, stateless) + body { + """ + return object: Sequence { + override fun iterator(): Iterator { + var removed = false + return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator() + } + } + """ } - """ } } - templates add f("minus(elements: Iterable)") { + val f_minus_iterable = fn("minus(elements: Iterable)") { + include(Iterables, Sets, Sequences) + } builder { operator(true) - only(Iterables, Sets, Sequences) doc { "Returns a list containing all elements of the original collection except the elements contained in the given [elements] collection." } returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { """ val other = elements.convertToSetForSetOperationWith(this) @@ -355,58 +388,64 @@ fun generators(): List { """ } - doc(Sets) { - """ - Returns a set containing all elements of the original set except the elements contained in the given [elements] collection. + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set except the elements contained in the given [elements] collection. - The returned set preserves the element iteration order of the original set. - """ - } - body(Sets) { - """ - val other = elements.convertToSetForSetOperationWith(this) - if (other.isEmpty()) - return this.toSet() - if (other is Set) - return this.filterNotTo(LinkedHashSet()) { it in other } - - val result = LinkedHashSet(this) - result.removeAll(other) - return result - """ - } - - doc(Sequences) { - """ - Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] collection. - - Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from - the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - """ - } - sequenceClassification(intermediate, stateful) - body(Sequences) { - """ - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.convertToSetForSetOperation() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } + The returned set preserves the element iteration order of the original set. + """ } - """ + body { + """ + val other = elements.convertToSetForSetOperationWith(this) + if (other.isEmpty()) + return this.toSet() + if (other is Set) + return this.filterNotTo(LinkedHashSet()) { it in other } + + val result = LinkedHashSet(this) + result.removeAll(other) + return result + """ + } + } + + specialFor(Sequences) { + doc { + """ + Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] collection. + + Note that the source sequence and the collection being subtracted are iterated only when an `iterator` is requested from + the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + """ + } + sequenceClassification(intermediate, stateful) + body { + """ + return object: Sequence { + override fun iterator(): Iterator { + val other = elements.convertToSetForSetOperation() + if (other.isEmpty()) + return this@minus.iterator() + else + return this@minus.filterNot { it in other }.iterator() + } + } + """ + } + } } - templates add f("minus(elements: Array)") { + val f_minus_array = fn("minus(elements: Array)") { + include(Iterables, Sets, Sequences) + } builder { operator(true) - only(Iterables, Sets, Sequences) doc { "Returns a list containing all elements of the original collection except the elements contained in the given [elements] array." } returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { """ if (elements.isEmpty()) return this.toList() @@ -414,50 +453,55 @@ fun generators(): List { return this.filterNot { it in other } """ } - doc(Sets) { - """ - Returns a set containing all elements of the original set except the elements contained in the given [elements] array. + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set except the elements contained in the given [elements] array. - The returned set preserves the element iteration order of the original set. - """ - } - body(Sets) { - """ - val result = LinkedHashSet(this) - result.removeAll(elements) - return result - """ - } - - doc(Sequences) { - """ - Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] array. - - Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from - the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - """ - } - sequenceClassification(intermediate, stateful) - body(Sequences) { - """ - if (elements.isEmpty()) return this - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.toHashSet() - return this@minus.filterNot { it in other }.iterator() - } + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(this) + result.removeAll(elements) + return result + """ + } + } + + specialFor(Sequences) { + doc { + """ + Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] array. + + Note that the source sequence and the array being subtracted are iterated only when an `iterator` is requested from + the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + """ + } + sequenceClassification(intermediate, stateful) + body { + """ + if (elements.isEmpty()) return this + return object: Sequence { + override fun iterator(): Iterator { + val other = elements.toHashSet() + return this@minus.filterNot { it in other }.iterator() + } + } + """ } - """ } } - templates add f("minus(elements: Sequence)") { + val f_minus_sequence = fn("minus(elements: Sequence)") { + include(Iterables, Sets, Sequences) + } builder { operator(true) - only(Iterables, Sets) doc { "Returns a list containing all elements of the original collection except the elements contained in the given [elements] sequence." } returns("List") - returns("SELF", Sets, Sequences) + specialFor(Sets, Sequences) { returns("SELF") } body { """ val other = elements.toHashSet() @@ -467,50 +511,57 @@ fun generators(): List { return this.filterNot { it in other } """ } - doc(Sets) { - """ - Returns a set containing all elements of the original set except the elements contained in the given [elements] sequence. + specialFor(Sets) { + doc { + """ + Returns a set containing all elements of the original set except the elements contained in the given [elements] sequence. - The returned set preserves the element iteration order of the original set. - """ - } - body(Sets) { - """ - val result = LinkedHashSet(this) - result.removeAll(elements) - return result - """ - } - - doc(Sequences) { - """ - Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] sequence. - - Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from - the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. - - The operation is _intermediate_ for this sequence and _terminal_ and _stateful_ for the [elements] sequence. - """ - } - body(Sequences) { - """ - return object: Sequence { - override fun iterator(): Iterator { - val other = elements.toHashSet() - if (other.isEmpty()) - return this@minus.iterator() - else - return this@minus.filterNot { it in other }.iterator() - } + The returned set preserves the element iteration order of the original set. + """ + } + body { + """ + val result = LinkedHashSet(this) + result.removeAll(elements) + return result + """ + } + } + + specialFor(Sequences) { + doc { + """ + Returns a sequence containing all elements of original sequence except the elements contained in the given [elements] sequence. + + Note that the source sequence and the sequence being subtracted are iterated only when an `iterator` is requested from + the resulting sequence. Changing any of them between successive calls to `iterator` may affect the result. + + The operation is _intermediate_ for this sequence and _terminal_ and _stateful_ for the [elements] sequence. + """ + } + body { + """ + return object: Sequence { + override fun iterator(): Iterator { + val other = elements.toHashSet() + if (other.isEmpty()) + return this@minus.iterator() + else + return this@minus.filterNot { it in other }.iterator() + } + } + """ } - """ } } - templates add f("partition(predicate: (T) -> Boolean)") { - inline(true) + val f_partition = fn("partition(predicate: (T) -> Boolean)") { + includeDefault() + include(CharSequences, Strings) + } builder { + inline() - doc { f -> + doc { """ Splits the original ${f.collection} into pair of lists, where *first* list contains elements for which [predicate] yielded `true`, @@ -534,15 +585,17 @@ fun generators(): List { """ } - doc(CharSequences, Strings) { f -> - """ - Splits the original ${f.collection} into pair of ${f.collection}s, - where *first* ${f.collection} contains characters for which [predicate] yielded `true`, - while *second* ${f.collection} contains characters for which [predicate] yielded `false`. - """ + specialFor(CharSequences, Strings) { + doc { + """ + Splits the original ${f.collection} into pair of ${f.collection}s, + where *first* ${f.collection} contains characters for which [predicate] yielded `true`, + while *second* ${f.collection} contains characters for which [predicate] yielded `false`. + """ + } + returns("Pair") } - returns(CharSequences, Strings) { "Pair" } - body(CharSequences, Strings) { f -> + body(CharSequences, Strings) { val toString = if (f == Strings) ".toString()" else "" """ val first = StringBuilder() @@ -559,10 +612,11 @@ fun generators(): List { } } - templates add f("windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List) -> R)") { + val f_windowed_transform = fn("windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (List) -> R)") { + include(Iterables, Sequences, CharSequences) + } builder { since("1.2") - only(Iterables, Sequences, CharSequences) - doc { f -> + doc { """ Returns a ${f.mapResult} of results of applying the given [transform] function to an each ${f.viewResult} representing a view over the window of the given [size] @@ -609,7 +663,9 @@ fun generators(): List { """ } - customSignature(CharSequences) { "windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R)" } + specialFor(CharSequences) { + signature("windowed(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R)") + } body(CharSequences) { """ checkWindowSizeStep(size, step) @@ -626,7 +682,7 @@ fun generators(): List { """ } - returns(Sequences) { "Sequence" } + specialFor(Sequences) { returns("Sequence") } body(Sequences) { """ return windowedSequence(size, step, partialWindows, reuseBuffer = true).map(transform) @@ -634,14 +690,15 @@ fun generators(): List { } } - templates add f("windowed(size: Int, step: Int = 1, partialWindows: Boolean = false)") { + val f_windowed = fn("windowed(size: Int, step: Int = 1, partialWindows: Boolean = false)") { + include(Iterables, Sequences, CharSequences) + } builder { since("1.2") - only(Iterables, Sequences, CharSequences) - returns(Iterables) { "List>" } - returns(Sequences) { "Sequence>" } - returns(CharSequences) { "List" } + specialFor(Iterables) { returns("List>") } + specialFor(Sequences) { returns("Sequence>") } + specialFor(CharSequences) { returns("List") } - doc { f -> + doc { """ Returns a ${f.mapResult} of snapshots of the window of the given [size] sliding along this ${f.collection} with the given [step], where each @@ -689,10 +746,11 @@ fun generators(): List { } } - templates add f("windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R)") { + val f_windowedSequence_transform = fn("windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false, transform: (CharSequence) -> R)") { + include(CharSequences) + } builder { since("1.2") - only(CharSequences) - doc { f -> + doc { """ Returns a sequence of results of applying the given [transform] function to an each ${f.viewResult} representing a view over the window of the given [size] @@ -712,7 +770,7 @@ fun generators(): List { """ } typeParam("R") - returns { "Sequence" } + returns("Sequence") body { """ @@ -723,10 +781,11 @@ fun generators(): List { } } - templates add f("windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false)") { + val f_windowedSequence = fn("windowedSequence(size: Int, step: Int = 1, partialWindows: Boolean = false)") { + include(CharSequences) + } builder { since("1.2") - only(CharSequences) - doc { f -> + doc { """ Returns a sequence of snapshots of the window of the given [size] sliding along this ${f.collection} with the given [step], where each @@ -743,15 +802,16 @@ fun generators(): List { @sample samples.collections.Sequences.Transformations.takeWindows """ } - returns { "Sequence" } + returns("Sequence") body(CharSequences) { "return windowedSequence(size, step, partialWindows) { it.toString() }" } } - templates add f("chunked(size: Int, transform: (List) -> R)") { + val f_chunked_transform = fn("chunked(size: Int, transform: (List) -> R)") { + include(Iterables, Sequences, CharSequences) + } builder { since("1.2") - only(Iterables, Sequences, CharSequences) - doc { f -> + doc { """ Splits this ${f.collection} into several ${f.viewResult.pluralize()} each not exceeding the given [size] and applies the given [transform] function to an each. @@ -771,17 +831,20 @@ fun generators(): List { typeParam("R") returns("List") - customSignature(CharSequences) { "chunked(size: Int, transform: (CharSequence) -> R)" } + specialFor(CharSequences) { + signature("chunked(size: Int, transform: (CharSequence) -> R)") + } sequenceClassification(intermediate, stateful) - returns(Sequences) { "Sequence" } + specialFor(Sequences) { returns("Sequence") } body { "return windowed(size, size, partialWindows = true, transform = transform)" } } - templates add f("chunked(size: Int)") { + val f_chunked = fn("chunked(size: Int)") { + include(Iterables, Sequences, CharSequences) + } builder { since("1.2") - only(Iterables, Sequences, CharSequences) - doc { f -> + doc { """ Splits this ${f.collection} into a ${f.mapResult} of ${f.snapshotResult.pluralize()} each not exceeding the given [size]. @@ -792,19 +855,20 @@ fun generators(): List { @sample samples.collections.Collections.Transformations.chunked """ } - returns(Iterables) { "List>" } - returns(Sequences) { "Sequence>" } - returns(CharSequences) { "List" } + specialFor(Iterables) { returns("List>") } + specialFor(Sequences) { returns("Sequence>") } + specialFor(CharSequences) { returns("List") } sequenceClassification(intermediate, stateful) body { "return windowed(size, size, partialWindows = true)" } } - templates add f("chunkedSequence(size: Int, transform: (CharSequence) -> R)") { + val f_chunkedSequence_transform = fn("chunkedSequence(size: Int, transform: (CharSequence) -> R)") { + include(CharSequences) + } builder { since("1.2") - only(CharSequences) - doc { f -> + doc { """ Splits this ${f.collection} into several ${f.viewResult.pluralize()} each not exceeding the given [size] and applies the given [transform] function to an each. @@ -822,7 +886,7 @@ fun generators(): List { } typeParam("R") - returns { "Sequence" } + returns("Sequence") body { """ @@ -831,10 +895,11 @@ fun generators(): List { } } - templates add f("chunkedSequence(size: Int)") { + val f_chunkedSequence = fn("chunkedSequence(size: Int)") { + include(CharSequences) + } builder { since("1.2") - only(CharSequences) - doc { f -> + doc { """ Splits this ${f.collection} into a sequence of ${f.snapshotResult.pluralize()} each not exceeding the given [size]. @@ -845,16 +910,17 @@ fun generators(): List { @sample samples.collections.Collections.Transformations.chunked """ } - returns { "Sequence" } + returns("Sequence") body(CharSequences) { "return chunkedSequence(size) { it.toString() }" } } - templates add f("zipWithNext(transform: (a: T, b: T) -> R)") { + val f_zipWithNext_transform = fn("zipWithNext(transform: (a: T, b: T) -> R)") { + include(Iterables, Sequences, CharSequences) + } builder { since("1.2") - only(Iterables, Sequences, CharSequences) typeParam("R") - doc { f -> + doc { """ Returns a ${f.mapResult} containing the results of applying the given [transform] function to an each pair of two adjacent ${f.element.pluralize()} in this ${f.collection}. @@ -865,7 +931,7 @@ fun generators(): List { """ } returns("List") - inline(true) + inline() body { """ val iterator = iterator() @@ -880,7 +946,7 @@ fun generators(): List { return result """ } - body(CharSequences) { f -> + body(CharSequences) { """ val size = ${if (f == CharSequences) "length" else "size" } - 1 if (size < 1) return emptyList() @@ -890,11 +956,12 @@ fun generators(): List { } return result """ - } - inline(false, Sequences) sequenceClassification(intermediate, stateless) - returns(Sequences) { "Sequence" } + specialFor(Sequences) { + inline(Inline.No) + returns("Sequence") + } body(Sequences) { """ return buildSequence result@ { @@ -911,11 +978,12 @@ fun generators(): List { } } - templates add f("zipWithNext()") { + val f_zipWithNext = fn("zipWithNext()") { + include(Iterables, Sequences, CharSequences) + } builder { since("1.2") - only(Iterables, Sequences, CharSequences) returns("List>") - doc { f -> + doc { """ Returns a ${f.mapResult} of pairs of each two adjacent ${f.element.pluralize()} in this ${f.collection}. @@ -925,14 +993,15 @@ fun generators(): List { """ } sequenceClassification(intermediate, stateless) - returns(Sequences) { "Sequence>" } + specialFor(Sequences) { returns("Sequence>") } body { "return zipWithNext { a, b -> a to b }" } } - templates add f("zip(other: Iterable, transform: (a: T, b: R) -> V)") { - exclude(Sequences) + val f_zip_transform = fn("zip(other: Iterable, transform: (a: T, b: R) -> V)") { + include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + } builder { doc { """ Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. @@ -941,7 +1010,7 @@ fun generators(): List { typeParam("R") typeParam("V") returns("List") - inline(true) + inline() body { """ val first = iterator() @@ -967,8 +1036,9 @@ fun generators(): List { } } - templates add f("zip(other: Array, transform: (a: T, b: R) -> V)") { - exclude(Sequences) + val f_zip_array_transform = fn("zip(other: Array, transform: (a: T, b: R) -> V)") { + include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + } builder { doc { """ Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. @@ -977,7 +1047,7 @@ fun generators(): List { typeParam("R") typeParam("V") returns("List") - inline(true) + inline() body { """ val arraySize = other.size @@ -1003,8 +1073,9 @@ fun generators(): List { } - templates add f("zip(other: SELF, transform: (a: T, b: T) -> V)") { - only(ArraysOfPrimitives) + val f_zip_sameArray_transform = fn("zip(other: SELF, transform: (a: T, b: T) -> V)") { + include(ArraysOfPrimitives) + } builder { doc { """ Returns a list of values built from elements of both collections with same indexes using provided [transform]. List has length of shortest collection. @@ -1012,8 +1083,8 @@ fun generators(): List { } typeParam("V") returns("List") - inline(true) - body() { + inline() + body { """ val size = minOf(size, other.size) val list = ArrayList(size) @@ -1025,8 +1096,9 @@ fun generators(): List { } } - templates add f("zip(other: Sequence, transform: (a: T, b: R) -> V)") { - only(Sequences) + val f_zip_sequence_transform = fn("zip(other: Sequence, transform: (a: T, b: R) -> V)") { + include(Sequences) + } builder { doc { """ Returns a sequence of values built from elements of both collections with same indexes using provided [transform]. Resulting sequence has length of shortest input sequences. @@ -1043,8 +1115,9 @@ fun generators(): List { } } - templates add f("zip(other: CharSequence, transform: (a: Char, b: Char) -> V)") { - only(CharSequences) + val f_zip_charSequence_transform = fn("zip(other: CharSequence, transform: (a: Char, b: Char) -> V)") { + include(CharSequences) + } builder { doc { """ Returns a list of values built from characters of both char sequences with same indexes using provided [transform]. List has length of shortest char sequence. @@ -1052,7 +1125,7 @@ fun generators(): List { } typeParam("V") returns("List") - inline(true) + inline() body { """ val length = minOf(this.length, other.length) @@ -1067,9 +1140,10 @@ fun generators(): List { } - templates add f("zip(other: Iterable)") { + val f_zip = fn("zip(other: Iterable)") { + include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + } builder { infix(true) - exclude(Sequences) doc { """ Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. @@ -1084,9 +1158,10 @@ fun generators(): List { } } - templates add f("zip(other: CharSequence)") { + val f_zip_charSequence = fn("zip(other: CharSequence)") { + include(CharSequences) + } builder { infix(true) - only(CharSequences) doc { """ Returns a list of pairs built from characters of both char sequences with same indexes. List has length of shortest char sequence. @@ -1100,9 +1175,10 @@ fun generators(): List { } } - templates add f("zip(other: Array)") { + val f_zip_array = fn("zip(other: Array)") { + include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + } builder { infix(true) - exclude(Sequences) doc { """ Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. @@ -1117,9 +1193,10 @@ fun generators(): List { } } - templates add f("zip(other: SELF)") { + val f_zip_sameArray = fn("zip(other: SELF)") { + include(ArraysOfPrimitives) + } builder { infix(true) - only(ArraysOfPrimitives) doc { """ Returns a list of pairs built from elements of both collections with same indexes. List has length of shortest collection. @@ -1133,9 +1210,10 @@ fun generators(): List { } } - templates add f("zip(other: Sequence)") { + val f_zip_sequence = fn("zip(other: Sequence)") { + include(Sequences) + } builder { infix(true) - only(Sequences) doc { """ Returns a sequence of pairs built from elements of both sequences with same indexes. @@ -1152,19 +1230,17 @@ fun generators(): List { } } - return templates + // documentation helpers + + private val Family.snapshotResult: String + get() = when (this) { + CharSequences, Strings -> "string" + else -> "list" + } + + private val Family.viewResult: String + get() = when (this) { + CharSequences, Strings -> "char sequence" + else -> "list" + } } - -// documentation helpers - -private val Family.snapshotResult: String - get() = when (this) { - CharSequences, Strings -> "string" - else -> "list" - } - -private val Family.viewResult: String - get() = when (this) { - CharSequences, Strings -> "char sequence" - else -> "list" - }