Boolean functors in lisp
common-lisp, functor, higher-order-functions, lambda, lisp
Solution
I'm not sure if such function is available from the box. If you need to combine functions that you can determine in a compile time you can write a macro to do this. If you have to detect predicate functions dynamically you can write function to do this that will loop throw the list of functions and accumulate the results until false condition.
The macro can look like this:
(defmacro combine-predicates (combine-func &rest preds)
(let ((x (gensym)))
`(lambda (,x) (,combine-func ,@(loop for p in preds
collecting `(funcall ,p ,x))))))
And you can use it like this
(remove-if (combine-predicates and
#'is-fruit-p
#'is-red-p
#'grows-on-trees-p) obj-list)
Problem
I find myself in a situation when I need to combine several predicate into one. Is there a standard way of doing this, something similar to `compliment`? Suppose there are several simple predicates (e.g. `is-fruit-p`, `is-red-p`, `grows-on-trees-p` etc.) and a list of objects from which a subset must be filtered out using more than one predicate. What's the better way of achieving this than the following: ``` (remove-if #'is-fruit-p (remove-if #'is-red-p (remove-if #'grows-on-trees-p list-of-objects))) ```