write a loopin one line; process file paths

Xah Lee xah at xahlee.org
Wed Oct 19 00:47:16 CEST 2005


Xah Lee wrote:
> is there a way to condense the following loop into one line?
>
> # -*- coding: utf-8 -*-
> # python
>
> import re, os.path
>
> imgPaths=[u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2059m-s.jpg',
> u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2062m-s.jpg',
> u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2097m-s.jpg',
> u'/Users/t/web/Periodic_dosage_dir/lanci/t4/oh/DSCN2099m-s.jpg',
> u'/Users/t/web/Icons_dir/icon_sum.gif']
>
> # change the image path to the full sized image, if it exists
> # that is, if image ends in -s.jpg, find one without the '-s'.
> temp=imgPaths[:]
> imgPaths=[]
> for myPath in temp:
>     p=myPath
>     (dirName, fileName) = os.path.split(myPath)
>     (fileBaseName, fileExtension)=os.path.splitext(fileName)
>     if(re.search(r'-s$',fileBaseName,re.U)):
>         p2=os.path.join(dirName,fileBaseName[0:-2]) + fileExtension
>         if os.path.exists(p2): p=p2
>     imgPaths.append(p)
>
> temp=[]
> print imgPaths

this is of interest to me, because i wanted to illustrate functional
programing, and Python's short-coming of it. (if you go to wikipedia
and read the article on Python, it will proudly say that Python
supports functional programing. A huge fucking lie. (more fantastic
fucking lies in the Perl article. And it is impossible to correct them,
because there is an army of ignorant morons, usually militant too, not
shy to do justice for humanity with heartiness.) )

In functional programing proper, the following loop for example would
be a single line. Why? because in fp, one is focused on the input &
output and less on the details how it is done. So here, one simply
reads the comment there and that'd be it. The one-line code would be in
the form of impPaths = f(impPaths), where f is a function made on the
spot.

So, in fp the source code tends to take the form of paragraphs of
algorithmic units, as opposed one million lines of code filling the
page.

I would have, written the following in a one-line. But in Python this
is impossible. (if possible, probably something so unusual and
unreadable)

... Here we may want to make a note on what does it mean to say that a
programing language supports functional programing. The FP style,
basically means that one writes pure functions and apply them to
expressions. That is, subroutines whose behavior are purely that of
input and output. (i.e. so-called without side-effects) But if one
programs in FP style, certain patterns of coding emerges. For example,
a lambda construct, sequencing of functions, assigning and moving of
functions, application of functions etc, and such languages naturally
developed many features in that direction such as types, macros, many
specialized loop constructs (recursion, nesting) and many other
features that goes by abstruse names...etc.

so here, if we judge Python (or Perl), why should we say it supports
functional programing? Simply because it is possible to define
subroutines without side-effects? What else, really, functional
language features does Python support? Can we really say, that a
functional programing coming to Python will find it reasonably supports
his FP coding style?

I would like to see the “also support functional programing”
removed in the wikipedia Python article.
( http://en.wikipedia.org/wiki/Python_programming_language )

(Perl the language, can arguably be said to support some functional
programing. (those Schwatchzian etc Transform for example, is a epitome
of functional programing.) However, Perl does not support
Object-Oriented programing.)

The Perl article is filled with more egregious lies. (In the past year)
I've tried to mend the article about every 3 months or so, tentatively
on obvious and technical and non-controversial items that's purely
propaganda but got denied by the Perl fuckfaces. I have written a full
page of criticism but the human animal fuckers handily censored it too.
If you think i have a point, please add this link to the Perl page's
external links.
http://xahlee.org/UnixResource_dir/perlr.html (book review and
criticism on Perl & community)

 Xah
 xah at xahlee.orghttp://xahlee.org/




More information about the Python-list mailing list