Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 3rd, 2006, 3:46 PM   #11
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
But, on the other hand:
[i * i for i in l if i % 2 == 0]
is more pleasant than
map(lambda x : x * x, filter(lambda x : x % 2 == 0, l))
Not that it's hard to create map and filter functions yourself, if you really want to
Arevos is offline   Reply With Quote
Old Feb 4th, 2006, 8:14 AM   #12
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Quote:
Originally Posted by Klipt
You mean a ternary operator? Finally! The and-or hack doesn't quite cut it :p

Certainly list comprehensions can replace map and filter, but I personally think
[i for i in l if p(i)]
is less pleasant than
map(p, l)
You mean filter(p, l)
Annoying thing about list/gen comps is the fact that you have to write out the var name three times for simple comparisons of the form
x for x in foo if x
There are discussions and requests for a shortcut, and personally I think one would be an excellent addition.
Cerulean is offline   Reply With Quote
Old Feb 4th, 2006, 8:46 AM   #13
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
Yes, yes, I should say what I mean... :p

Quote:
Originally Posted by Arevos
But, on the other hand:
[i * i for i in l if i % 2 == 0]
is more pleasant than
map(lambda x : x * x, filter(lambda x : x % 2 == 0, l))
I think list comprehensions are a great idea. But filter and map did have specific uses, and if they get rid of them I hope they at least work in a shortcut for filter so you don't have to write the variable name 3 times, as Cerulean mentioned.

In general I agree with Python's 'there should be one (obvious) way to do it' philosophy, but if you forget the 'obvious' part and get really extreme you could end up getting rid of for loops (while can 'do the same thing') and if statements (while with an immediate break can do the same thing ... although if you did both you'd need to specify the break level...) - and that would be awful
Klipt is offline   Reply With Quote
Old Feb 4th, 2006, 9:10 AM   #14
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4 Arevos is on a distinguished road
Well, even if they disappear, if you find yourself needing them, it's not hard to write them in:
def map(f, s):
    return [f(x) for x in s]
def filter(f, s):
    return [x for x in s if f(s)]
Arevos is offline   Reply With Quote
Old Feb 5th, 2006, 5:30 AM   #15
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Dietrich
I found this sweet little code on the web somewhere. It contains both reduce() and lambda. Any idea how to rewrite this without those two functions?
import textwrap
para = "\n\n"

print para.join(textwrap.fill(elem, 40)
                      for elem in msg.split(para))
Admittedly textwrap uses filter and map, but we can assume that someone will fix it by Py3.0 as part of the general library cleanup. ;-)

The code rewritten without lambda or reduce is:
def wrap(text, width):
    """
    A word-wrap function that preserves existing line breaks
    and most spaces in the text. Expects that existing line
    breaks are linux style newlines (\n).
    """
    def func(line, word):
        nextword = word.split("\n", 1)[0]
        n = len(line) - line.rfind('\n') - 1 + len(nextword)
        if n >= width:
            sep = "\n"
        else:
            sep = " "
        return '%s%s%s' % (line, sep, word)
    text = text.split(" ")
    while len(text) > 1:
        text[0] = func(text.pop(0), text[0])
    return text[0]
print wrap(msg, 40)
-T.
hydroxide is offline   Reply With Quote
Old Feb 5th, 2006, 10:10 AM   #16
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thumbs up

Great coding hydroxide! A fine example of a generator expression and the textwrap module. I didn't even know textwrap existed.

Why did you use "\n\n" rather than "\n" for the separator?

Thanks
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Feb 5th, 2006, 10:31 AM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I believe the separator is a single newline (sep = "\n"); paragraphs (para) are often defined by a double newline.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 8th, 2006, 5:30 AM   #18
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Dietrich
hydroxide mentioned this blog from Guido himself:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
... and you won't believe this, but... http://mail.python.org/pipermail/pyt...ry/060415.html

I suspect that this is NOT the end of the story.

I wouldn't mind anonymous functions moving to a comprehension-like syntax [I'm using "with" because it's environmentally friendly and sort of fits =]:
anon = foo(x, y*z) with x, y, z
anon(a, b, c)

z = 4
curried_anon = foo(x, y*z) with x, y

x = [(3,2), (2,4), (1,1)]
for elem in sorted(x, key=elem[1] with elem):
    ...

myobj.callback(button.click() with None)
-T.
[Oh yeah, DaWei's right about "\n\n" being a paragraph separator - textwrap treats the text it's fed as being a single paragraph - you might in theory have a double-spaced textfile, or summat]
hydroxide is offline   Reply With Quote
Old Feb 9th, 2006, 3:21 PM   #19
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Thanks hydroxide! Almost looks like lambda will stay, at least if our wise BDFL has his way. What other computer language did Python borrow lambda from?

'With' has a ring to it!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Feb 9th, 2006, 3:42 PM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
'With' has a ring to it!
Perish the thought.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC