Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   The Demise of lambda, map, filter and reduce (http://www.programmingforums.org/showthread.php?t=8140)

Dietrich Jan 27th, 2006 12:56 PM

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?

Arevos Jan 27th, 2006 4:31 PM

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".

Cerulean Jan 27th, 2006 4:58 PM

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.

Dietrich Jan 28th, 2006 11:40 AM

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.

Arevos Jan 28th, 2006 12:59 PM

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


Ooble Jan 28th, 2006 1:06 PM

If you need to do that, you should restructure your program so you're not passing function pointers when you don't have to.

Dietrich Feb 2nd, 2006 6:59 PM

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!"
"""


Arevos Feb 3rd, 2006 7:19 AM

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.

Cerulean Feb 3rd, 2006 12:17 PM

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

Klipt Feb 3rd, 2006 2:08 PM

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)


All times are GMT -5. The time now is 12:31 PM.

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