Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jan 27th, 2006, 1:56 PM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile The Demise of lambda, map, filter and reduce

hydroxide mentioned this blog from Guido himself:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196

Very interesting reading material. Is anyone going to miss these functions?

Also, does anyone have additional info on the upcoming Python 3.0?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 27th, 2006, 5:31 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
The functionality of filter and map is the same as list comprehensions, so I don't think there's much loss there. I've never found a use for reduce in any program I've devised, and lambda is rather un-pythonic.

This said, I wish Python had some syntactic sugar for:
def anon(...):
    # code
function(anon)
Perhaps something like:
keyword function:
   # code
Where 'keyword could be something like "do" or "use".
Arevos is offline   Reply With Quote
Old Jan 27th, 2006, 5:58 PM   #3
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
Where `function(anon)` does what exactly?

I like map for the speedups you can get by pushing the loop into C. Other than for that, it's not of much use now the list comps are around and more readable. Same goes for the other functions for me really.
Cerulean is offline   Reply With Quote
Old Jan 28th, 2006, 12:40 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Talking about syntactic sugar, a save and intelligent input for numbers would be nice. Something that accepts only a numeric value, and does not allow some clown to erase your hard disk.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jan 28th, 2006, 1:59 PM   #5
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by Cerulean
Where `function(anon)` does what exactly?
Uh, well, 'function' is a function, and 'anon' is the inline function defined in my example. For instance:
def print_output(func):
    print func(10)

def increment(x):
    return x + 1

print_output(increment)
Could have some syntactic sugar that would allow it to be re-written:
def print_output(func):
    print func(10)

use x in print_output:
   return x + 1
Arevos is offline   Reply With Quote
Old Jan 28th, 2006, 2:06 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
If you need to do that, you should restructure your program so you're not passing function pointers when you don't have to.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 2nd, 2006, 7:59 PM   #7
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
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?
# note reduce() and lambda will be dropped with Python3.0

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).
    """
    return reduce(lambda line, word, width=width: '%s%s%s' %
                  (line,
                   ' \n'[(len(line)-line.rfind('\n')-1
                         + len(word.split('\n',1)[0]
                              ) >= width)],
                   word),
                  text.split(' ')
                 )

# 2 very long lines separated by a blank line
msg = """Arthur:  "The Lady of the Lake, her arm clad in the purest \
shimmering samite, held aloft Excalibur from the bosom of the water, \
signifying by Divine Providence that I, Arthur, was to carry \
Excalibur. That is why I am your king!"

Smarty:  "Listen. Strange women lying in ponds distributing swords is \
no basis for a system of government. Supreme executive power derives \
from a mandate from the masses, not from some farcical aquatic \
ceremony!\""""

# example: make it fit in 40 columns
print wrap(msg,40)

# result is below, note Smarty is now S,arty (why?)
"""
Arthur:  "The Lady of the Lake, her arm
clad in the purest shimmering samite,
held aloft Excalibur from the bosom of
the water, signifying by Divine
Providence that I, Arthur, was to carry
Excalibur. That is why I am your king!"

S,arty:  "Listen. Strange women lying in
ponds distributing swords is no basis
for a system of government. Supreme
executive power derives from a mandate
from the masses, not from some farcical
aquatic ceremony!"
"""
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Feb 3rd, 2006, 8:19 AM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Also, does anyone have additional info on the upcoming Python 3.0?
I recently discovered that bidirectional yields are coming in Python 2.5. Mmm... continuations.

Quote:
Originally Posted by Ooble
If you need to do that, you should restructure your program so you're not passing function pointers when you don't have to.
Likewise, one could alter one's carpentry techniques so that drills aren't needed. But doing so would not make the process any easier.

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?
There are longer ways to rewrite it, but I've yet to find anything so concise, even though I've been thinking about it for quite a while.
Arevos is offline   Reply With Quote
Old Feb 3rd, 2006, 1:17 PM   #9
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
That's a pretty sweet function and a concise one at that, but profusely unreadable. A balance really is necessary.
Another addition to (the next release, I believe) of Python is conditional expressions in the form of
x if condition else y
Cerulean is offline   Reply With Quote
Old Feb 3rd, 2006, 3:08 PM   #10
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
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)
Klipt 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 4:06 AM.

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