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.

Post History

81%
+7 −0
Q&A Kotlin FloatArray from Iterable<Float>

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

posted 3y ago by Derek Elkins‭

Answer
#1: Initial revision by user avatar Derek Elkins‭ · 2022-01-30T01:38:49Z (almost 3 years ago)
In your situation, the most obvious thing to do is use a for loop over the `Iterable` or the [`Iterable.forEach` extension method](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each.html) 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.

```kotlin
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:

```kotlin
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:

```kotlin
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.