Florent Hivert wrote: > Dear Jason, >> Okay, how about this: >> >> class inline_operator: > > You probably mean : > > class infix_operator: > ... > > "infix" remember "infix" :)
Yes. Right. Thanks. > >> # EXAMPLE >> >> a=[1,2,3] >> b=[3,4,5] >> >> @inline_operator >> def emul(a,b): >> return [i*j for i,j in zip(a,b)] >> >> # Returns [3,8,15] >> a *emul* b > > Another suggestion: It allows also to define prefix postfix operator by > partial specialization:: > > sage: bla = emul*[1,2,3] > sage: [4,5,6]*bla > [4, 10, 18] > > sage: bla = [1,2,3]*emul > sage: bla*[4,5,6] > [4, 10, 18] > > which looks perfectly good to me. However I'd rather having an error it the > following two behavior:: > > sage: bla = emul*[1,2,3]*[1,2,4] > sage: [4,5,6]*bla > [4, 10, 24] > > sage: bla = [1,2,4]*emul > sage: [1,1,1]*bla*[1,1,1] > [1, 1, 1] > > That is when left or right are not None, multiplying raise an error instead of > replacing them silently:: > > def __rmul__(self, left): > if self.right is None: > if self.left is None: > return inline_operator(self.function, left=left) > else: > raise SyntaxError, "Infix operator already has its left > argument" > else: > return self.function(left, self.right) > > def __mul__(self, right): > if self.left is None: > if self.right is None: > return inline_operator(self.function, right=right) > else: > raise SyntaxError, "Infix operator already has its right > argument" > else: > return self.function(self.left, right) > > What do you think ? > I think it's converging to a nice solution! Good job. Jason --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to sage-devel-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---