![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
But, on the other hand:
[i * i for i in l if i % 2 == 0] map(lambda x : x * x, filter(lambda x : x % 2 == 0, l)) ![]() |
|
|
|
|
|
#12 | |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
Quote:
![]() 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 |
|
|
|
|
|
|
#13 | |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
Yes, yes, I should say what I mean... :p
Quote:
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 ![]() |
|
|
|
|
|
|
#14 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
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)] |
|
|
|
|
|
#15 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
import textwrap
para = "\n\n"
print para.join(textwrap.fill(elem, 40)
for elem in msg.split(para))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) |
|
|
|
|
|
|
#16 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#17 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#18 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
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)[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] |
|
|
|
|
|
|
#19 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#20 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|