-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicates.go
More file actions
97 lines (83 loc) · 2.16 KB
/
predicates.go
File metadata and controls
97 lines (83 loc) · 2.16 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package numstream
import (
"fmt"
)
//a Predicate takes an int and returns a boolean.
type Predicate func(int) bool
type Predicate2 func(int, int) bool
//True always returns true.
func True(n int) bool {
return true
}
//False always returns false.
func False(n int) bool {
return false
}
//IsLess returns true iff the second argument is less than the first.
func IsLessThan(n, m int) bool {
return m < n
}
//IsMultipleOf returns true iff the second argument is a multiple of the first.
func IsMultipleOf(m, n int) bool {
return n%m == 0
}
//IsPalindrome returns true iff the int argument can be reversed without changing its value.
func IsPalindrome(n int) bool {
s := fmt.Sprintf("%d", n)
l := len(s)
for i, c := range s {
if c != rune(s[l-i-1]) {
return false
}
}
return true
}
/* Functions returning Predicates */
//Partial2Pred returns a Predicate from a Predicate2 with the first argument preapplied
func Partial2Pred(p Predicate2, m int) Predicate {
return func(n int) bool {
return p(m, n)
}
}
//Done returns a Predicate that evaluates to true iff a signal has been received on the done channel.
func Done(done <-chan bool) Predicate {
var d bool
go func() {
d = <-done
}()
return func(n int) bool {
return d
}
}
//Conjoin returns a Predicate that evalutes to true iff every Predicate argument evaluates to true
//for the applied int.
func Conjoin(preds ...Predicate) Predicate {
return func(n int) bool {
for _, p := range preds {
if !p(n) {
return false
}
}
return true
}
}
//Disjoin returns a Predicate that evaluates to true iff any Predicate argument evaluates to true
//for the applied int
func Disjoin(preds ...Predicate) Predicate {
return func(n int) bool {
for _, p := range preds {
if p(n) {
return true
}
}
return false
}
}
//IsLessThanX returns a Predicate that evalutes to true iff the applied int argument is less than the bound int argument.
func IsLessThanX(x int) Predicate {
return Partial2Pred(IsLessThan, x)
}
//IsMultipeOf returns a Predicate that evaluates to true iff the applied int is a multiple of the bound int argument.
func IsMultipleOfX(x int) Predicate {
return Partial2Pred(IsMultipleOf, x)
}