awkward icon indicating copy to clipboard operation
awkward copied to clipboard

refactor setter boilerplate code

Open ianna opened this issue 9 months ago • 0 comments

          @pfackeldey - I wonder if we should try to avoid this boilerplate code. Perhaps, something like:
class BackendArraySetter:
    def __init__(self, array, backend):
        self.array = array
        self.backend = backend
        self.nplike = backend.nplike

    def __setitem__(self, index, value):
        if hasattr(self.nplike, "at"):  # JAX-like
            self.array = self.array.at[index].set(value)
        elif hasattr(self.array, "__setitem__"):  # NumPy, CuPy, etc.
            self.array[index] = value
        elif hasattr(self.nplike, "scatter"):  # PyTorch-like (custom handling)
            # You can define custom scatter logic here if needed
            raise NotImplementedError("Add scatter logic for torch here.")
        else:
            raise TypeError(f"Unsupported nplike type: {type(self.nplike)}")

    def result(self):
        return self.array

Originally posted by @ianna in https://github.com/scikit-hep/awkward/pull/3457#discussion_r2040608193

ianna avatar Apr 14 '25 08:04 ianna