2016-10-10 14:50:54 +00:00
|
|
|
package predicate
|
|
|
|
|
|
|
|
type Predicate func() bool
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v Predicate) And(predicate Predicate) Predicate {
|
|
|
|
return All(v, predicate)
|
2016-10-11 10:44:17 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v Predicate) Or(predicate Predicate) Predicate {
|
|
|
|
return Any(v, predicate)
|
2016-10-11 10:44:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 14:50:54 +00:00
|
|
|
func All(predicates ...Predicate) Predicate {
|
|
|
|
return func() bool {
|
|
|
|
for _, p := range predicates {
|
|
|
|
if !p() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Any(predicates ...Predicate) Predicate {
|
|
|
|
return func() bool {
|
|
|
|
for _, p := range predicates {
|
|
|
|
if p() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-10-11 10:30:53 +00:00
|
|
|
|
|
|
|
func Not(predicate Predicate) Predicate {
|
|
|
|
return func() bool {
|
|
|
|
return !predicate()
|
|
|
|
}
|
|
|
|
}
|