I have made a function which takes in an end point and a step, then produces a list of integers with the step in mind. For example, if the end point is 10 and the step is 2, the function should return the list [0, 2, 4, 6, 8, 10].
Can I make this function more efficient?
Here is the function:
fun numberStepsFromZero(end: Int, step: Int): MutableList<Int> {
if (step == 0) {
throw IllegalArgumentException("Step cannot be 0")
}
var currentNumber = 0
val list = mutableListOf<Int>()
while (currentNumber <= end) {
list.add(currentNumber)
currentNumber += step
}
return list
}
Example usage:
val list = try {
numberStepsFromZero(10, 0)
} catch (e: Exception) {
Log.d("TAAAG", "$e")
}
range()
in Python,linspace()
in Matlab,⍳
in APL andiota()
in C++. \$\endgroup\$