predicate

Python predicate function name convention

Python predicate function name convention Question: Does Python have some convention for names of predicate functions? For example predicate function names end with p for Common Lisp or ? for Scheme. Sometimes are used prefixes like is_ or has_, …, but they may not be appropriate in some cases (they can be quite long to …

Total answers: 1

Pointfree function combination in Python

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 …

Total answers: 6

Find first element in a sequence that matches a predicate

Find first element in a sequence that matches a predicate Question: I want an idiomatic way to find the first element in a list that matches a predicate. The current code is quite ugly: [x for x in seq if predicate(x)][0] I’ve thought about changing it to: from itertools import dropwhile dropwhile(lambda x: not predicate(x), …

Total answers: 4