From: Xah Lee
Subject: Re: write a loopin one line; process file paths
Date: 
Message-ID: <1129708210.963305.43270@g49g2000cwa.googlegroups.com>
······@gmail.com wrote:
> it will be added in 2.5 I beleve. At the moment, you can:
>
> predicate and <true expression> or <false expression>

Ah, i see. Here's the full code again:

# -*- coding: utf-8 -*-
# python

import re, os.path

imgPaths=[u'/Users/t/t4/oh/DSCN2059m-s.jpg',
u'/Users/t/t4/oh/DSCN2062m-s.jpg', u'/Users/t/t4/oh/DSCN2097m-s.jpg',
u'/Users/t/t4/oh/DSCN2099m-s.jpg', u'/Users/t/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'.

imgPaths2=[]
for myPath in imgPaths:
    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
    imgPaths2.append(p)

imgPaths3 = map( lambda x: os.path.exists(x[1]) and x[1] or x[0],
map(lambda x: (x, re.sub( r"^(.+?)-s(\.[^.]+)$",r"\1\2", x)),
imgPaths))

print imgPaths2 == imgPaths3

------------------

> A few things I don't like about python for FP is that it relies heavily
> on exception which make these one liners not possible in many case.
> ...

yeah i know how it feels. I haven't had much experience with Python
yet... but doing it in Perl gets all twisted. (especially when one
tries to use trees as data structure (nested lists) in Perl)

besides syntactical issues, another thing with doing functional
programing in imperative languages is that they become very inefficent.
Functional languages are optimized by various means of functional
programings styles. Doing them in imperative languages usually comes
with a order of execution penalty because imperative language compilers
are usually dumb.

though, one can't totally blame for Python's lack of ability to do
functional programing. Its language's syntax of using indentations for
blocks by design, pretty much is in odds with functional programing's
sequencing of functions and other ways. (such as generic mechanism for
prefix/postfix syntax, or macros or other transformations and patterns,
or heavy reliance on the free flow of expressions/values)

I don't blame Python that it doesn't cater to functional programing BY
DESIGN. But i do hate the mother fucking fuckheads Pythoners for one
thing that they don't know what functional programing really is, and on
the other hand fucking trumpet their righteousness and lies thru their
teeth of their ignorance. (that Guido guy with his Python 3000 article
on his blog is one example, possibly forgivable in that particular
instance. (http://xahlee.org/perl-python/python_3000.html))

(excuse me for lashing out)

 Xah
 ···@xahlee.org
∑ http://xahlee.org/

> like :
> >>> os.paths.exists(something) and "print it is there" or "file not found"
>
> I am practicing FP in python and it is in general doable with the help
> of itertools and add the needed functions as needed, like scanl/scanl1
> etc.
>
> A few things I don't like about python for FP is that it relies heavily
> on exception which make these one liners not possible in many case.
>
> Example:
>
> a=[]
> a[0] would raise exception
>
> I usually prefer None as a special value which can be handled in normal
> flow(filter out, may be "x is None and something or otherthing"). But
> the liberal use of Exception force you to structure the program either
> in a very odd way(test with hasattr/haskey etc. before use) or needs
> lots of try/except blocks. I am using a python XHTML template Kid which
> relies on one-liner and found it very difficult.
>
> However, if you don't really need one-liner but just FP style to
> shorten the program, it is fine but I just find the try/except block
> quite ugly.
>
> Then there is the side effect of iterators/generators which unless you
> know can burn you badly(especially when you are chaining these objects
> in these multiple map/filter calls), as mentioned in another post.
>

From: ······@gmail.com
Subject: Re: write a loopin one line; process file paths
Date: 
Message-ID: <1129710240.173768.151950@g14g2000cwa.googlegroups.com>
Xah Lee wrote:
> besides syntactical issues, another thing with doing functional
> programing in imperative languages is that they become very inefficent.
> Functional languages are optimized by various means of functional
> programings styles. Doing them in imperative languages usually comes
> with a order of execution penalty because imperative language compilers
> are usually dumb.

The itertools module and the new generator feature tries to make this
better as it is lazily evaluated, in a sense. But its side effect is
nasty. Usable though need to twist my mind a bit, not like when I do it
in haskell. But the occasion imperative dip makes some of my programs
easier to code.

>
> though, one can't totally blame for Python's lack of ability to do
> functional programing. Its language's syntax of using indentations for
> blocks by design, pretty much is in odds with functional programing's
> sequencing of functions and other ways. (such as generic mechanism for
> prefix/postfix syntax, or macros or other transformations and patterns,
> or heavy reliance on the free flow of expressions/values)

That I am not sure. haskell also use this indentation but I don't see a
problem about it. One thing I find it odd though is the @decorator
construct. I just don't understand why it is viewed as simple. It
becomes very ugly IMO when there is a number of it, I am doing some web
apps which requres varies decorators(one over the other).

@format
@login_block
@validate
@something
def my_func():

I find it easier to read and under stand in the form of

format(login_block(validate(something(my_func))))) though haskell's $
and . is even better.

format.login_block.validate.somethin.my_func

>
> I don't blame Python that it doesn't cater to functional programing BY
> DESIGN. But i do hate the mother fucking fuckheads Pythoners for one
> thing that they don't know what functional programing really is, and on
> the other hand fucking trumpet their righteousness and lies thru their
> teeth of their ignorance. (that Guido guy with his Python 3000 article
> on his blog is one example, possibly forgivable in that particular
> instance. (http://xahlee.org/perl-python/python_3000.html))

That seems to be the case, I mean the design. For fairness, list
comphrehension sometimes looks cleaner, though becomes ugly when you do
the pipe/chain like programming in FP(multiple for). This again is an
odd choice. It seems to be a flattening of multiple line for loop, yet
at the same time python seems to be moving towards being more
explicit(no map/filter/etc as they can be done in for loop).

But I believe Python is designed for easy to code and read and maintain
in mind. One has to admit that without some training, FP is not very
intuitive, my head spin when I see haskell code. A for loop is easier
to understand.

Well, if you want clean FP, you can always try haskell which is getting
better and better in terms of real world module support(file system,
network etc).
>
> (excuse me for lashing out)
> 
>  Xah
>  ···@xahlee.org
> ∑ http://xahlee.org/
From: Xah Lee
Subject: Re: write a loopin one line; process file paths
Date: 
Message-ID: <1129715281.505043.60640@o13g2000cwo.googlegroups.com>
Thanks a lot for various notes. Bonono?

I will have to look at the itertools module. Just went to the doc
http://www.python.org/doc/2.4.1/lib/module-itertools.html
looks interesting.

> But I believe Python is designed for easy to code and read and maintain
> in mind.

> One has to admit that without some training, FP is not very
> intuitive, my head spin when I see haskell code. A for loop is easier
> to understand.

This i'm not sure. Of the past couple of years i increasingly developed
a theory (probably well-known among proper experts), that the
difficulty of human feats of various forms, are primarily a perception
and familiarity thing. This may be getting off topic, but i wrote an
essay expresising much of the idea using Juggling as a example:
Difficulty Perceptions in Human Feats
 http://xahlee.org/Periodic_dosage_dir/t2/juggling.html

likewise, i think this applies to mental feats as well. In particular,
i think that whether imperative code or functional code is easier for
the mind is almost ENTIRELY dependent on which one the person is more
familiar with, coulped with a innate attitude one may have picked up.

> Well, if you want clean FP, you can always try haskell which is getting
> better and better in terms of real world module support(file system,
> network etc).

oh Haskell, my love! I am really going to learn it now. (maybe i'll
start A-Java-Haskell-A-Day) This month i just learned and read about
how Perl 6 is implemented in Haskell! (because one Taiwaness hacker
single-handedly by happenstance tried to do it, as a by-product of
learning Haskell) This Pugs (Perl6 in Haskell) really brought two
rather incompatible communities together somewhat for mutual exchange.
(the learning, on the surface, is politely said to be mutual, but i'm
pretty sure it's mostly Perlers learning from the Haskell folks)

... there is a sentiment among the elite tech-geeking morons, early on
imbued by the concept of troll, so that they in general don't
communicate and learn from any other language except their own.
Anything cross-posted is considered as troll, and the inter-language
communication has been essentially completely cut off. Basically, the
only ones generating all the garbage posts are these troll-criers
themselves. (will have to flesh out on this particular point of
net-sociology in a essay some other day.)

 Xah
 ···@xahlee.org
∑ http://xahlee.org/
From: ······@gmail.com
Subject: Re: write a loopin one line; process file paths
Date: 
Message-ID: <1129716553.375057.62940@g14g2000cwa.googlegroups.com>
Xah Lee wrote:
> This i'm not sure. Of the past couple of years i increasingly developed
> a theory (probably well-known among proper experts), that the
> difficulty of human feats of various forms, are primarily a perception
> and familiarity thing. This may be getting off topic, but i wrote an
> essay expresising much of the idea using Juggling as a example:
> Difficulty Perceptions in Human Feats
>  http://xahlee.org/Periodic_dosage_dir/t2/juggling.html
>
> likewise, i think this applies to mental feats as well. In particular,
> i think that whether imperative code or functional code is easier for
> the mind is almost ENTIRELY dependent on which one the person is more
> familiar with, coulped with a innate attitude one may have picked up.

But most of us start learning programming with imperative language, I
started with COBOL and Pascal(are they still taught). Then we would hit
the problem of looping one way or another pretty soon. Without a doubt,
some experienced programmer would tell you to use for loop or search
for one. This is even true in two heavily used declarative tools, SQL
and Excel. If there is foldl/scanl/map in SQL, I think it would be much
easier to code. Likewise for Excel, it is very functional(like its
ancestors 1-2-3) then VBA was thrown in.

In fact, I think haskell should be taught in any CS course as it opens
up a completely different way of approaching problem and it is easier
to read than LISP.

> oh Haskell, my love! I am really going to learn it now. (maybe i'll
> start A-Java-Haskell-A-Day) This month i just learned and read about
> how Perl 6 is implemented in Haskell! (because one Taiwaness hacker
> single-handedly by happenstance tried to do it, as a by-product of
> learning Haskell) This Pugs (Perl6 in Haskell) really brought two
> rather incompatible communities together somewhat for mutual exchange.
> (the learning, on the surface, is politely said to be mutual, but i'm
> pretty sure it's mostly Perlers learning from the Haskell folks)
After seeing Haskell, I don't think I would go back to Perl(which I
like better than python but python has the momentum as a general
purpose language). That is why I am doing the think in haskell, code in
python, whenever possible.

>
> ... there is a sentiment among the elite tech-geeking morons, early on
> imbued by the concept of troll, so that they in general don't
> communicate and learn from any other language except their own.
> Anything cross-posted is considered as troll, and the inter-language
> communication has been essentially completely cut off. Basically, the
> only ones generating all the garbage posts are these troll-criers
> themselves. (will have to flesh out on this particular point of
> net-sociology in a essay some other day.)
>
May be you can tone down a bit if you want a constructive discussion. I
am too old to have feelings about opinionated posts(and insensitive to)
so I can go through the technical stuff. Confucius said "I can always
learn something whenever there is 3 people getting together", lousy
translation :-)
From: John Thingstad
Subject: Re: write a loopin one line; process file paths
Date: 
Message-ID: <op.sy5kfeckpqzri1@mjolner.upc.no>
On Wed, 19 Oct 2005 11:48:01 +0200, Xah Lee <···@xahlee.org> wrote:

> Thanks a lot for various notes. Bonono?
>
> I will have to look at the itertools module. Just went to the doc
> http://www.python.org/doc/2.4.1/lib/module-itertools.html
> looks interesting.
>
>> But I believe Python is designed for easy to code and read and maintain
>> in mind.
>
>> One has to admit that without some training, FP is not very
>> intuitive, my head spin when I see haskell code. A for loop is easier
>> to understand.
>
> This i'm not sure. Of the past couple of years i increasingly developed
> a theory (probably well-known among proper experts), that the
> difficulty of human feats of various forms, are primarily a perception
> and familiarity thing. This may be getting off topic, but i wrote an
> essay expresising much of the idea using Juggling as a example:
> Difficulty Perceptions in Human Feats
>  http://xahlee.org/Periodic_dosage_dir/t2/juggling.html
>
> likewise, i think this applies to mental feats as well. In particular,
> i think that whether imperative code or functional code is easier for
> the mind is almost ENTIRELY dependent on which one the person is more
> familiar with, coulped with a innate attitude one may have picked up.
>
>> Well, if you want clean FP, you can always try haskell which is getting
>> better and better in terms of real world module support(file system,
>> network etc).
>
> oh Haskell, my love! I am really going to learn it now. (maybe i'll
> start A-Java-Haskell-A-Day) This month i just learned and read about
> how Perl 6 is implemented in Haskell! (because one Taiwaness hacker
> single-handedly by happenstance tried to do it, as a by-product of
> learning Haskell) This Pugs (Perl6 in Haskell) really brought two
> rather incompatible communities together somewhat for mutual exchange.
> (the learning, on the surface, is politely said to be mutual, but i'm
> pretty sure it's mostly Perlers learning from the Haskell folks)
>
> ... there is a sentiment among the elite tech-geeking morons, early on
> imbued by the concept of troll, so that they in general don't
> communicate and learn from any other language except their own.
> Anything cross-posted is considered as troll, and the inter-language
> communication has been essentially completely cut off. Basically, the
> only ones generating all the garbage posts are these troll-criers
> themselves. (will have to flesh out on this particular point of
> net-sociology in a essay some other day.)
>
>  Xah
>  ···@xahlee.org
> ∑ http://xahlee.org/
>

Honestly.. "your programming skills suck"

John

-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/