Clean size/length/first/head/empty properties and functions.
This commit is contained in:
@@ -95,8 +95,11 @@ fun aggregates(): List<GenericFunction> {
|
||||
return count
|
||||
"""
|
||||
}
|
||||
body(Strings, Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"return size"
|
||||
body(Strings) {
|
||||
"return length()"
|
||||
}
|
||||
body(Maps, Collections, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"return size()"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,7 +161,7 @@ fun aggregates(): List<GenericFunction> {
|
||||
}
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
if (size == 0) return null
|
||||
if (size() == 0) return null
|
||||
|
||||
var minElem = this[0]
|
||||
var minValue = f(minElem)
|
||||
@@ -330,7 +333,7 @@ fun aggregates(): List<GenericFunction> {
|
||||
returns("R")
|
||||
body {
|
||||
"""
|
||||
var index = size - 1
|
||||
var index = lastIndex
|
||||
var accumulator = initial
|
||||
while (index >= 0) {
|
||||
accumulator = operation(get(index--), accumulator)
|
||||
@@ -368,7 +371,7 @@ fun aggregates(): List<GenericFunction> {
|
||||
returns("T")
|
||||
body {
|
||||
"""
|
||||
var index = size - 1
|
||||
var index = lastIndex
|
||||
if (index < 0) throw UnsupportedOperationException("Empty iterable can't be reduced")
|
||||
|
||||
var accumulator = get(index--)
|
||||
|
||||
@@ -10,7 +10,7 @@ fun arrays(): List<GenericFunction> {
|
||||
doc { "Returns true if the array is empty" }
|
||||
returns("Boolean")
|
||||
body {
|
||||
"return size == 0"
|
||||
"return size() == 0"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -159,7 +159,7 @@ fun elements(): List<GenericFunction> {
|
||||
"""
|
||||
when (this) {
|
||||
is List<*> -> {
|
||||
if (size == 0)
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty")
|
||||
else
|
||||
return this[0] as T
|
||||
@@ -175,7 +175,7 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
if (size == 0)
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty")
|
||||
return this[0]
|
||||
"""
|
||||
@@ -188,7 +188,7 @@ fun elements(): List<GenericFunction> {
|
||||
"""
|
||||
when (this) {
|
||||
is List<*> -> {
|
||||
if (size == 0)
|
||||
if (isEmpty())
|
||||
return null
|
||||
else
|
||||
return this[0] as T
|
||||
@@ -204,7 +204,7 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
return if (size > 0) this[0] else null
|
||||
return if (isEmpty()) null else this[0]
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -242,10 +242,10 @@ fun elements(): List<GenericFunction> {
|
||||
"""
|
||||
when (this) {
|
||||
is List<*> -> {
|
||||
if (size == 0)
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty")
|
||||
else
|
||||
return this[size - 1] as T
|
||||
return this[this.lastIndex] as T
|
||||
}
|
||||
else -> {
|
||||
val iterator = iterator()
|
||||
@@ -259,11 +259,22 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Streams) {
|
||||
"""
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
throw NoSuchElementException("Collection is empty")
|
||||
var last = iterator.next()
|
||||
while (iterator.hasNext())
|
||||
last = iterator.next()
|
||||
return last
|
||||
"""
|
||||
}
|
||||
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
if (size == 0)
|
||||
if (isEmpty())
|
||||
throw NoSuchElementException("Collection is empty")
|
||||
return this[size - 1]
|
||||
return this[lastIndex]
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -274,7 +285,7 @@ fun elements(): List<GenericFunction> {
|
||||
body {
|
||||
"""
|
||||
when (this) {
|
||||
is List<*> -> return if (size > 0) this[size - 1] as T else null
|
||||
is List<*> -> return if (isEmpty()) null else this[size() - 1] as T
|
||||
else -> {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
@@ -287,9 +298,25 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
body(Streams) {
|
||||
"""
|
||||
return if (size > 0) this[size - 1] else null
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
return null
|
||||
var last = iterator.next()
|
||||
while (iterator.hasNext())
|
||||
last = iterator.next()
|
||||
return last
|
||||
"""
|
||||
}
|
||||
body(Strings) {
|
||||
"""
|
||||
return if (isEmpty()) null else this[length() - 1]
|
||||
"""
|
||||
}
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
return if (isEmpty()) null else this[size() - 1]
|
||||
"""
|
||||
}
|
||||
}
|
||||
@@ -331,14 +358,13 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
val bucks = '$'
|
||||
templates add f("single()") {
|
||||
doc { "Returns single element, or throws exception if there is no or more than one element" }
|
||||
returns("T")
|
||||
body {
|
||||
"""
|
||||
when (this) {
|
||||
is List<*> -> return when (size) {
|
||||
is List<*> -> return when (size()) {
|
||||
0 -> throw NoSuchElementException("Collection is empty")
|
||||
1 -> this[0] as T
|
||||
else -> throw IllegalArgumentException("Collection has more than one element")
|
||||
@@ -355,9 +381,18 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
body(Strings) {
|
||||
"""
|
||||
return when (size) {
|
||||
return when (length()) {
|
||||
0 -> throw NoSuchElementException("Collection is empty")
|
||||
1 -> this[0]
|
||||
else -> throw IllegalArgumentException("Collection has more than one element")
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
return when (size()) {
|
||||
0 -> throw NoSuchElementException("Collection is empty")
|
||||
1 -> this[0]
|
||||
else -> throw IllegalArgumentException("Collection has more than one element")
|
||||
@@ -372,7 +407,7 @@ fun elements(): List<GenericFunction> {
|
||||
body {
|
||||
"""
|
||||
when (this) {
|
||||
is List<*> -> return if (size == 1) this[0] as T else null
|
||||
is List<*> -> return if (size() == 1) this[0] as T else null
|
||||
else -> {
|
||||
val iterator = iterator()
|
||||
if (!iterator.hasNext())
|
||||
@@ -385,9 +420,14 @@ fun elements(): List<GenericFunction> {
|
||||
}
|
||||
"""
|
||||
}
|
||||
body(Strings, Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
body(Strings) {
|
||||
"""
|
||||
return if (size == 1) this[0] else null
|
||||
return if (length() == 1) this[0] else null
|
||||
"""
|
||||
}
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
return if (size() == 1) this[0] else null
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,16 +27,16 @@ fun filtering(): List<GenericFunction> {
|
||||
"""
|
||||
}
|
||||
|
||||
body(Strings) { "return substring(Math.min(n, size))" }
|
||||
body(Strings) { "return substring(Math.min(n, length()))" }
|
||||
returns(Strings) { "String" }
|
||||
|
||||
body(Collections, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
if (n >= size)
|
||||
if (n >= size())
|
||||
return ArrayList<T>()
|
||||
|
||||
var count = 0
|
||||
val list = ArrayList<T>(size - n)
|
||||
val list = ArrayList<T>(size() - n)
|
||||
for (item in this) {
|
||||
if (count++ >= n) list.add(item)
|
||||
}
|
||||
@@ -61,7 +61,7 @@ fun filtering(): List<GenericFunction> {
|
||||
"""
|
||||
}
|
||||
|
||||
body(Strings) { "return substring(0, Math.min(n, size))" }
|
||||
body(Strings) { "return substring(0, Math.min(n, length()))" }
|
||||
returns(Strings) { "String" }
|
||||
|
||||
doc(Streams) { "Returns a stream containing first *n* elements" }
|
||||
@@ -76,7 +76,7 @@ fun filtering(): List<GenericFunction> {
|
||||
body(Collections, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
var count = 0
|
||||
val realN = if (n > size) size else n
|
||||
val realN = if (n > size()) size() else n
|
||||
val list = ArrayList<T>(realN)
|
||||
for (item in this) {
|
||||
if (count++ == realN)
|
||||
|
||||
@@ -19,7 +19,7 @@ fun sets(): List<GenericFunction> {
|
||||
}
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
val set = LinkedHashSet<T>(size)
|
||||
val set = LinkedHashSet<T>(size())
|
||||
for (item in this) set.add(item)
|
||||
return set
|
||||
"""
|
||||
|
||||
@@ -52,7 +52,7 @@ fun snapshots(): List<GenericFunction> {
|
||||
*/
|
||||
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
val list = ArrayList<T>(size)
|
||||
val list = ArrayList<T>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
"""
|
||||
@@ -65,7 +65,7 @@ fun snapshots(): List<GenericFunction> {
|
||||
returns("List<Pair<K, V>>")
|
||||
body {
|
||||
"""
|
||||
val result = ArrayList<Pair<K, V>>(size)
|
||||
val result = ArrayList<Pair<K, V>>(size())
|
||||
for (item in this)
|
||||
result.add(item.key to item.value)
|
||||
return result
|
||||
@@ -93,7 +93,7 @@ fun snapshots(): List<GenericFunction> {
|
||||
*/
|
||||
body(ArraysOfPrimitives) {
|
||||
"""
|
||||
val list = ArrayList<T>(size)
|
||||
val list = ArrayList<T>(size())
|
||||
for (item in this) list.add(item)
|
||||
return list
|
||||
"""
|
||||
|
||||
@@ -19,10 +19,10 @@ fun specialJVM(): List<GenericFunction> {
|
||||
doc { "Returns new array which is a copy of the original array" }
|
||||
returns("SELF")
|
||||
body {
|
||||
"return Arrays.copyOf(this, size)"
|
||||
"return Arrays.copyOf(this, size())"
|
||||
}
|
||||
body(ArraysOfObjects) {
|
||||
"return Arrays.copyOf(this, size) as Array<T>"
|
||||
"return Arrays.copyOf(this, size()) as Array<T>"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ fun specialJVM(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size)") {
|
||||
templates add f("binarySearch(element: T, fromIndex: Int = 0, toIndex: Int = size())") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
doc { "Searches array or range of array for provided element index using binary search algorithm. Array is expected to be sorted." }
|
||||
@@ -63,7 +63,7 @@ fun specialJVM(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("sort(fromIndex: Int = 0, toIndex: Int = size)") {
|
||||
templates add f("sort(fromIndex: Int = 0, toIndex: Int = size())") {
|
||||
only(ArraysOfObjects, ArraysOfPrimitives)
|
||||
exclude(PrimitiveType.Boolean)
|
||||
doc { "Sorts array or range in array inplace" }
|
||||
|
||||
Reference in New Issue
Block a user