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

Copy link
Copy Markdown

Fixes #221

filter, filterIndexed, filterMultiIndexed, filterNot, distinctBy, and partition in IteratingNDArray.kt were inferring the return array's dtype by calling DataType.of(list.first()). When the filtering predicate matched zero elements, list.first() threw NoSuchElementException instead of returning an empty array of the parent array's type.

Additionally, in ndarrayCommon (constructors.kt), val dtypeFromFirst = DataType.of(elements.first()) was evaluated eagerly even when an explicit dtype was supplied, causing crashes on empty collections.

This PR uses the parent array's this.dtype directly in these operations and lazily resolves dtype in ndarrayCommon. Added regression tests for empty filtering across all operations.

@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