Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Kotlin FloatArray from Iterable<Float>

+5
−0

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 Floats into a FloatBuffer. Is there a more idiomatic way to accomplish this? Perhaps one that avoids the FloatArray altogether?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+7
−0

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 FloatArrays 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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Works for me (1 comment)

Sign up to answer this question »