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

Re: Accumulator

Avi Bryant wrote:
> On Fri, 24 May 2002, Paul Prescod wrote:
> > I've lost the thread of this conversation. I thought that you gave me
> > some Ruby code to convert. I thought that you said that it does not need
> > full continuations but does need "mutable closures" to implement CPS. I
> > showed you how to do mutable closures and, I think, CPS.
> The Ruby code I gave you was in a CPS style; you gave me back Python code
> that wasn't in CPS, and asked why it needed to be in CPS.  I was trying to
> answer that question.

Sorry, now I'm back on the same page. Here is the code in (AFAIK) full
CPS. The primary difference between this and the original Ruby CPS is
that I define functions by name rather than using anonymous functions. I
don't really consider either the Ruby version or the Python version easy
to read but I would asy the Ruby has a slight edge in that department.

def compute_price():
    def add_tax_if_necessary(price):
       context = Struct()
       context.price = price
       if price > tax_threshold:
            def add_tax(tax):
                context.price += tax
            get_tax(price, add_tax)


# this helper class would be in a shared module
class Struct:

def get_base_price(next_thing_to_do): 
    next_thing_to_do(input("price: "))

def get_tax(price, next_thing_to_do):

tax_threshold = 100

def do_something_with(x):
    print x

while 1:

> How many of Python's modules and libraries will run on Stackless?  If all
> or most of them, why isn't it more generally used?

If there is no reason to switch from Guido-maintained or
commercially-maintained distributions then programmers will not make the
effort. Most of us don't see enough benefit in stackless.

Also, consider the network effects. You develop a module that depends on
stackless and then decide you want to share it with others. The others
don't have stackless. Also, newer versions of stackless uses a different
implementation technique that depends on processor knowledge which
reduces portability.

 Paul Prescod