15

Python 3.2 documentation refers to Collin Winter's functional module which contains function compose:

The compose() function implements function composition. In other words, it returns a wrapper around the outer and inner callables, such that the return value from inner is fed directly to outer.

Unfortunately, this module hasn't been updated since July 2006; I wonder if there's any replacement available.

For now, I only need compose function. Is the following original functional.compose definition still good for Python 3?

def compose(func_1, func_2, unpack=False):
    """
    compose(func_1, func_2, unpack=False) -> function

    The function returned by compose is a composition of func_1 and func_2.
    That is, compose(func_1, func_2)(5) == func_1(func_2(5))
    """
    if not callable(func_1):
        raise TypeError("First argument to compose must be callable")
    if not callable(func_2):
        raise TypeError("Second argument to compose must be callable")

    if unpack:
        def composition(*args, **kwargs):
            return func_1(*func_2(*args, **kwargs))
    else:
        def composition(*args, **kwargs):
            return func_1(func_2(*args, **kwargs))
    return composition

This SO question is somewhat related; it asks whether Python should support special syntax for compose.

5
  • Python 3 does ot have the callable built in keyword - it is usually replaced with hasattr(obj, "__call__") otherwise, the code above should work. Commented Jan 23, 2012 at 19:58
  • 3
    callable() was added back to the language in 3.2. Commented Jan 23, 2012 at 20:04
  • I think that should be fine in Python 3.2. As others point out, for Python 3.0 and 3.1, you'd need to implement callable, but if you're happy with 3.2, just copy, paste and credit. Commented Jan 23, 2012 at 20:27
  • Thanks.. Then the only remaining question is whether functional module has a replacement in Python 3. Commented Jan 23, 2012 at 21:22
  • Since I couldn't find a port of functional to Py3, I slapped one together at github. But I ran into a problem with the copyright/license stuff that stalled me. I think it might be easier to reimplement it from scratch. Commented Jan 31, 2013 at 22:23

1 Answer 1

6

Your implementation of compose is valid for python 3.2 as discussed in the comments above. Most of the functions of the library you gave have a python equivalent written in the documentation.

Functions such as map and filter are already implemented in python and can also be simply expressed as list comprehensions. Python has an id function returning the identity of an object (as integer), but the id function of the library can be expressed as lambda x: x.

Another modules you might find interesting are itertools and functools which has partial and reduce (which is similar to foldl but the argument order is not the same).

Here is a simple implementations of a few of them that I didn't find in the standard library:

from functools import reduce

def flip(f):
    if not callable(f):
        raise TypeError("Cannot filp a non-callable object")
    def result(*args, **kw):
        args = list(args)
        args.reverse()
        return f(*args, **kw)
    return result

def ilast(i):
    return reduce(lambda _, x: x, i)

def iscanl(f, v, seq):
    yield v
    for a in seq:
        v = f(v, a)
        yield v

def scanl(*args, **kw):
    return list(iscanl(*args, **kw))

def foldl(*args, **kw):
    return ilast(iscanl(*args, **kw))
# Or using reduce
#def foldl(f, v, seq):
#    return reduce(f, seq, v)

def iscanr_reverse(f, v, seq):
    return iscanl(flip(f), v, seq)

def scanr(*args, **kw):
    result = list(iscanr_reverse(*args, **kw))
    result.reverse()
    return result

def foldr(*args, **kw):
    return ilast(iscanr_reverse(*args, **kw))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.