On Thu, Aug 21, 2008 at 9:41 PM, Arnaud Bergeron <[EMAIL PROTECTED]> wrote: > >>> > data /. x_?(# < 0 &) -> 0 (this is perhaps not the killer example) >>> >>> What does that do? >> >> /. is the pattern replacement operator, _ is a placeholder pattern >> that matches anything, x_ gives this placeholder a name so you can use >> it later, ? filters the matches (in this case, everything matches) >> with the pure function # < 0&, which takes one argument and returns >> true if the argument is less than 0, and the -> 0 part says anything >> that made it through the filter gets set to 0. So >> >> In[40]:= data = {-1, 2, 3}; >> In[41]:= data /. x_?(# < 0 &) -> 0 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Just out of curiosity, what words do you say in your head when you read In[41]? I would like to understand what people are thinking when they find an expression like that easy to read and appealing. When I look at Arnaud's python version: > sage: data = [-1, 2, 3] > sage: [(0 if d < 0 else d) for d in data] I read "the list got from data where the negative entries are replaced by 0", or maybe more literally "the list made by taking each entry d of data and putting 0 if it is negative else putting d." I want to strongly emphasize that (1) I'm not trying to start a "mathematica vs python language flamewar", that (2) I'm not trying to claim above that there is anything bad about Mathematica's notation, and (3) I'm also not suggesting Sage should have Mathematica's notation. That said, I could certainly see a place in sage for something like this: sage: data = [-1, 2, 3] sage: data = ma_eval('data /. x_?(# < 0 &) -> 0') sage: data [0, 2, 3] where ma_eval is a function that evaluates a mathematica-style expression in the scope of the current Sage session, and returns the result as a Python object. It's something that could be totally ignored by most users, but something people who understand the power of Mathematica might find very useful. Anyway, that's why this thread is I think potentially interesting. I recall Ondrej Certik once posting something about a GSoC project from maybe a year ago that maybe did something like that; I'm not sure. >> Out[41]= {0, 2, 3} >> >> In other words >> sage: data = [-1,2,3] >> sage: [(d < 0 and [0] or [d])[0] for d in data] >> [0,2,3] > > What about > > sage: data = [-1, 2, 3] > sage: [(0 if d < 0 else d) for d in data] > [0, 2, 3] > > Which is way clearer to me. > > Arnaud > >> I haven't been using python long enough to think this one is nice, but >> Mark Pilgrim says it's cool: >> http://diveintopython.org/power_of_introspection/and_or.html#d0e9975 >> > > > > -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---