Skip to content

Instantly share code, notes, and snippets.

@simcap
Created October 18, 2017 19:54
Show Gist options
  • Select an option

  • Save simcap/786ca6975d5235f6e80cddcdbc3f5d76 to your computer and use it in GitHub Desktop.

Select an option

Save simcap/786ca6975d5235f6e80cddcdbc3f5d76 to your computer and use it in GitHub Desktop.
package main
import "fmt"
func main() {
//params := []string{"1", "2", "3"}
fmt.Println(and(and(stringNode("1"), stringNode("2")), or(stringNode("3"))))
}
type node interface {
eval([]string) bool
}
func and(ns ...node) *andNode {
a := new(andNode)
a.childrens = append(a.childrens, ns...)
return a
}
func or(ns ...node) *orNode {
a := new(orNode)
a.childrens = append(a.childrens, ns...)
return a
}
type andNode struct {
childrens []node
}
func (a *andNode) eval(arr []string) bool {
out := true
for _, e := range a.childrens {
out = out && e.eval(arr)
}
return out
}
type orNode struct {
childrens []node
}
func (o *orNode) eval(arr []string) bool {
var out bool
for _, e := range o.childrens {
out = out || e.eval(arr)
}
return out
}
type stringNode string
func (s stringNode) eval(arr []string) bool {
for _, e := range arr {
if string(s) == e {
return true
}
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment