Chain like Ruby

I think it can be simplified more to mimic the Hack :wink:

from functools import partial

class apply(partial):
    def __rmatmul__(self, argv):
        return self(argv)

p = apply(print)
>>> range(5) @apply(list) @p
[0, 1, 2, 3, 4]

>>> [2, 1, 3] @apply(map, lambda x: x*x) @apply(sorted) @p
[1, 4, 9]

Unfortunately, the following doesn’t work: :worried:

>>> [2, 1, 3] @apply(np.array) @p
ValueError: matmul: Input operand 1 does not have enough dimensions ... (snip) ...

because (x @ y) calls x.__matmul__ prior to y.__rmatmul__.

1 Like