-
Notifications
You must be signed in to change notification settings - Fork 12
/
predicate.go
66 lines (57 loc) · 1.55 KB
/
predicate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package fuego
// Predicate represents a predicate (boolean-valued function) of one argument.
type Predicate func(t Entry) bool // TODO return EntryBool instead of bool??
// And is a composed predicate that represents a short-circuiting logical
// AND of this predicate and another.
func (p Predicate) And(other Predicate) Predicate {
return func(t Entry) bool {
if p == nil || other == nil {
return false
}
return p(t) && other(t)
}
}
// Or is a composed predicate that represents a short-circuiting logical
// OR of two predicates.
func (p Predicate) Or(other Predicate) Predicate {
return func(t Entry) bool {
if p == nil {
p = False
}
if other == nil {
return p(t)
}
return p(t) || other(t)
}
}
// Xor is a composed predicate that represents a short-circuiting logical
// XOR of two predicates.
func (p Predicate) Xor(other Predicate) Predicate {
return func(t Entry) bool {
return p.Or(other).And(p.And(other).Negate())(t)
}
}
// Negate is an alias for Not().
func (p Predicate) Negate() Predicate {
return p.Not()
}
// Not is the logical negation of a predicate.
func (p Predicate) Not() Predicate {
return func(t Entry) bool {
return p == nil || !p(t)
}
}
// FunctionPredicate creates a Predicate from a Function.
func FunctionPredicate(f Function) Predicate {
return func(t Entry) bool {
return bool(f(t).(EntryBool))
}
}
// False is a predicate that returns always false.
func False(t Entry) bool {
return false
}
// True is a predicate that returns always true.
func True(t Entry) bool {
return Predicate(False).Negate()(nil)
}