2016-10-10 10:50:54 -04:00
|
|
|
package predicate
|
|
|
|
|
|
|
|
type Predicate func() bool
|
|
|
|
|
2016-10-11 06:44:17 -04:00
|
|
|
func (this Predicate) And(predicate Predicate) Predicate {
|
|
|
|
return All(this, predicate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this Predicate) Or(predicate Predicate) Predicate {
|
|
|
|
return Any(this, predicate)
|
|
|
|
}
|
|
|
|
|
2016-10-10 10:50:54 -04: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 06:30:53 -04:00
|
|
|
|
|
|
|
func Not(predicate Predicate) Predicate {
|
|
|
|
return func() bool {
|
|
|
|
return !predicate()
|
|
|
|
}
|
|
|
|
}
|