Pointfree function combination in Python - python

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?

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]

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.

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

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)))

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

Related

How to include identical if block in multiple functions?

I'm sorry to ask such a basic question, but what's the Pythonic way to include the same if block that can conditionally return in multiple functions? Here's my setup:
def a():
if bool:
return 'yeehaw'
return 'a'
def b():
if bool:
return 'yeehaw'
return 'b'
I'd like to factor the common conditional out of the two functions, but I'm not sure how to do so.
Use a decorator or closure
def my_yeehaw(result):
def yeehaw():
if some_bool:
return 'yeehaw'
return result
return yeehaw
a = my_yeehaw('a')
b = my_yeehaw('b')
You could use a lambda that takes in a. bool and a default value to return if the condition is false:
check = lambda condition, default: 'yeehaw' if condition else default
def a():
return check(condition, 'a')
def b():
return check(condition, 'b')
I am new to python but I think you can use a default argument to send a or b based on what is passed to the function.
def a(x='a'):
if condition: #where condition can be True or False
return 'yeehaw'
return x
(note: my naming wasn't the best, consider that same_bool function might be better called identical_if_block(...) to follow your example
And I am also assuming bool_ is a parameter, though it could work as a global. But not as bool which, like any function object, is always Truthy
>>> bool(bool)
True
)
Use a function, as long as it doesn't need to return falsies.
def same_bool(bool_):
" works for any result except a Falsy"
return "yeehaw" if bool_ else None
def a(bool_):
res = same_bool(bool_)
if res:
return res
return 'a'
def b(bool_, same_bool_func):
#you can pass in your boolean chunk function
res = same_bool_func(bool_)
if res:
return res
return 'b'
print ("a(True):", a(True))
print ("a(False):", a(False))
print ("b(True, same_bool):", b(True,same_bool))
print ("b(False, same_bool):", b(False,same_bool))
output:
a(True): yeehaw
a(False): a
b(True, same_bool): yeehaw
b(False, same_bool): b
If you do need falsies, use a special guard value
def same_bool(bool_):
" works for any result"
return False if bool_ else NotImplemented
def a(bool_):
res = same_bool(bool_)
if res is not NotImplemented:
return res
return 'a'
You could also feed in "a" and "b" since they are constant results, but I assume that's only in your simplified example.
def same_bool(bool_, val):
return "yeehaw" if bool_ else val
def a(bool_):
return same_bool(bool_, "a")
I ended up liking the decorator syntax, as the functions that include the duplicative conditional logic have a good deal else going on in them:
# `function` is the decorated function
# `args` & `kwargs` are the inputs to `function`
def yeehaw(function):
def decorated(*args, **kwargs):
if args[0] == 7: return 99 # boolean check
return function(*args, **kwargs)
return decorated
#yeehaw
def shark(x):
return str(x)
shark(7)

Can python decorators be used to change behavior of a function returned by a function?

I have functions, that return validator functions, simple example:
def check_len(n):
return lambda s: len(s) == n
Is it possible to add a decorator, that prints out a message, in case the check evaluates to false?
Something like this:
#log_false_but_how
def check_len(n):
return lambda s: len(s) == n
check_one = check_len(1)
print(check_one('a')) # returns True
print(check_one('abc')) # return False
Expected output:
True
validator evaluated to False
False
I've tried creating an annotation, but can only access the function creation with it.
One way would be to define the functions like this:
def log_false(fn):
def inner(*args):
res = fn(*args)
if not res:
print("validation failed for {}".format(fn.__name__))
return res
return inner
#log_false
def check_one(s):
return check_len(1)(s)
But this way we lose the dynamic creation of validation functions.
You're doing the validation in the wrong place. check_len is a function factory, so res is not a boolean - it's a function. Your #log_false decorator has to wrap a validator function around each lambda returned by check_len. Basically you need to write a decorator that decorates the returned functions.
def log_false(validator_factory):
# We'll create a wrapper for the validator_factory
# that applies a decorator to each function returned
# by the factory
def check_result(validator):
#functools.wraps(validator)
def wrapper(*args, **kwargs):
result = validator(*args, **kwargs)
if not result:
name = validator_factory.__name__
print('validation failed for {}'.format(name))
return result
return wrapper
#functools.wraps(validator_factory)
def wrapper(*args, **kwargs):
validator = validator_factory(*args, **kwargs)
return check_result(validator)
return wrapper
Result:
#log_false
def check_len(n):
return lambda s: len(s) == n
check_one = check_len(1)
print(check_one('a')) # prints nothing
print(check_one('abc')) # prints "validation failed for check_len"

Is there something like the threading macro from Clojure in Python?

In Clojure I can do something like this:
(-> path
clojure.java.io/resource
slurp
read-string)
instead of doing this:
(read-string (slurp (clojure.java.io/resource path)))
This is called threading in Clojure terminology and helps getting rid of a lot of parentheses.
In Python if I try to use functional constructs like map, any, or filter I have to nest them to each other. Is there a construct in Python with which I can do something similar to threading (or piping) in Clojure?
I'm not looking for a fully featured version since there are no macros in Python, I just want to do away with a lot of parentheses when I'm doing functional programming in Python.
Edit: I ended up using toolz which supports pipeing.
Here is a simple implementation of #deceze's idea (although, as #Carcigenicate points out, it is at best a partial solution):
import functools
def apply(x,f): return f(x)
def thread(*args):
return functools.reduce(apply,args)
For example:
def f(x): return 2*x+1
def g(x): return x**2
thread(5,f,g) #evaluates to 121
I wanted to take this to the extreme and do it all dynamically.
Basically, the below Chain class lets you chain functions together similar to Clojure's -> and ->> macros. It supports both threading into the first and last arguments.
Functions are resolved in this order:
Object method
Local defined variable
Built-in variable
The code:
class Chain(object):
def __init__(self, value, index=0):
self.value = value
self.index = index
def __getattr__(self, item):
append_arg = True
try:
prop = getattr(self.value, item)
append_arg = False
except AttributeError:
try:
prop = locals()[item]
except KeyError:
prop = getattr(__builtins__, item)
if callable(prop):
def fn(*args, **kwargs):
orig = list(args)
if append_arg:
if self.index == -1:
orig.append(self.value)
else:
orig.insert(self.index, self.value)
return Chain(prop(*orig, **kwargs), index=self.index)
return fn
else:
return Chain(prop, index=self.index)
Thread each result as first arg
file = Chain(__file__).open('r').readlines().value
Thread each result as last arg
result = Chain(range(0, 100), index=-1).map(lambda x: x * x).reduce(lambda x, y: x + y).value

How To make decorator get invoked on a recursive function call?

So here's an extension to this question: https://stackoverflow.com/a/37568895/2290820
on how to optionally Enable or Disable Decorator on a Function.
On those lines, I came up with something like this to make decorator get invoked on a recursive call:
def deco(f):
def fattr(attr):
f.attr = attr
def closure(*args):
f(*args)
f.unwrap = f
f.closure = closure
return f
return fattr
#deco
def printa(x):
if x > 1:
print x
return printa(x-1)
else:
print x
return
printa({1:1})(5)
# do the same call w/o deocorator
def finta(x):
if x > 1:
print x
return finta(x-1)
else:
print x
return
finta(5) # this works
to experiment with decorators on a recursive function. Clearly, printa recursive version is not behaving the way it should be.
I could do
g = printa({1:1})
g.closure(5)
to turn on the decorator option or not use that option. Anyway, regardless of good or bad design, How can I make decorator get invoked on a recursive call?
In your deco you have an assignment f.attr = attr that "eats" your argument after first recursive call. Your should modify your recursive call this way:
def deco(f):
def fattr(attr):
f.attr = attr
def closure(*args):
f(*args)
f.unwrap = f
f.closure = closure
return f
return fattr
#deco
def printa(x):
if x > 1:
print x
return printa(None)(x-1) # None will be assigned to f.attr
else:
print x
return
printa({1:1})(5)
5
4
3
2
1

how to compose two or more functions in python

I have these two functions:
def swap_joker1(afile):
idx = afile.index(27)
if idx == len(afile) - 1:
afile[idx],afile[0]=afile[0],afile[idx]
else:
afile[idx],afile[idx+1]=afile[idx+1],afile[idx]
return afile
def swap_joker2(afile):
idx = afile1.index(28)
afile[idx],afile[idx+1],afile[idx+2]=afile[idx+1],afile[idx+2],afile[idx]
return afile
how can I compose them together. and become a new function called cipher_functions?
Well, you can create your own cute function composition function:
import functools
def compose(*functions):
return functools.reduce(lambda f, g: lambda x: f(g(x)), functions)
def foo(var):
return var // 2
def bar(var):
return var + 1
if __name__ == '__main__':
# Apply bar, then foo
composition = compose(bar, foo)
print composition(6)
You can apply the functions in whatever way you like, and in as many ways as you like. This answer was made possible, with the help of this website.
A few methods:
cipher_functions = lambda afile:swap_joker2(swap_joker1(afile))
def cipher_functions(afile):
return swap_joker2(swap_joker1(afile))
import functional #third party, not maintained. Alternatives exist
cipher_functions = functional.compose(swap_joker1, swap_joker2)
It would be great if Python offered a composition operator. Unfortunately, you need to do it yourself.
def cipher_functions(afile):
# This is f(g(x)); swap for g(f(x)) if necessary
return swap_joker1(swap_joker2(afile))
You can define a composition function easily:
def compose(f, g):
return lambda *args, **kwargs: f(g(*args, **kwargs))
cipher_functions = compose(swap_joker1, swap_joker2)
Your swap_joker1 function returns either afile or None, which make composition a bit hard.
Assuming it was actually:
def swap_joker1(afile):
idx = afile.index(27)
if idx == len(afile) - 1:
afile[idx],afile[0]=afile[0],afile[idx]
else:
afile[idx],afile[idx+1]=afile[idx+1],afile[idx]
return afile
Which returns afile unconditionally, you can simply write the composition as:
def cipher_functions(afile):
return swap_joker2(swap_joker1(afile))
And more generally:
def compose(f, g):
"Returns x -> (f o g)(x) = f(g(x))"
def fog(*args, **kwargs):
return f(g(*args, **kwargs))
return fog
cipher_functions = compose(swap_joker2, swap_joker1)

Categories