Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Kotlin FloatArray from Iterable<Float>
Parent
Kotlin FloatArray from Iterable<Float>
Suppose I am using a Java API from Kotlin and I want to call a function that takes a FloatArray
(represented as float[]
in Java), and I have an Iterable<Float>
as my input data type. To be more precise, I have a MutableList<Triple<Float, Float, Float>>
, and I'm feeding it to the function like this:
val myList: MutableList<Triple<Float, Float, Float>> = getElements()
// Flatten list of tuples down to array of Float
val myIt = myList.flatMap { (x, y, z) -> listOf(x, y, z) }
myFloatBuffer.put(myIt.toList().toFloatArray())
From my experimentation, List<Float>
converts freely to FloatArray
through toFloatArray
, but evaluating an iterator into a list and then converting it into an array seems unnecessary at best. Is there an idiomatic way to create a Kotlin FloatArray
from an Iterable<Float>
?
Additionally, the above code implies that the goal is to put
a series of Float
s into a FloatBuffer
. Is there a more idiomatic way to accomplish this? Perhaps one that avoids the FloatArray
altogether?
Post
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
Josh Hyatt |
Thread: Works for me Very detailed, useful answer to each part of the question. I appreciate that you pointed out that an unsized iterable can't be used to create a fixed-s... |
Jan 31, 2022 at 17:49 |
In your situation, the most obvious thing to do is use a for loop over the Iterable
or the Iterable.forEach
extension method depending on your preference, and directly put
floats into the FloatBuffer
. This avoids any intermediate data structure. Indeed, I'd do this directly over myList
to avoid the 3-element list intermediates.
for ((x, y, z) in myList) {
myFloatBuffer.put(x)
myFloatBuffer.put(y)
myFloatBuffer.put(z)
}
As far as creating a FloatArray
from an Iterable
/Iterator
, I think I ran into a similar annoyance last time I was using Kotlin, which was admittedly a while ago. I don't think there's a nice, efficient way to do this in the standard library, though I could have missed something. It's understandable why it is this way; there's no way to know a priori how many elements an Iterator
/Iterable
has and FloatArray
s can't be resized. The standard library option is thus along the lines that you went: convert to a dynamically sized data structure whose size can be measured, namely a List
, and then convert that to an array. If you do know what the size of the array is, then you have a couple of options. One option is to create the array and then mutate it after the fact:
val myFloatArray = FloatArray(knownSize)
myIterable.forEachIndexed { i, x -> myFloatArray[i] = x }
// or
myIterator.withIndex().forEach { (i, x) -> myFloatArray[i] = x }
Obviously, this will lead to an array out of bounds exception if there are more than knownSize
elements in the Iterable
/Iterator
. If there are fewer, then the remaining elements of the array will be populated with the default value of 0 (or you could specify a different default to the FloatArray
constructor).
A slightly more efficient but abusive option would be:
val myFloatArray = FloatArray(knownSize) { myIterator.nextFloat() }
In this case, if myIterator
has more than knownSize
elements, they will simply be left unconsumed. If myIterator
has too few elements, you'd get a NoSuchElementException
.
If performance isn't a concern, the toList().toFloatArray()
approach is likely the most concise and idiomatic.
0 comment threads