Created
October 18, 2017 19:54
-
-
Save simcap/786ca6975d5235f6e80cddcdbc3f5d76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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