Skip to content

Provide algorithm that allows zipping arrays whose values are not prealigned #8752

@pepijnve

Description

@pepijnve

Is your feature request related to a problem or challenge? Please describe what you are trying to do.

The zip algorithm requires two arrays of equal length. This requires callers to ensure the truth and false values are correctly aligned before calling zip. When zip is used to merge two partial results, this can be cumbersome.

Consider a typical pattern where you want to conditionally compute one value or another for a single input array depending on a predicate:

  1. A boolean array is derived by evaluating a predicate on the input array (e.g., values > threshold)
  2. The filter function is used with this boolean array to extract elements where the predicate is true, and with its complement to extract elements where the predicate is false
  3. Separate computations are performed on these two filtered arrays (e.g., compute_a(true_values) and compute_b(false_values))
  4. The results are two arrays that are smaller than the original array. To use zip, these results must first be "scattered" back to their original indices to create two full-length arrays where each position aligns with the original boolean mask

This scatter step is a bit wasteful because it requires allocating additional memory and copying values to intermediate arrays that exist solely to satisfy zip's equal-length requirement. It would be nice to be able to avoid this intermediate step.

Describe the solution you'd like

Some array combination algorithm that does not require the values in the input arrays to be aligned.

Describe alternatives you've considered

Use interleave.

The interleave algorithm can be used to achieve the desired result, but this requires computing an index array that's more complex than necessary. In the example given above, the boolean array has already been computed. It would be nice to be able to use that as is for the merge operation.

Additional context

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementAny new improvement worthy of a entry in the changelog

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions