StdLib generators: provide extension points for family properties specific to documentation and code.
This commit is contained in:
@@ -446,18 +446,16 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
returns("T")
|
returns("T")
|
||||||
body { f ->
|
body { f ->
|
||||||
with (DocExtensions) {
|
"""
|
||||||
"""
|
if (isEmpty())
|
||||||
if (isEmpty())
|
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||||
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
|
|
||||||
|
|
||||||
var accumulator = this[0]
|
var accumulator = this[0]
|
||||||
for (index in 1..lastIndex) {
|
for (index in 1..lastIndex) {
|
||||||
accumulator = operation(index, accumulator, this[index])
|
accumulator = operation(index, accumulator, this[index])
|
||||||
}
|
|
||||||
return accumulator
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
return accumulator
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -488,18 +486,16 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(ArraysOfObjects) { f ->
|
body(ArraysOfObjects) { f ->
|
||||||
with (DocExtensions) {
|
"""
|
||||||
"""
|
if (isEmpty())
|
||||||
if (isEmpty())
|
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||||
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
|
|
||||||
|
|
||||||
var accumulator: S = this[0]
|
var accumulator: S = this[0]
|
||||||
for (index in 1..lastIndex) {
|
for (index in 1..lastIndex) {
|
||||||
accumulator = operation(index, accumulator, this[index])
|
accumulator = operation(index, accumulator, this[index])
|
||||||
}
|
|
||||||
return accumulator
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
return accumulator
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,20 +511,18 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
}
|
}
|
||||||
returns("T")
|
returns("T")
|
||||||
body { f ->
|
body { f ->
|
||||||
with (DocExtensions) {
|
"""
|
||||||
"""
|
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.collection} can't be reduced.")
|
|
||||||
|
|
||||||
var accumulator = get(index--)
|
var accumulator = get(index--)
|
||||||
while (index >= 0) {
|
while (index >= 0) {
|
||||||
accumulator = operation(index, get(index), accumulator)
|
accumulator = operation(index, get(index), accumulator)
|
||||||
--index
|
--index
|
||||||
}
|
|
||||||
|
|
||||||
return accumulator
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -546,20 +540,18 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
typeParam("T: S")
|
typeParam("T: S")
|
||||||
returns("S")
|
returns("S")
|
||||||
body { f ->
|
body { f ->
|
||||||
with (DocExtensions) {
|
"""
|
||||||
"""
|
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.collection} can't be reduced.")
|
|
||||||
|
|
||||||
var accumulator: S = get(index--)
|
var accumulator: S = get(index--)
|
||||||
while (index >= 0) {
|
while (index >= 0) {
|
||||||
accumulator = operation(index, get(index), accumulator)
|
accumulator = operation(index, get(index), accumulator)
|
||||||
--index
|
--index
|
||||||
}
|
|
||||||
|
|
||||||
return accumulator
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return accumulator
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,18 +562,16 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
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 { f -> "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 { f ->
|
||||||
with (DocExtensions) {
|
"""
|
||||||
"""
|
if (isEmpty())
|
||||||
if (isEmpty())
|
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||||
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
|
|
||||||
|
|
||||||
var accumulator = this[0]
|
var accumulator = this[0]
|
||||||
for (index in 1..lastIndex) {
|
for (index in 1..lastIndex) {
|
||||||
accumulator = operation(accumulator, this[index])
|
accumulator = operation(accumulator, this[index])
|
||||||
}
|
|
||||||
return accumulator
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
return accumulator
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -606,18 +596,16 @@ fun aggregates(): List<GenericFunction> {
|
|||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
body(ArraysOfObjects) { f ->
|
body(ArraysOfObjects) { f ->
|
||||||
with (DocExtensions) {
|
"""
|
||||||
"""
|
if (isEmpty())
|
||||||
if (isEmpty())
|
throw UnsupportedOperationException("Empty ${f.doc.collection} can't be reduced.")
|
||||||
throw UnsupportedOperationException("Empty ${f.collection} can't be reduced.")
|
|
||||||
|
|
||||||
var accumulator: S = this[0]
|
var accumulator: S = this[0]
|
||||||
for (index in 1..lastIndex) {
|
for (index in 1..lastIndex) {
|
||||||
accumulator = operation(accumulator, this[index])
|
accumulator = operation(accumulator, this[index])
|
||||||
}
|
|
||||||
return accumulator
|
|
||||||
"""
|
|
||||||
}
|
}
|
||||||
|
return accumulator
|
||||||
|
"""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ enum class Family {
|
|||||||
|
|
||||||
val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations }
|
val isPrimitiveSpecialization: Boolean by lazy { this in primitiveSpecializations }
|
||||||
|
|
||||||
|
class DocExtension(val family: Family)
|
||||||
|
class CodeExtension(val family: Family)
|
||||||
|
val doc = DocExtension(this)
|
||||||
|
val code = CodeExtension(this)
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val primitiveSpecializations = setOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives)
|
val primitiveSpecializations = setOf(ArraysOfPrimitives, RangesOfPrimitives, ProgressionsOfPrimitives, Primitives)
|
||||||
val defaultFamilies = setOf(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
|
val defaultFamilies = setOf(Iterables, Sequences, ArraysOfObjects, ArraysOfPrimitives)
|
||||||
|
|||||||
+13
@@ -2,6 +2,19 @@ package templates
|
|||||||
|
|
||||||
import templates.Family.*
|
import templates.Family.*
|
||||||
|
|
||||||
|
val Family.DocExtension.collection: String
|
||||||
|
get() = with(DocExtensions) { family.collection }
|
||||||
|
|
||||||
|
val Family.DocExtension.element: String
|
||||||
|
get() = with(DocExtensions) { family.element }
|
||||||
|
|
||||||
|
val Family.CodeExtension.size: String
|
||||||
|
get() = when (family) {
|
||||||
|
Iterables, Collections, Lists, Sets, Maps, InvariantArraysOfObjects, ArraysOfObjects, ArraysOfPrimitives -> "size"
|
||||||
|
CharSequences, Strings -> "length"
|
||||||
|
else -> error("size property isn't supported for $family")
|
||||||
|
}
|
||||||
|
|
||||||
object DocExtensions {
|
object DocExtensions {
|
||||||
|
|
||||||
val Family.element: String
|
val Family.element: String
|
||||||
Reference in New Issue
Block a user