diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt index 821c64d78c9..617da5cc4bf 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -17,7 +17,7 @@ fun main(args: Array) { Ordering, // ArrayOps, Snapshots, -// Mapping, + Mapping, SetOps, Aggregates, Guards, diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index e88e5d10fe8..85063c2b44a 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -1,14 +1,45 @@ +/* + * 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 import templates.Family.* import templates.SequenceClass.* -fun mapping(): List { - val templates = arrayListOf() +object Mapping : TemplateGroupBase() { - templates add f("withIndex()") { + init { + val terminalOperationPattern = Regex("^\\w+To") + defaultBuilder { + if (sequenceClassification.isEmpty()) { + if (terminalOperationPattern in signature) + sequenceClassification(terminal) + else + sequenceClassification(intermediate, stateless) + } + } + } + + val f_withIndex = fn("withIndex()") { + includeDefault() include(CharSequences) - doc { f -> "Returns a ${if (f == Sequences) f.mapResult else "lazy [Iterable]"} of [IndexedValue] for each ${f.element} of the original ${f.collection}." } + } builder { + doc { + "Returns a ${if (f == Sequences) f.mapResult else "lazy [Iterable]"} of [IndexedValue] for each ${f.element} of the original ${f.collection}." + } returns("Iterable>") body { """ @@ -16,18 +47,17 @@ fun mapping(): List { """ } - returns(Sequences) { "Sequence>" } - body(Sequences) { - """ - return IndexingSequence(this) - """ - } + specialFor(Sequences) { returns("Sequence>") } + body(Sequences) { """return IndexingSequence(this)""" } } - templates add f("mapIndexed(transform: (index: Int, T) -> R)") { - inline(true) + val f_mapIndexed = fn("mapIndexed(transform: (index: Int, T) -> R)") { + includeDefault() + include(CharSequences) + } builder { + inline() - doc { f -> + doc { """ Returns a ${f.mapResult} containing the results of applying the given [transform] function to each ${f.element} and its index in the original ${f.collection}. @@ -46,17 +76,22 @@ fun mapping(): List { body(CharSequences) { "return mapIndexedTo(ArrayList(length), transform)" } - inline(false, Sequences) - returns(Sequences) { "Sequence" } + specialFor(Sequences) { + inline(Inline.No) + returns("Sequence") + } body(Sequences) { "return TransformingIndexedSequence(this, transform)" } } - templates add f("map(transform: (T) -> R)") { - inline(true) + val f_map = fn("map(transform: (T) -> R)") { + includeDefault() + include(Maps, CharSequences) + } builder { + inline() - doc { f -> + doc { """ Returns a ${f.mapResult} containing the results of applying the given [transform] function to each ${f.element} in the original ${f.collection}. @@ -74,21 +109,22 @@ fun mapping(): List { "return mapTo(ArrayList(length), transform)" } - inline(false, Sequences) - returns(Sequences) { "Sequence" } + specialFor(Sequences) { + inline(Inline.No) + returns("Sequence") + } body(Sequences) { "return TransformingSequence(this, transform)" } - include(Maps) } - templates add f("mapNotNull(transform: (T) -> R?)") { - inline(true) - include(Maps, CharSequences) - exclude(ArraysOfPrimitives) + val f_mapNotNull = fn("mapNotNull(transform: (T) -> R?)") { + include(Iterables, ArraysOfObjects, Sequences, Maps, CharSequences) + } builder { + inline() typeParam("R : Any") returns("List") - doc { f -> + doc { """ Returns a ${f.mapResult} containing only the non-null results of applying the given [transform] function to each ${f.element} in the original ${f.collection}. @@ -98,21 +134,23 @@ fun mapping(): List { "return mapNotNullTo(ArrayList(), transform)" } - inline(false, Sequences) - returns(Sequences) { "Sequence" } + specialFor(Sequences) { + inline(Inline.No) + returns("Sequence") + } body(Sequences) { "return TransformingSequence(this, transform).filterNotNull()" } } - templates add f("mapIndexedNotNull(transform: (index: Int, T) -> R?)") { - inline(true) - include(CharSequences) - exclude(ArraysOfPrimitives) + val f_mapIndexedNotNull = fn("mapIndexedNotNull(transform: (index: Int, T) -> R?)") { + include(Iterables, ArraysOfObjects, Sequences, CharSequences) + } builder { + inline() typeParam("R : Any") returns("List") - doc { f -> + doc { """ Returns a ${f.mapResult} containing only the non-null results of applying the given [transform] function to each ${f.element} and its index in the original ${f.collection}. @@ -124,17 +162,22 @@ fun mapping(): List { "return mapIndexedNotNullTo(ArrayList(), transform)" } - inline(false, Sequences) - returns(Sequences) { "Sequence" } + specialFor(Sequences) { + inline(Inline.No) + returns("Sequence") + } body(Sequences) { "return TransformingIndexedSequence(this, transform).filterNotNull()" } } - templates add f("mapTo(destination: C, transform: (T) -> R)") { - inline(true) + val f_mapTo = fn("mapTo(destination: C, transform: (T) -> R)") { + includeDefault() + include(Maps, CharSequences) + } builder { + inline() - doc { f -> + doc { """ Applies the given [transform] function to each ${f.element} of the original ${f.collection} and appends the results to the given [destination]. @@ -151,13 +194,15 @@ fun mapping(): List { return destination """ } - include(Maps, CharSequences) } - templates add f("mapIndexedTo(destination: C, transform: (index: Int, T) -> R)") { - inline(true) + val f_mapIndexedTo = fn("mapIndexedTo(destination: C, transform: (index: Int, T) -> R)") { + includeDefault() + include(CharSequences) + } builder { + inline() - doc { f -> + doc { """ Applies the given [transform] function to each ${f.element} and its index in the original ${f.collection} and appends the results to the given [destination]. @@ -177,17 +222,16 @@ fun mapping(): List { return destination """ } - include(CharSequences) } - templates add f("mapNotNullTo(destination: C, transform: (T) -> R?)") { - inline(true) - include(Maps, CharSequences) - exclude(ArraysOfPrimitives) + val f_mapNotNullTo = fn("mapNotNullTo(destination: C, transform: (T) -> R?)") { + include(Iterables, ArraysOfObjects, Sequences, Maps, CharSequences) + } builder { + inline() typeParam("R : Any") typeParam("C : MutableCollection") returns("C") - doc { f -> + doc { """ Applies the given [transform] function to each ${f.element} in the original ${f.collection} and appends only the non-null results to the given [destination]. @@ -201,14 +245,14 @@ fun mapping(): List { } } - templates add f("mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?)") { - inline(true) - include(CharSequences) - exclude(ArraysOfPrimitives) + val f_mapIndexedNotNullTo = fn("mapIndexedNotNullTo(destination: C, transform: (index: Int, T) -> R?)") { + include(Iterables, ArraysOfObjects, Sequences, CharSequences) + } builder { + inline() typeParam("R : Any") typeParam("C : MutableCollection") returns("C") - doc { f -> + doc { """ Applies the given [transform] function to each ${f.element} and its index in the original ${f.collection} and appends only the non-null results to the given [destination]. @@ -224,53 +268,38 @@ fun mapping(): List { } } - templates add f("flatMap(transform: (T) -> Iterable)") { - inline(true) + val f_flatMap = fn("flatMap(transform: (T) -> Iterable)") { + includeDefault() + include(Maps, CharSequences) + } builder { + inline() - exclude(Sequences) - doc { f -> "Returns a single list of all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}." } + doc { "Returns a single list of all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}." } typeParam("R") returns("List") body { "return flatMapTo(ArrayList(), transform)" } - include(Maps, CharSequences) - } - - templates add f("flatMap(transform: (T) -> Sequence)") { - only(Sequences) - doc { "Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence." } - typeParam("R") - returns("Sequence") - body { - "return FlatteningSequence(this, transform, { it.iterator() })" + specialFor(Sequences) { + inline(Inline.No) + signature("flatMap(transform: (T) -> Sequence)") + doc { "Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence." } + returns("Sequence") + body { + "return FlatteningSequence(this, transform, { it.iterator() })" + } } } - templates add f("flatMapTo(destination: C, transform: (T) -> Iterable)") { - inline(true) - exclude(Sequences) - doc { f -> "Appends all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}, to the given [destination]." } - typeParam("R") - typeParam("C : MutableCollection") - returns("C") - body { - """ - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination - """ - } + val f_flatMapTo = fn("flatMapTo(destination: C, transform: (T) -> Iterable)") { + includeDefault() include(Maps, CharSequences) - } - - templates add f("flatMapTo(destination: C, transform: (T) -> Sequence)") { - inline(true) - - only(Sequences) - doc { "Appends all elements yielded from results of [transform] function being invoked on each element of original sequence, to the given [destination]." } + } builder { + inline() + doc { "Appends all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}, to the given [destination]." } + specialFor(Sequences) { + signature("flatMapTo(destination: C, transform: (T) -> Sequence)") + } typeParam("R") typeParam("C : MutableCollection") returns("C") @@ -285,11 +314,13 @@ fun mapping(): List { } } - templates add f("groupBy(keySelector: (T) -> K)") { - inline(true) - + val f_groupBy_key = fn("groupBy(keySelector: (T) -> K)") { + includeDefault() include(CharSequences) - doc { f -> + } builder { + inline() + + doc { """ Groups ${f.element.pluralize()} of the original ${f.collection} by the key returned by the given [keySelector] function applied to each ${f.element} and returns a map where each group key is associated with a list of corresponding ${f.element.pluralize()}. @@ -305,13 +336,15 @@ fun mapping(): List { body { "return groupByTo(LinkedHashMap>(), keySelector)" } } - templates add f("groupByTo(destination: M, keySelector: (T) -> K)") { - inline(true) - + val f_groupByTo_key = fn("groupByTo(destination: M, keySelector: (T) -> K)") { + includeDefault() include(CharSequences) + } builder { + inline() + typeParam("K") typeParam("M : MutableMap>") - doc { f -> + doc { """ Groups ${f.element.pluralize()} of the original ${f.collection} by the key returned by the given [keySelector] function applied to each ${f.element} and puts to the [destination] map each group key associated with a list of corresponding ${f.element.pluralize()}. @@ -335,11 +368,12 @@ fun mapping(): List { } } - templates add f("groupBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { - inline(true) - + val f_groupBy_key_value = fn("groupBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { + includeDefault() include(CharSequences) - doc { f -> + } builder { + inline() + doc { """ Groups values returned by the [valueTransform] function applied to each ${f.element} of the original ${f.collection} by the key returned by the given [keySelector] function applied to the ${f.element} @@ -358,15 +392,16 @@ fun mapping(): List { } - templates add f("groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { - inline(true) - + val f_groupByTo_key_value = fn("groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { + includeDefault() include(CharSequences) + } builder { + inline() typeParam("K") typeParam("V") typeParam("M : MutableMap>") - doc { f -> + doc { """ Groups values returned by the [valueTransform] function applied to each ${f.element} of the original ${f.collection} by the key returned by the given [keySelector] function applied to the ${f.element} @@ -391,17 +426,18 @@ fun mapping(): List { } } - templates add f("groupingBy(crossinline keySelector: (T) -> K)") { + val f_groupingBy = fn("groupingBy(crossinline keySelector: (T) -> K)") { + include(Iterables, Sequences, ArraysOfObjects, CharSequences) + } builder { since("1.1") - inline(true) - only(Iterables, Sequences, ArraysOfObjects, CharSequences) + inline() typeParam("T") typeParam("K") returns("Grouping") - doc { f -> + doc { """ Creates a [Grouping] source from ${f.collection.prefixWithArticle()} to be used later with one of group-and-fold operations using the specified [keySelector] function to extract a key from each ${f.element}. @@ -420,14 +456,4 @@ fun mapping(): List { } } - val terminalOperationPattern = Regex("^\\w+To") - templates.forEach { with (it) { - if (sequenceClassification.isEmpty()) { - if (terminalOperationPattern in signature) - sequenceClassification(terminal) - else - sequenceClassification(intermediate, stateless) - } - } } - return templates -} \ No newline at end of file +}