Add takeLast method for String, lists and arrays.
Check the parameter n in drop, take and takeLast methods.
This commit is contained in:
@@ -6,10 +6,12 @@ fun filtering(): List<GenericFunction> {
|
||||
val templates = arrayListOf<GenericFunction>()
|
||||
|
||||
templates add f("drop(n: Int)") {
|
||||
val n = "\$n"
|
||||
doc { "Returns a list containing all elements except first [n] elements" }
|
||||
returns("List<T>")
|
||||
body {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
var count = 0
|
||||
val list = ArrayList<T>()
|
||||
for (item in this) {
|
||||
@@ -23,6 +25,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Sequences) { "Sequence<T>" }
|
||||
body(Sequences) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
return DropSequence(this, n)
|
||||
"""
|
||||
}
|
||||
@@ -31,8 +34,9 @@ fun filtering(): List<GenericFunction> {
|
||||
body(Strings) { "return substring(Math.min(n, length()))" }
|
||||
returns(Strings) { "String" }
|
||||
|
||||
body(Collections, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
body(Collections) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
if (n >= size())
|
||||
return emptyList()
|
||||
|
||||
@@ -44,13 +48,29 @@ fun filtering(): List<GenericFunction> {
|
||||
return list
|
||||
"""
|
||||
}
|
||||
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
if (n >= size())
|
||||
return emptyList()
|
||||
|
||||
val list = ArrayList<T>(size() - n)
|
||||
for (index in n..size() - 1) {
|
||||
list.add(this[index])
|
||||
}
|
||||
return list
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("take(n: Int)") {
|
||||
val n = "\$n"
|
||||
doc { "Returns a list containing first [n] elements" }
|
||||
returns("List<T>")
|
||||
body {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
var count = 0
|
||||
val list = ArrayList<T>(n)
|
||||
for (item in this) {
|
||||
@@ -70,6 +90,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Sequences) { "Sequence<T>" }
|
||||
body(Sequences) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
return TakeSequence(this, n)
|
||||
"""
|
||||
}
|
||||
@@ -77,6 +98,7 @@ fun filtering(): List<GenericFunction> {
|
||||
include(Collections)
|
||||
body(Collections, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
require(n >= 0, "Requested element count $n is less than zero.")
|
||||
var count = 0
|
||||
val realN = if (n > size()) size() else n
|
||||
val list = ArrayList<T>(realN)
|
||||
@@ -90,6 +112,29 @@ fun filtering(): List<GenericFunction> {
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("takeLast(n: Int)") {
|
||||
val n = "\$n"
|
||||
doc { "Returns a list containing last [n] elements" }
|
||||
only(Lists, ArraysOfObjects, ArraysOfPrimitives, Strings)
|
||||
returns("List<T>")
|
||||
|
||||
doc(Strings) { "Returns a string containing the last [n] characters from this string, or the entire string if this string is shorter"}
|
||||
body(Strings) { "return substring(length() - Math.min(n, length()), length())" }
|
||||
returns(Strings) { "String" }
|
||||
|
||||
body(Lists, ArraysOfObjects, ArraysOfPrimitives) {
|
||||
"""
|
||||
require(n >= 0, { "Requested element count $n is less than zero." })
|
||||
val size = size()
|
||||
val realN = if (n > size) size else n
|
||||
val list = ArrayList<T>(realN)
|
||||
for (index in size - realN .. size - 1)
|
||||
list.add(this[index])
|
||||
return list
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
templates add f("dropWhile(predicate: (T) -> Boolean)") {
|
||||
inline(true)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user