Rewrite Elements.kt in new DSL
This commit is contained in:
@@ -12,7 +12,7 @@ import templates.*
|
|||||||
*/
|
*/
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val templateGroups = sequenceOf<TemplateGroup>(
|
val templateGroups = sequenceOf<TemplateGroup>(
|
||||||
// Elements,
|
Elements,
|
||||||
// Filtering,
|
// Filtering,
|
||||||
// Ordering,
|
// Ordering,
|
||||||
// ArrayOps,
|
// ArrayOps,
|
||||||
|
|||||||
@@ -1,20 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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 elements(): List<GenericFunction> {
|
object Elements : TemplateGroupBase() {
|
||||||
val templates = arrayListOf<GenericFunction>()
|
|
||||||
|
|
||||||
|
init {
|
||||||
|
defaultBuilder {
|
||||||
|
sequenceClassification(terminal)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
templates add f("contains(element: T)") {
|
val f_contains = fn("contains(element: T)") {
|
||||||
|
include(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
operator(true)
|
operator(true)
|
||||||
|
|
||||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
|
doc { "Returns `true` if [element] is found in the ${f.collection}." }
|
||||||
doc { f -> "Returns `true` if [element] is found in the ${f.collection}." }
|
|
||||||
typeParam("@kotlin.internal.OnlyInputTypes T")
|
typeParam("@kotlin.internal.OnlyInputTypes T")
|
||||||
returns("Boolean")
|
returns("Boolean")
|
||||||
body(Iterables) { f ->
|
body(Iterables) {
|
||||||
"""
|
"""
|
||||||
if (this is Collection)
|
if (this is Collection)
|
||||||
return contains(element)
|
return contains(element)
|
||||||
@@ -28,14 +49,16 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val f_indexOf = fn("indexOf(element: T)") {
|
||||||
templates add f("indexOf(element: T)") {
|
include(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists)
|
||||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists)
|
} builder {
|
||||||
doc { f -> "Returns first index of [element], or -1 if the ${f.collection} does not contain element." }
|
doc { "Returns first index of [element], or -1 if the ${f.collection} does not contain element." }
|
||||||
typeParam("@kotlin.internal.OnlyInputTypes T")
|
typeParam("@kotlin.internal.OnlyInputTypes T")
|
||||||
annotations(Lists) { """@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases""" }
|
specialFor(Lists) {
|
||||||
|
annotation("""@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases""")
|
||||||
|
}
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body { f ->
|
body {
|
||||||
"""
|
"""
|
||||||
${if (f == Iterables) "if (this is List) return this.indexOf(element)" else ""}
|
${if (f == Iterables) "if (this is List) return this.indexOf(element)" else ""}
|
||||||
var index = 0
|
var index = 0
|
||||||
@@ -79,13 +102,16 @@ fun elements(): List<GenericFunction> {
|
|||||||
body(Lists) { "return indexOf(element)" }
|
body(Lists) { "return indexOf(element)" }
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("lastIndexOf(element: T)") {
|
val f_lastIndexOf = fn("lastIndexOf(element: T)") {
|
||||||
only(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists)
|
include(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives, Lists)
|
||||||
doc { f -> "Returns last index of [element], or -1 if the ${f.collection} does not contain element." }
|
} builder {
|
||||||
|
doc { "Returns last index of [element], or -1 if the ${f.collection} does not contain element." }
|
||||||
typeParam("@kotlin.internal.OnlyInputTypes T")
|
typeParam("@kotlin.internal.OnlyInputTypes T")
|
||||||
annotations(Lists) { """@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases""" }
|
specialFor(Lists) {
|
||||||
|
annotation("""@Suppress("EXTENSION_SHADOWED_BY_MEMBER") // false warning, extension takes precedence in some cases""")
|
||||||
|
}
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body { f ->
|
body {
|
||||||
"""
|
"""
|
||||||
${if (f == Iterables) "if (this is List) return this.lastIndexOf(element)" else ""}
|
${if (f == Iterables) "if (this is List) return this.lastIndexOf(element)" else ""}
|
||||||
var lastIndex = -1
|
var lastIndex = -1
|
||||||
@@ -130,11 +156,13 @@ fun elements(): List<GenericFunction> {
|
|||||||
body(Lists) { "return lastIndexOf(element)" }
|
body(Lists) { "return lastIndexOf(element)" }
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("indexOfFirst(predicate: (T) -> Boolean)") {
|
val f_indexOfFirst = fn("indexOfFirst(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
inline()
|
||||||
|
|
||||||
include(Lists)
|
doc { "Returns index of the first ${f.element} matching the given [predicate], or -1 if the ${f.collection} does not contain such ${f.element}." }
|
||||||
doc { f -> "Returns index of the first ${f.element} matching the given [predicate], or -1 if the ${f.collection} does not contain such ${f.element}." }
|
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -160,10 +188,13 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("indexOfLast(predicate: (T) -> Boolean)") {
|
val f_indexOfLast = fn("indexOfLast(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
inline()
|
||||||
|
|
||||||
doc { f -> "Returns index of the last ${f.element} matching the given [predicate], or -1 if the ${f.collection} does not contain such ${f.element}." }
|
doc { "Returns index of the last ${f.element} matching the given [predicate], or -1 if the ${f.collection} does not contain such ${f.element}." }
|
||||||
returns("Int")
|
returns("Int")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -201,9 +232,12 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("elementAt(index: Int)") {
|
val f_elementAt = fn("elementAt(index: Int)") {
|
||||||
|
includeDefault()
|
||||||
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
val index = '$' + "index"
|
val index = '$' + "index"
|
||||||
doc { f -> "Returns ${f.element.prefixWithArticle()} at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this ${f.collection}." }
|
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this ${f.collection}." }
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -218,16 +252,18 @@ fun elements(): List<GenericFunction> {
|
|||||||
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") }
|
return elementAtOrElse(index) { throw IndexOutOfBoundsException("Sequence doesn't contain element at index $index.") }
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
inline(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
|
|
||||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
inlineOnly()
|
||||||
return get(index)
|
body { "return get(index)" }
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("elementAtOrElse(index: Int, defaultValue: (Int) -> T)") {
|
val f_elementAtOrElse = fn("elementAtOrElse(index: Int, defaultValue: (Int) -> T)") {
|
||||||
doc { f -> "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." }
|
includeDefault()
|
||||||
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." }
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -259,19 +295,22 @@ fun elements(): List<GenericFunction> {
|
|||||||
return defaultValue(index)
|
return defaultValue(index)
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
inline(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
|
specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
inlineOnly()
|
||||||
"""
|
body {
|
||||||
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
|
"""
|
||||||
"""
|
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("getOrElse(index: Int, defaultValue: (Int) -> T)") {
|
val f_getOrElse = fn("getOrElse(index: Int, defaultValue: (Int) -> T)") {
|
||||||
doc { f -> "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." }
|
include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or the result of calling the [defaultValue] function if the [index] is out of bounds of this ${f.collection}." }
|
||||||
returns("T")
|
returns("T")
|
||||||
inline(Inline.Only)
|
inlineOnly()
|
||||||
only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
|
return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
|
||||||
@@ -279,9 +318,11 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val f_elementAtOrNull = fn("elementAtOrNull(index: Int)") {
|
||||||
templates add f("elementAtOrNull(index: Int)") {
|
includeDefault()
|
||||||
doc { f -> "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." }
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -313,18 +354,17 @@ fun elements(): List<GenericFunction> {
|
|||||||
return null
|
return null
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
inline(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { Inline.Only }
|
specialFor(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
inlineOnly()
|
||||||
"""
|
body { "return this.getOrNull(index)" }
|
||||||
return this.getOrNull(index)
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("getOrNull(index: Int)") {
|
val f_getOrNull = fn("getOrNull(index: Int)") {
|
||||||
doc { f -> "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." }
|
include(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns ${f.element.prefixWithArticle()} at the given [index] or `null` if the [index] is out of bounds of this ${f.collection}." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
only(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
return if (index >= 0 && index <= lastIndex) get(index) else null
|
return if (index >= 0 && index <= lastIndex) get(index) else null
|
||||||
@@ -332,9 +372,11 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val f_first = fn("first()") {
|
||||||
templates add f("first()") {
|
includeDefault()
|
||||||
doc { f -> """Returns first ${f.element}.
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
doc { """Returns first ${f.element}.
|
||||||
@throws [NoSuchElementException] if the ${f.collection} is empty.
|
@throws [NoSuchElementException] if the ${f.collection} is empty.
|
||||||
""" }
|
""" }
|
||||||
returns("T")
|
returns("T")
|
||||||
@@ -351,7 +393,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
"""
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
||||||
@@ -367,8 +409,12 @@ fun elements(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
templates add f("firstOrNull()") {
|
|
||||||
doc { f -> "Returns the first ${f.element}, or `null` if the ${f.collection} is empty." }
|
val f_firstOrNull = fn("firstOrNull()") {
|
||||||
|
includeDefault()
|
||||||
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns the first ${f.element}, or `null` if the ${f.collection} is empty." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -403,14 +449,15 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("first(predicate: (T) -> Boolean)") {
|
val f_first_predicate = fn("first(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
|
|
||||||
include(CharSequences)
|
include(CharSequences)
|
||||||
doc { f -> """Returns the first ${f.element} matching the given [predicate].
|
} builder {
|
||||||
|
inline()
|
||||||
|
doc { """Returns the first ${f.element} matching the given [predicate].
|
||||||
@throws [NoSuchElementException] if no such ${f.element} is found.""" }
|
@throws [NoSuchElementException] if no such ${f.element} is found.""" }
|
||||||
returns("T")
|
returns("T")
|
||||||
body { f ->
|
body {
|
||||||
"""
|
"""
|
||||||
for (element in this) if (predicate(element)) return element
|
for (element in this) if (predicate(element)) return element
|
||||||
throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
||||||
@@ -418,11 +465,13 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("firstOrNull(predicate: (T) -> Boolean)") {
|
val f_firstOrNull_predicate = fn("firstOrNull(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
|
|
||||||
include(CharSequences)
|
include(CharSequences)
|
||||||
doc { f -> "Returns the first ${f.element} matching the given [predicate], or `null` if ${f.element} was not found." }
|
} builder {
|
||||||
|
inline()
|
||||||
|
|
||||||
|
doc { "Returns the first ${f.element} matching the given [predicate], or `null` if ${f.element} was not found." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -432,16 +481,22 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("find(predicate: (T) -> Boolean)") {
|
val f_find = fn("find(predicate: (T) -> Boolean)") {
|
||||||
inline(Inline.Only)
|
includeDefault()
|
||||||
include(CharSequences)
|
include(CharSequences)
|
||||||
doc { f -> "Returns the first ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
|
} builder {
|
||||||
|
inline(Inline.Only)
|
||||||
|
doc { "Returns the first ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body { "return firstOrNull(predicate)"}
|
body { "return firstOrNull(predicate)"}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("last()") {
|
|
||||||
doc { f -> """Returns the last ${f.element}.
|
val f_last = fn("last()") {
|
||||||
|
includeDefault()
|
||||||
|
include(CharSequences, Lists)
|
||||||
|
} builder {
|
||||||
|
doc { """Returns the last ${f.element}.
|
||||||
@throws [NoSuchElementException] if the ${f.collection} is empty.""" }
|
@throws [NoSuchElementException] if the ${f.collection} is empty.""" }
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
@@ -471,7 +526,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
return last
|
return last
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
"""
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
||||||
@@ -480,8 +535,11 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("lastOrNull()") {
|
val f_lastOrNull = fn("lastOrNull()") {
|
||||||
doc { f -> "Returns the last ${f.element}, or `null` if the ${f.collection} is empty." }
|
includeDefault()
|
||||||
|
include(Lists, CharSequences)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns the last ${f.element}, or `null` if the ${f.collection} is empty." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -522,14 +580,16 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("last(predicate: (T) -> Boolean)") {
|
val f_last_predicate = fn("last(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
|
include(Lists, CharSequences)
|
||||||
|
} builder {
|
||||||
|
inline()
|
||||||
|
|
||||||
include(CharSequences)
|
doc { """Returns the last ${f.element} matching the given [predicate].
|
||||||
doc { f -> """Returns the last ${f.element} matching the given [predicate].
|
|
||||||
@throws [NoSuchElementException] if no such ${f.element} is found.""" }
|
@throws [NoSuchElementException] if no such ${f.element} is found.""" }
|
||||||
returns("T")
|
returns("T")
|
||||||
body { f ->
|
body {
|
||||||
"""
|
"""
|
||||||
var last: T? = null
|
var last: T? = null
|
||||||
var found = false
|
var found = false
|
||||||
@@ -545,7 +605,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) { f ->
|
body(CharSequences, ArraysOfPrimitives, ArraysOfObjects) {
|
||||||
"""
|
"""
|
||||||
for (index in this.indices.reversed()) {
|
for (index in this.indices.reversed()) {
|
||||||
val element = this[index]
|
val element = this[index]
|
||||||
@@ -554,7 +614,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
throw NoSuchElementException("${f.doc.collection.capitalize()} contains no ${f.doc.element} matching the predicate.")
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(Lists) { f ->
|
body(Lists) {
|
||||||
"""
|
"""
|
||||||
val iterator = this.listIterator(size)
|
val iterator = this.listIterator(size)
|
||||||
while (iterator.hasPrevious()) {
|
while (iterator.hasPrevious()) {
|
||||||
@@ -566,12 +626,14 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("lastOrNull(predicate: (T) -> Boolean)") {
|
val f_lastOrNull_predicate = fn("lastOrNull(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
include(CharSequences)
|
include(Lists, CharSequences)
|
||||||
doc { f -> "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
|
} builder {
|
||||||
|
inline()
|
||||||
|
doc { "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body { f ->
|
body {
|
||||||
"""
|
"""
|
||||||
var last: T? = null
|
var last: T? = null
|
||||||
for (element in this) {
|
for (element in this) {
|
||||||
@@ -605,16 +667,21 @@ fun elements(): List<GenericFunction> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("findLast(predicate: (T) -> Boolean)") {
|
val f_findLast = fn("findLast(predicate: (T) -> Boolean)") {
|
||||||
inline(Inline.Only)
|
includeDefault()
|
||||||
include(Lists, CharSequences)
|
include(Lists, CharSequences)
|
||||||
doc { f -> "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
|
} builder {
|
||||||
|
inline(Inline.Only)
|
||||||
|
doc { "Returns the last ${f.element} matching the given [predicate], or `null` if no such ${f.element} was found." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body { "return lastOrNull(predicate)"}
|
body { "return lastOrNull(predicate)"}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("single()") {
|
val f_single = fn("single()") {
|
||||||
doc { f -> "Returns the single ${f.element}, or throws an exception if the ${f.collection} is empty or has more than one ${f.element}." }
|
includeDefault()
|
||||||
|
include(Lists, CharSequences)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns the single ${f.element}, or throws an exception if the ${f.collection} is empty or has more than one ${f.element}." }
|
||||||
returns("T")
|
returns("T")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -643,7 +710,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
return single
|
return single
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) { f ->
|
body(CharSequences, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
"""
|
"""
|
||||||
return when (${f.code.size}) {
|
return when (${f.code.size}) {
|
||||||
0 -> throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
0 -> throw NoSuchElementException("${f.doc.collection.capitalize()} is empty.")
|
||||||
@@ -654,8 +721,11 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("singleOrNull()") {
|
val f_singleOrNull = fn("singleOrNull()") {
|
||||||
doc { f -> "Returns single ${f.element}, or `null` if the ${f.collection} is empty or has more than one ${f.element}." }
|
includeDefault()
|
||||||
|
include(Lists, CharSequences)
|
||||||
|
} builder {
|
||||||
|
doc { "Returns single ${f.element}, or `null` if the ${f.collection} is empty or has more than one ${f.element}." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -696,12 +766,14 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("single(predicate: (T) -> Boolean)") {
|
val f_single_predicate = fn("single(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
include(CharSequences)
|
include(CharSequences)
|
||||||
doc { f -> "Returns the single ${f.element} matching the given [predicate], or throws exception if there is no or more than one matching ${f.element}." }
|
} builder {
|
||||||
|
inline()
|
||||||
|
doc { "Returns the single ${f.element} matching the given [predicate], or throws exception if there is no or more than one matching ${f.element}." }
|
||||||
returns("T")
|
returns("T")
|
||||||
body { f ->
|
body {
|
||||||
"""
|
"""
|
||||||
var single: T? = null
|
var single: T? = null
|
||||||
var found = false
|
var found = false
|
||||||
@@ -719,10 +791,12 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates add f("singleOrNull(predicate: (T) -> Boolean)") {
|
val f_singleOrNull_predicate = fn("singleOrNull(predicate: (T) -> Boolean)") {
|
||||||
inline(true)
|
includeDefault()
|
||||||
include(CharSequences)
|
include(CharSequences)
|
||||||
doc { f -> "Returns the single ${f.element} matching the given [predicate], or `null` if ${f.element} was not found or more than one ${f.element} was found." }
|
} builder {
|
||||||
|
inline()
|
||||||
|
doc { "Returns the single ${f.element} matching the given [predicate], or `null` if ${f.element} was not found or more than one ${f.element} was found." }
|
||||||
returns("T?")
|
returns("T?")
|
||||||
body {
|
body {
|
||||||
"""
|
"""
|
||||||
@@ -741,10 +815,12 @@ fun elements(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates addAll (1..5).map { n ->
|
val f_components = (1..5).map { n ->
|
||||||
f("component$n()") {
|
fn("component$n()") {
|
||||||
|
include(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
} builder {
|
||||||
operator(true)
|
operator(true)
|
||||||
inline(Inline.Only)
|
inlineOnly()
|
||||||
fun getOrdinal(n: Int) = n.toString() + when (n) {
|
fun getOrdinal(n: Int) = n.toString() + when (n) {
|
||||||
1 -> "st"
|
1 -> "st"
|
||||||
2 -> "nd"
|
2 -> "nd"
|
||||||
@@ -754,10 +830,7 @@ fun elements(): List<GenericFunction> {
|
|||||||
doc { "Returns ${getOrdinal(n)} *element* from the collection." }
|
doc { "Returns ${getOrdinal(n)} *element* from the collection." }
|
||||||
returns("T")
|
returns("T")
|
||||||
body { "return get(${n-1})" }
|
body { "return get(${n-1})" }
|
||||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
templates.forEach { it.sequenceClassification(terminal) }
|
|
||||||
return templates
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user