go get -u github.com/1pkg/gamb
This package provides generic variadic implemention of McCarthy's Ambiguous Operator in go. Gamb exposes three ambiguous functions Amb
to yield first variable matching ambiguous predicate; All
to yield all variables matching ambiguous predicate; Ord
to yield first strict ordered variable matching ambiguous predicate in single pass. Ambiguous predicate is defined as function func(v ...interface{}) bool
that accepts ambiguous variable sets permutations and check some bolean condition against them.
out := Amb(
func(v ...interface{}) bool {
return v[0].(int)+v[1].(int)-v[2].(int) == 7
}
NewVar(10, 20, 30),
NewVar(1, 2, 3, 5, 10),
NewVar(2, 3, 4),
)
fmt.Println(out) // [10 1 4]
out := All(
func(v ...interface{}) bool {
return v[0].(int)+v[1].(int)-v[2].(int) == 7
}
NewVar(10, 20, 30),
NewVar(1, 2, 3, 5, 10),
NewVar(2, 3, 4),
)
fmt.Println(out) // [[10 1 4], [10, 2, 3], [10, 3, 2]]
out := Ord(
func(v ...interface{}) bool {
return v[0].(int)+v[1].(int)-v[2].(int) == 31
}
NewVar(10, 20, 30),
NewVar(1, 2, 3, 5, 10),
NewVar(2, 3, 4),
)
fmt.Println(out) // [30 5 4]
Note: ambiguous operator generally requires some form of backtracking with n^m
operators, where n
- size of ambiguous variable and m
- number of ambiguous variables (for ord
operator single pass is used reducing complexity to n
). Therefore ambiguous operator is not efficient on processing big inputs.
Gamb is licensed under the MIT License.
See LICENSE for the full license text.