Skip to content

fix: retain array dtype in filter operations and avoid first() on empty results - #353

Closed
shayann07 wants to merge 1 commit into
Kotlin:developfrom
shayann07:fix/iterating-ndarray-empty-filter
Closed

shayann07 wants to merge 1 commit into
Kotlin:developfrom
shayann07:fix/iterating-ndarray-empty-filter

Conversation

@shayann07

@shayann07 shayann07 commented Oct 4, 2026 •

Copy link
Copy Markdown

Resolves #221

Addresses maintainer feedback from @devcrocod on empty collection handling and type inference across multidimensional array operations:

  1. Retain dtype on empty filter/slice results:

    • ilter, ilterIndexed, ilterMultiIndexed, ilterNot, distinctBy, groupBy, and partition in IteratingNDArray.kt now preserve his.dtype when predicates match zero elements instead of invoking DataType.of(list.first()) (which threw NoSuchElementException).
    • dropWhile now returns an empty array with his.dtype when all elements are dropped instead of throwing an error.
  2. Dtype resolution in flatMap:

    • latMap, latMapIndexed, and latMapMultiIndexed resolve dtype via DataType.ofKClass(R::class) using reified type parameters so that empty results retain their target data type.
  3. Safe constructor dimension calculation and JS type inference:

    • Primitive 2D array constructors in primitiveArrays.kt safely handle empty outer arrays without indexing element 0.
    • In constructors.kt (
      darrayCommon), when an explicit dtype is provided, type inference avoids inspecting elements.first(), fixing incorrect type inference on Kotlin/JS.

Added tests in IterableNDArrayTest.kt and ErrorHandlingTest.kt covering empty and non-empty results across all data types (Int, Double, Float, Long, Byte, Short, ComplexFloat, ComplexDouble), verifying clean passes on JVM, Native (MingwX64), and JS/WasmJS.

@shayann07

Copy link
Copy Markdown
Author

Closing for now so the approach can be discussed in #221 first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing empty list check in array filter function

1 participant