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.
Post History
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 mo...
#1: Initial revision
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: ```kotlin 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?