lambda - Boolean functors in lisp -


i find myself in situation when need combine several predicate one. there standard way of doing this, similar compliment?

suppose there several simple predicates (e.g. is-fruit-p, is-red-p, grows-on-trees-p etc.) , list of objects subset must filtered out using more 1 predicate. what's better way of achieving following:

(remove-if #'is-fruit-p             (remove-if #'is-red-p                        (remove-if #'grows-on-trees-p list-of-objects))) 

i'm not sure if such function available box. if need combine functions can determine in compile time can write macro this. if have detect predicate functions dynamically can write function loop throw list of functions , accumulate results until false condition.

the macro can this:

(defmacro combine-predicates (combine-func &rest preds)   (let ((x (gensym)))     `(lambda (,x) (,combine-func ,@(loop p in preds                        collecting `(funcall ,p ,x)))))) 

and can use this

(remove-if (combine-predicates ,                                 #'is-fruit-p                                 #'is-red-p                                 #'grows-on-trees-p) obj-list) 

Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

Making Empty C++ Project: General exception (Exception from HRESULT:0x80131500) Visual Studio Community 2015 -

How to fix java warning for "The value of the local variable is not used " -