Skip to content

fix: reified groupNDArrayBy values and empty chunked/windowed (#221) - #362

Merged
devcrocod merged 1 commit into
developfrom
fix/221-followup
Oct 30, 2026
Merged

devcrocod merged 1 commit into
developfrom
fix/221-followup

Conversation

@devcrocod

Copy link
Copy Markdown
Collaborator

Follow-up to #357. Fixes #221.

  • JS dtype in groupNDArrayBy / groupNDArrayByTo with valueTransform. The dtype came from the first value's runtime class, and on JS a whole-valued Double (and any Byte, Short or Float) is an Int at runtime. V is now reified and the dtype comes from it.
  • Abstract element types. flatMap* (since fix: preserve array dtype on empty filter and transform operations (#221) #357) and the reified groupNDArrayBy* threw IllegalStateException for Number or Any, even on empty input. A shared inferDataType helper takes the dtype from the static type when it names one, and otherwise from the first element, as before fix: preserve array dtype on empty filter and transform operations (#221) #357.
  • chunked / windowed on empty input threw ArithmeticException: / by zero. They now return an empty (0, 0) array; the window width is clamped to the element count, as it is for short arrays.
  • ndarrayCommon: the error for an empty collection without a dtype is now a require that names the shape and suggests toNDArray() or mk.zeros<T>(...).
  • Docs: signatures in flatMap.md and groupNDArray.md. Tests from fix: preserve array dtype on empty filter and transform operations (#221) #357 renamed to testXxx per CONTRIBUTING.md, with an unused import and lambda parameter removed.

API changes

multik-core.api loses two lines, because reified inline functions compile to private methods:

-	public static final fun groupNDArrayBy (Lorg/jetbrains/kotlinx/multik/ndarray/data/MultiArray;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
-	public static final fun groupNDArrayByTo (Lorg/jetbrains/kotlinx/multik/ndarray/data/MultiArray;Ljava/util/Map;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
  • Binary: compiled Kotlin callers keep working, since the body is inlined. Java can no longer call these two functions, and prebuilt klibs that call them need a rebuild.
  • Source: a call whose V is a non-reified type parameter no longer compiles ("Cannot use 'V' as reified type parameter"), and groupNDArrayByTo now requires V : Any. These are the same rules as for map, flatMap and scan.

Release notes

  • chunked, windowed and the operations fixed in fix: preserve array dtype on empty filter and transform operations (#221) #357 return empty arrays on empty input instead of throwing.
  • Non-reified mk.ndarray(elements: Collection<T>, …) on an empty collection throws IllegalArgumentException (was NoSuchElementException).
  • groupNDArrayBy / groupNDArrayByTo with valueTransform take a reified V (see API changes).

Testing

  • JVM: :multik-core:jvmTest, 689 tests, 0 failures.
  • Native: :multik-core:macosArm64Test, 674 tests, 0 failures.
  • JS: the JS test tasks are disabled in the build (Enable WASM and JS tests #247), so jsNodeTest was run locally with the Node test task enabled: IterableNDArrayTest 48/48, ErrorHandlingTest 8/8. Before the fix, the existing test_groupNDArrayBy and the new testGroupNDArrayByValueTransformDtype failed on JS (IntDataType instead of DoubleDataType).
  • apiCheck and korroCheck pass.

Checklist

  • Existing tests pass
  • New/updated tests for changed behavior
  • New/updated documentation if necessary

Copilot AI balanced review requested due to automatic review settings October 27, 2026 16:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The acknowledged compatibility breaks for Java, non-reified generic callers, and prebuilt klibs require explicit maintainer acceptance.

No open issues 🎉

What changed in this PR

This follow-up to #357 addresses #221 by improving dtype inference and empty-array handling in Multik’s collection-style operations.

Changes:

  • Uses reified grouping value types to preserve dtypes on JS, with runtime inference for abstract types when elements are available.
  • Returns (0, 0) arrays for empty chunked and windowed input and improves empty-constructor errors.
  • Adds regression tests and synchronizes documentation and the API dump.
File Description
multik-core/​src/​commonTest/​kotlin/​org/​jetbrains/​kotlinx/​multik/​ndarray/​data/​ErrorHandlingTest.kt Checks shape details in empty-constructor errors.
multik-core/​src/​commonTest/​kotlin/​org/​jetbrains/​kotlinx/​multik/​iterable/​IterableNDArrayTest.kt Adds dtype and empty-input regressions; standardizes affected test names.
multik-core/​src/​commonMain/​kotlin/​org/​jetbrains/​kotlinx/​multik/​ndarray/​operations/​IteratingNDArray.kt Updates dtype inference and handles empty windows.
multik-core/​src/​commonMain/​kotlin/​org/​jetbrains/​kotlinx/​multik/​api/​constructors.kt Makes empty-collection errors more actionable.
multik-core/​api/​multik-core.api Records removal of two grouping signatures from the API dump.
docs/​topics/​apiDocs/​groupNDArray.md Documents the reified grouping value type.
docs/​topics/​apiDocs/​flatMap.md Corrects result-type bounds and multidimensional signatures.

Review effort: Balanced

- groupNDArrayBy/groupNDArrayByTo with valueTransform take a reified V, so the
  dtype comes from V instead of the first value's runtime class, which is Int
  for a whole-valued Double on JS.
- flatMap* and groupNDArrayBy* fall back to the runtime class when the element
  type is abstract (Number, Any), as before #357.
- chunked/windowed return an empty (0, 0) array on empty input instead of
  dividing by zero.
- ndarrayCommon names the shape and the alternatives when it cannot infer the
  dtype of an empty collection.
- Sync the flatMap and groupNDArrayBy signatures in the docs, rename the #357
  tests to testXxx and update the API dump.
@devcrocod
devcrocod merged commit 5b670e9 into develop Oct 30, 2026
2 checks passed
@devcrocod
devcrocod deleted the fix/221-followup branch October 30, 2026 12:17
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

2 participants