First time here? Check out the FAQ!

Ask Your Question
0

Delayed evaluation without all the parenthesis?

asked 13 years ago

Alister gravatar image

updated 13 years ago

Is there a way to have delayed evaluation without having unnecessary parenthesis. For example,

(x.add(x, hold = True)).add(x, hold = True)

gives

(x + x) + x

not

x + x + x

Thanks

EDIT:

Great answer burcin! I was hoping to be able to it in more of a binary way though, so that I could use the Infix hack from http://code.activestate.com/recipes/3....

I wanted to make a binary operator which is still delayed. Defined here

class Infix(object):
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x: self.function(other, x))
    def __or__(self, other):
        return self.function(other)

p=Infix(lambda x,y: x.add(y, hold=True))

Usage is

x |p| x |p| x
Preview: (hide)

2 Answers

Sort by » oldest newest most voted
2

answered 13 years ago

Is this what you need?

sage: x.add(x,x,hold=True)
x + x + x
Preview: (hide)
link
0

answered 13 years ago

Jason Grout gravatar image

Sage already has an infix_operator decorator:

sage: @infix_operator('or')
....: def f(x,y): return x.add(y, hold=True)
....: 
sage: x |f| x |f| x
(x + x) + x

See http://sagemath.org/doc/reference/sag...

Preview: (hide)
link

Comments

Nice! But that still leaves some unnecessary parenthesis.

Alister gravatar imageAlister ( 13 years ago )

Does your infix operator not have the parentheses? The parentheses are showing exactly what you typed, using order of operations, using a *binary* addition operator.

Jason Grout gravatar imageJason Grout ( 13 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 13 years ago

Seen: 518 times

Last updated: Nov 10 '11