-
Notifications
You must be signed in to change notification settings - Fork 1k
Description
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:
- A boolean array is derived by evaluating a predicate on the input array (e.g., values > threshold)
- The
filterfunction is used with this boolean array to extract elements where the predicate istrue, and with its complement to extract elements where the predicate isfalse - Separate computations are performed on these two filtered arrays (e.g., compute_a(true_values) and compute_b(false_values))
- 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