On 13 October 2012 16:22, Mike Meyer <mwm@mired.org> wrote:
On Sat, 13 Oct 2012 19:18:12 +1100
> On 13/10/12 19:05, Yuval Greenfield wrote:
> I believe that Haskell treats operators as if they were function objects,For the record, Haskell allows operators to be used as functions by
> so you could do something like:
quoting them in ()'s (to provide the functionality of operator) and to
turn functions into operators by quoting them in ``'s.
Yeah, but then your function has to dispatch for *all*
> negative_values = map(-, values)
>
> but I think that puts the emphasis on the wrong thing. If (and that's a big
> if) we did something like this, it should be a pair of methods __op__ and
> the right-hand version __rop__ which get called on the *operands*, not the
> operator/function object:
>
> def __op__(self, other, symbol)
operators. Depending on how we handle backwards compatibility with
__add__ et. al.
I'd rather slice it the other way (leveraging $ being unsused):
def __$<op>__(self, other, right):
so it only has to dispatch on left/right invocation.
<op> must match a new grammer symbol "operator_symbol", with limits on
it to for readability reasons: say at most three characters, all
coming from an appropriate unicode class or classes (you want to catch
the current operators and dollar sign).
Both of these leave both operator precedence and backwards
compatibility to be dealt with.
>>> (2 *dot* "__mul__" |mappedover| 10-to-25) >> tolist
[20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
>>> list(map((2).__mul__, range(10, 25)))
[20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
>>> map((2).__mul__, 10-to-25) >> tolist
[20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
>>> @Inline
... def multiply(x, y): return x*y
...
>>> 3 ^multiply^ 3
9
>>> 1 |div| 3 |div| 3
0.1111111111111111
>>> 1 |div| 3 *div* 3
1.0
>>> 2 |(div|3)
0.6666666666666666
>>> (div|3)(2)
0.6666666666666666
>>> (div|(div|3))(3) # Go on, guess why!
1.0
>>> 2 + (div|3) # 'Cause you can, yo
0.6666666666666666