[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accumulator

On Thu, 23 May 2002, Paul Prescod wrote:

> I'm going to avoid deeply nested expressions, but I won't add any return
> statements. I could use Python closures and CPS-ish style, but I don't
> even see a need for them, unless I've misunderstood something big.

The thing that makes continuations interesting here, which you may indeed
be misunderstanding, is how you implement this part in web context:

> def get_base_price():
>     return input("price: ")

particularly if you also assume something like

def get_tax(price):
     return state_tax(input("state or province: "))

And then especially if you worry about the fact that someone might hit the
back button, and choose a different base price after all.

If you can implement those two methods in a web app without using
continuations, such that the algorithm below can still be expressed that
simply, I'd be interested.

> Here's the algorithm:
> def add_tax_if_necessary(context):
>    if context.price > tax_threshold:
>        add_tax(context)
> def add_tax(context):
>     context.price += get_tax(context.price)
> def compute_price():
>    context = Struct(price = get_base_price())
>    add_tax_if_necessary(context)
>    do_something_with(context.price)
> The algorithm makes no use of any unusual Python features. I prefer this
> unrolled functional version because in a web server I could associate
> each function with a page using a simple dictionary. The context object
> could use whatever is available in my app server, like the .NET
> framework or J2EE. The app server would make sure that the context was
> available on whatever machine a request came on in. (Zope may have this
> capability, not sure)