[Python-Dev] functools additions

2007-04-15 Thread SevenInchBread

So if it's alright with the privledged folk - I'd like to commit these
minor (and probably non-controversial) additions to the functools module.



def cat(x): return x

def multimap(func, s, n=2):
   assert n > 0, "n must be positive"
   return (map(func, seq)
   if n == 1 else
   map(lambda x: multimap(func, x, n-1),
   seq))

def multifilter(func, s, n=2):
   return multimap(lambda x: filter(func, x), s, n-1)

def multireduce(func, s, n=2):
   return multimap(lambda x: reduce(func, x), s, n-1)


class nullfunc(object):
   def __call__(self, *a, **k): return self
   def __getattr(self, name): return getattr(None, name)



cat is a generic identity function - useful for some higher-order functions
to specify a function that "does nothing". multimap, multifilter, and
multireduce, are all multi-dimensional versions of map, filter, and reduce.
nullfunc is a special callable object that emulates the failed callable None
proposal - which wasn't really a good idea, but would have been more useful
as a different object apart from None.

you could probably even put cat in __builtins__ - so you don't need to waste
effort importing such a trivial function.

--.
"What's money? A man is a success if he gets up in the morning and goes to
bed at night and in between does what he wants to do." ~ Bob Dylan
___
Python-Dev mailing list
[EMAIL PROTECTED]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] functools additions

2007-04-15 Thread Martin v. Löwis
> So if it's alright with the privledged folk - I'd like to commit
> these minor (and probably non-controversial) additions to the functools
> module.

Do you have commit access? What's your real name?

-1 on these additions. If lambda x:x would be added, it should be named
"identity", not "cat" (if that name is meant to raise associations with
the Unix cat(1) utility, please understand that "cat" is short for
"concatenate").

Regards,
Martin
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] functools additions

2007-04-15 Thread SevenInchBread

Do you have commit access? What's your real name?

I prefer to remain pseudonymous, and I don't have commit access.

Yeah... they're not terribly useful - more or less there for the sake of
being there. Batteries included and all that


...but now I've got a more useful idea for a function wrapper around
predicate functions to make them a little more functionally inclined.
Something like...

@predicate
def hasnext(x): return hasattr(x, "next")

@predicate
def hasiter(x): return hasattr(x, "__iter__")

isiterable   = hasnext or hasiter   #or/and/not operators construct new
predicate functions

isEmptyIterable = isiterable and not predicate(bool)

isgenerator = isiterable and (lambda x: hasattr(x, "send") and hasattr(x,
"throw"))

filter(isgenerator or callable, SomeSequence)

--
"What's money? A man is a success if he gets up in the morning and goes to
bed at night and in between does what he wants to do." ~ Bob Dylan
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] functools additions

2007-04-15 Thread BJörn Lindqvist
> def cat(x): return x
>
> def multimap(func, s, n=2):
> assert n > 0, "n must be positive"
> return (map(func, seq)
> if n == 1 else
> map(lambda x: multimap(func, x, n-1),
> seq))
>
> def multifilter(func, s, n=2):
> return multimap(lambda x: filter(func, x), s, n-1)
>
> def multireduce(func, s, n=2):
> return multimap(lambda x: reduce(func, x), s, n-1)
>
> class nullfunc(object):
> def __call__(self, *a, **k): return self
> def __getattr(self, name): return getattr(None, name)

Could you describe what these functions do? Preferably with examples
that demonstrates that they are useful.

-- 
mvh Björn
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] functools additions

2007-04-15 Thread Jean-Paul Calderone
On Sun, 15 Apr 2007 18:18:16 -0400, SevenInchBread <[EMAIL PROTECTED]> wrote:
>>>Do you have commit access? What's your real name?
>I prefer to remain pseudonymous, and I don't have commit access.
>
>Yeah... they're not terribly useful - more or less there for the sake of
>being there. Batteries included and all that
>

Please discuss this on the python-ideas list before bringing it up on
python-dev.

Jean-Paul
___
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com