The matrix multiplication operator @ is going to be introduced in Python
3.5 and I am thinking about the following idea:
The semantics of matrix multiplication is the composition of the
corresponding linear transformations.
A linear transformation is a particular example of a more general concept -
The latter are frequently composed with ("wrap") each other. For example:
However, it is not very readable in case of many wrapping layers.
Therefore, it could be useful to employ
the matrix multiplication operator @ for indication of function
composition. This could be done by such (simplified) decorator:
def __init__(self, func):
self.func = func
def __call__(self, arg):
def __matmul__(self, other):
def composition(*args, **kwargs):
return self.func(other(*args, **kwargs))
I think using such decorator with functions that are going to be deeply
could improve readability.
You could compare (note that only the outermost function should be
plot(sorted(sqrt(real(data_array)))) vs. (plot @ sorted @ sqrt @ real)
I think the latter is more readable, also compare
sunique = sorted @ list @ set
Apart from readability, there are following pros of the proposed decorator:
1. Similar semantics as for matrix multiplication.
2. Same symbol for composition as for decorators.
3. The symbol @ resembles mathematical notation for function composition: ∘
I think it could be a good idea to add such a decorator to the stdlib