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

Re: Accumulator



Paul Prescod <paul@prescod.net> writes:

> Pixel wrote:
> > ...
> > well, you didn't talk about "reduce". The main pb with list
> > comprehensions is that they are limited to giving lists as a result.
> 
> reduce is used roughly once in the 50,000 lines of code that are the
> Python standard library. It clearly is not pulling its weight. In a
> project I worked on with six different Python programmers, ranging in
> expertise from guru to newbie, nobody used it once.
> 
> "reduce" may be the right thing for Lisp programmers but we have
> concrete proof that it is not the right thing for Python programmers.
> That's why I strongly disagree with Paul G. that Python will grow more
> Lisp-like over time. When it tried that, it failed.

well i was signaling function "partition", looking at python's code, i
found it in pydoc.py named _split_list. So even if python's code is
clearly not functional, some parts are :)

(more about pydoc.py below)


[...]

> Anything that can be done with lambda can be done without it, because
> these two statements are equivalent:
> 
> _ = lambda y: y
> 
> def _(y):
> 	...
> 
> Python's goal is regularity and readability, not succinctness.

well:

also in pydoc.py, multicolumn:
----------------------------------------
def multicolumn(list, format, cols=4):
    result = ''
    rows = (len(list)+cols-1)/cols
    for col in range(cols):
        result = result + '<td width="%d%%" valign=top>' % (100/cols)
        for i in range(rows*col, rows*col+rows):
            if i < len(list):
                result = result + format(list[i]) + '<br>\n'
        result = result + '</td>'
    return '<table width="100%%" summary="list"><tr>%s</tr></table>' % result
----------------------------------------
i'd rather write it:
----------------------------------------
def multicolumn(list, format, cols=4):
    rows = (len(list)+cols-1)/cols
    lines = '\n'.join([ '<td width="%d%%" valign=top>%s</td>' % (100/cols, '<br>\n'.join(map(format, col)))
                        for col in groupBy(list, rows) ])
    return '<table width="100%%" border=1 summary="list"><tr>%s</tr></table>' % lines

# using

def groupBy(list, nb):
    l = []
    while list:
        subl, list = list[0:nb], list[nb:]
        l += [subl]
    return l
----------------------------------------
this is quite dense, but i don't know how to use a local variable to
have it more readable. Note that the result is not exactly the same, a
'<br>\n' is dropped, but I think the output is nicer that way (since
the dropped <br> is unneeded)

I find my version more readable, not bothering with rows*col and
rows*col+rows...


Still in pydoc.py, i've seen a hell lot of things alike:

        children = []
        for file in os.listdir(dir):
            path = os.path.join(dir, file)
            if ispackage(path):
                children.append((path, package + (package and '.') + file))
            else:
                children.append((path, package))
        children.sort() # so that spam.py comes before spam.pyc or spam.pyo
        return children

which would nicely give

        os.listdir(dir).map{|file|
            path = os.path.join(dir, file)
            pkg = ispackage(path) ? (package == "" ? "" : ".") + file : ""
            path, package + pkg
        }.sort

in Ruby (not tested, but should be something like this)