oday
07/14/2021, 9:30 AMval leftOne = arrayOne.drop(0).filter { it < arrayOne.first() }.toIntArray()
oday
07/14/2021, 9:31 AModay
07/14/2021, 9:32 AMdrop(arrayone.first())
would be neaterUbi
07/14/2021, 11:34 AMdrop(arrayone.first())
will be incorrect. drop(n)
removes first n elements from the array. drop(1)
is correct in your case.
See:
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/drop.htmlTobias Berger
07/14/2021, 11:52 AMdrop(1)
the result should be identical to your python code, although the code flow is a bit different. using drop and filter will create List instances, so if performance is crucial, you might benefit from converting to a sequence before these operations and back to a list later. Also, in Kotlin, especially when working with JVM, Lists are far more common than arrays.
If you want a neater way, I suggest refactoring and not using the first array item for the comparison. Just have one parameter for the reference value and another for the items you want to filter. Of course I don't know your specific use case and if there is anything really blocking you from doing that.Tobias Berger
07/14/2021, 11:55 AMarray.filter { it < referenceValue }.toIntArray()
or just work with Lists and omit the toIntArray()
calloday
07/14/2021, 1:45 PModay
07/14/2021, 1:45 PModay
07/14/2021, 1:46 PMDave K
07/14/2021, 2:05 PMarray.first().run { array.filter { it < this }.toIntArray() }
I’m not sure where the sweet spot for you is in terms of keeping it as close to the metal as possible while also being neater