<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>On Jul 12, 2013, at 3:36, Haoyi Li <<a href="mailto:haoyi.sg@gmail.com">haoyi.sg@gmail.com</a>> wrote:</div><div><br></div><blockquote type="cite"><div>I would much prefer the somewhat-more-difficult route of modifying the parser to let `a += b` be an expression, and then you could write</div><div><br></div><div>data = fold(lambda a, b: a += b, [], iterables)</div></blockquote><div><br></div><div>But that doesn't really _mean_ anything as an expression; it's pure side effect. Besides, if that returns the new a then it invites all the expression order problems from C and friends that Python has always escaped; if it returns None, the fold doesn't actually work.</div><div><br></div><div>Meanwhile, the way you wrote it:</div><div><br></div><div><blockquote type="cite" style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">`a += b`</blockquote><br></div><div>Reminds me. We took away backticks for repr; what about using them for quoting operators? `+=` says "quoting" in the lisp sense more loudly than the PHP sense... At least to me. And this means fold wouldn't need to accept a string; it's getting a function.</div><div><br></div><div>Of course it's the exact opposite of Haskell, where you use backticks to turn a function into an operator and parens to turn an operator into a function. </div><br><blockquote type="cite">

<div>or even groovy/scala/mathematica style</div><div><br></div><div><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">data = fold(_ += _, [], iterables)</span></div></blockquote><br><div>I semi-suggested this elsewhere, but without the magic of guessing whether two underscores meant the same arg twice or two different args (so you'd have to write _1 == _2 or similar).</div><div><br></div><div>You can actually write functions this way today using an expression template library, without macros or anything:</div><div><br></div><div>    class Expr:</div><div>        def __init__(self, f=identity)</div><div>            self.f = f</div><div>        def __iadd__(self, other):</div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">            return Expr(compose(iadd, self.f))</span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">        def __call__(self, *args):</span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">            return self.f(*args)</span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); "><br></span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">    _1, _2 = Expr(), Expr()</span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); "><br></span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">Actually, this silly proof of concept _would_ work with _ for both args, but only because it won't work for much else (even adding literals).</span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); "><br></span></div><div><span style="-webkit-tap-highlight-color: rgba(26, 26, 26, 0.296875); -webkit-composition-fill-color: rgba(175, 192, 227, 0.230469); -webkit-composition-frame-color: rgba(77, 128, 180, 0.230469); ">Anyway, while you can do this, I'm not sure you should. Boost.Lambda had this kind of magic, and nobody proposed it for C++11 when real lambdas were added, because it's a clumsy fit for a language that wasn't designed for it from the start--you end up needing const, var, and ref functions to paper over the gaps where overloading doesn't quite get you there, and horrible workarounds for the operators that can't be overloaded, ...</span></div><div><br></div></body></html>