Boolean combinators
In the Clojure Tip above, I described a kind of Boolean combinator that lets us build complex rules out of simpler rules. Your task is to build those combinators. Please define:
(defn rule-and
([])
([rule])
([rule1 rule2])
([rule1 rule2 & rules]))
(defn rule-or
([])
([rule])
([rule1 rule2])
([rule1 rule2 & rules]))
(defn rule-not [rule])
Note that rule-and
and rule-or
are monoids and so they
follow the monoid
pattern.
For a bonus, write the functions rule-if
(that implements
the logical if arrow operator) and rule-iff
(that
implements the logical if and only if double-arrow
operator). Not that rule-if
is different from the
programming if
since it returns true
if the condition is
false
.
Please submit your solutions as comments on this gist.
@steffan-westcott Yup, the zero args vs any-arity issue didn't show up in my testing, and I didn't notice the lack of generality. Nice solution with junction, btw.
@miner Cool! My macro-fu is weak, so I tend to take refuge in The First Rule of Clojure Macro Club. :) In practice, I wouldn't bother golfing this stuff down (original w/ duplication is fine) but I wanted to offer at least something different. It's fun and educational to see the other approaches here!