Pointfree function combination in Python

Question:

I have some predicates, e.g.:

is_divisible_by_13 = lambda i: i % 13 == 0
is_palindrome = lambda x: str(x) == str(x)[::-1]

and want to logically combine them as in:

filter(lambda x: is_divisible_by_13(x) and is_palindrome(x), range(1000,10000))

The question is now: Can such combination be written in a pointfree style, such as:

filter(is_divisible_by_13 and is_palindrome, range(1000,10000))

This has of course not the desired effect because the truth value of lambda functions is True and and and or are short-circuiting operators. The closest thing I came up with was to define a class P which is a simple predicate container that implements __call__() and has the methods and_() and or_() to combine predicates. The definition of P is as follows:

import copy

class P(object):
    def __init__(self, predicate):
        self.pred = predicate

    def __call__(self, obj):
        return self.pred(obj)

    def __copy_pred(self):
        return copy.copy(self.pred)

    def and_(self, predicate):
        pred = self.__copy_pred()
        self.pred = lambda x: pred(x) and predicate(x)
        return self

    def or_(self, predicate):
        pred = self.__copy_pred()
        self.pred = lambda x: pred(x) or predicate(x)
        return self

With P I can now create a new predicate that is a combination of predicates like this:

P(is_divisible_by_13).and_(is_palindrome)

which is equivalent to the above lambda function. This comes closer to what I’d like to have, but it is also not pointfree (the points are now the predicates itself instead of their arguments). Now the second question is: Is there a better or shorter way (maybe without parentheses and dots) to combine predicates in Python than using classes like P and without using (lambda) functions?

Asked By: Frank S. Thomas

||

Answers:

You can override the & (bitwise AND) operator in Python by adding an __and__ method to the P class. You could then write something like:

P(is_divisible_by_13) & P(is_palindrome)

or even

P(is_divisible_by_13) & is_palindrome

Similarly, you can override the | (bitwise OR) operator by adding an __or__ method and the ~ (bitwise negation) operator by adding a __not__ method. Note that you cannot override the built-in and, or and not operator, so this is probably as close to your goal as possible. You still need to have a P instance as the leftmost argument.

For sake of completeness, you may also override the in-place variants (__iand__, __ior__) and the right-side variants (__rand__, __ror__) of these operators.

Code example (untested, feel free to correct):

class P(object):
    def __init__(self, predicate):
        self.pred = predicate

    def __call__(self, obj):
        return self.pred(obj)

    def __copy_pred(self):
        return copy.copy(self.pred)

    def __and__(self, predicate):
        def func(obj):
            return self.pred(obj) and predicate(obj)
        return P(func)

    def __or__(self, predicate):
        def func(obj):
            return self.pred(obj) or predicate(obj)
        return P(func)

One more trick to bring you closer to point-free nirvana is the following decorator:

from functools import update_wrapper

def predicate(func):
    """Decorator that constructs a predicate (``P``) instance from
    the given function."""
    result = P(func)
    update_wrapper(result, func)
    return result

You can then tag your predicates with the predicate decorator to make them an instance of P automatically:

@predicate
def is_divisible_by_13(number):
    return number % 13 == 0

@predicate
def is_palindrome(number):
    return str(number) == str(number)[::-1]

>>> pred = (is_divisible_by_13 & is_palindrome)
>>> print [x for x in xrange(1, 1000) if pred(x)]
[494, 585, 676, 767, 858, 949]
Answered By: Tamás

Basically, your approach seems to be the only feasible one in Python. There’s a python module on github using roughly the same mechanism to implement point-free function composition.

I have not used it, but at a first glance his solution looks a bit nicer (because he uses decorators and operator overloading where you use a class and __call__).

But other than that it’s not technically point-free code, it’s just “point-hidden” if you will. Which may or may not be enough for you.

Answered By: rwos

You could use the Infix operator recipe:

AND = Infix(lambda f, g: (lambda x: f(x) and g(x)))
for n in filter(is_divisible_by_13 |AND| is_palindrome, range(1000,10000)):
    print(n)

yields

1001
2002
3003
4004
5005
6006
7007
8008
9009
Answered By: Reinstate Monica

That would be my solution:

class Chainable(object):

    def __init__(self, function):
        self.function = function

    def __call__(self, *args, **kwargs):
        return self.function(*args, **kwargs)

    def __and__(self, other):
        return Chainable( lambda *args, **kwargs:
                                 self.function(*args, **kwargs)
                                 and other(*args, **kwargs) )

    def __or__(self, other):
        return Chainable( lambda *args, **kwargs:
                                 self.function(*args, **kwargs)
                                 or other(*args, **kwargs) )

def is_divisible_by_13(x):
    return x % 13 == 0

def is_palindrome(x):
    return str(x) == str(x)[::-1]

filtered = filter( Chainable(is_divisible_by_13) & is_palindrome,
                   range(0, 100000) )

i = 0
for e in filtered:
    print str(e).rjust(7),
    if i % 10 == 9:
        print
    i += 1

And this is my result:

    0     494     585     676     767     858     949    1001    2002    3003
 4004    5005    6006    7007    8008    9009   10101   11011   15951   16861
17771   18681   19591   20202   21112   22022   26962   27872   28782   29692
30303   31213   32123   33033   37973   38883   39793   40404   41314   42224
43134   44044   48984   49894   50505   51415   52325   53235   54145   55055
59995   60606   61516   62426   63336   64246   65156   66066   70707   71617
72527   73437   74347   75257   76167   77077   80808   81718   82628   83538
84448   85358   86268   87178   88088   90909   91819   92729   93639   94549
95459   96369   97279   98189   99099
Answered By: Niklas R

Python already has a way of combining two functions: lambda. You can easily make your own compose and multiple compose functions:

compose2 = lambda f,g: lambda x: f(g(x))
compose = lambda *ff: reduce(ff,compose2)

filter(compose(is_divisible_by_13, is_palindrome, xrange(1000)))
Answered By: Marcin

Here’s a solution based on the Ramda.js library which has the allPass, anyPass, and complement combinators especially for this purpose. Here are those functions implemented in Python 3.10:

from typing import Callable, TypeVar

T = TypeVar('T')


def any_pass(predicates: list[Callable[[T], bool]]) -> Callable[[T], bool]:
    def inner(value: T) -> bool:
        for predicate in predicates:
            if predicate(value):
                return True
        return False
    return inner


def all_pass(predicates: list[Callable[[T], bool]]) -> Callable[[T], bool]:
    def inner(value: T) -> bool:
        for predicate in predicates:
            if not predicate(value):
                return False
        return True
    return inner


def complement(predicate: Callable[[T], bool]) -> Callable[[T], bool]:
    def inner(value: T) -> bool:
        return not predicate(value)
    return inner


if __name__ == '__main__':

    def is_divisible_by_13(n: int) -> bool:
        return n % 13 == 0

    def is_palindrome(x: int) -> bool:
        return str(x) == str(x)[::-1]

    nums = list(range(1000, 10000))
    assert (list(filter(all_pass([is_divisible_by_13, is_palindrome]), nums)) 
            == [1001, 2002, 3003, 4004, 5005, 6006, 7007, 8008, 9009])

    assert (list(filter(any_pass([is_divisible_by_13, is_palindrome]), nums))[:9] 
            == [1001, 1014, 1027, 1040, 1053, 1066, 1079, 1092, 1105])

    assert (list(filter(complement(is_palindrome), nums))[:9]
            == [1000, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009])

Answered By: Oleg Alexander