I’m writing a plugin for another piece of sofware we use in my office that will allow users to audit the files they are working on. I’m trying to keep my tool as flexible as possible. The idea I have is that the user will generate a tree of Nodes that can contain other Nodes as sub-nodes. At the bottom of the tree the Nodes will be condition nodes that will either fail or pass depending on the file the user is working in. In addition, the user can set each Node to a specific logic type including AND, OR, NOR, NAND.
AND: All sub nodes must pass
OR: At least one sub node must pass
NAND: At least one sub node must fail
NOR: All sub nodes must fail
What I’m trying to figure out now is if I have some collection of bool’s that were returned by a node or sub-node, what is the most efficient way to apply the logic types above to this list? Off hand I started writing foreach statments, but it seems since binary logic is so fundamental to the way computers work there would be a better, faster, and less iterative method.
Linq is your friend:
This really still just does a
foreach, but they are a lot more compact and theAllandAnyoperations fit what you need.The
p => pis a lambda that returns a boolean. If you for example are checking nodes that have a methodDoesThisPass, you rewrite the checks like this: