Logical patterns 3
Last updated
Last updated
We discussed the concept of generating conditional outputs using “if” expressions. In this section we refine this idea to general execution control flow of information using conditionals.
Let’s assume that given a number “value” we wish to compute its inverse “1 / value” or “0”, if the number is zero ie. to avoid division by zero. Using conditionals with the “if” function in the expression editor has the following outcome: The component just blatantly fails; but why?
The reason for this is based on the sequence of events that take place when we evaluate expressions. In order for the “if” function to be executed, all its supplied arguments must be evaluated beforehand. At first the conditional sub-expression “value = 0” is evaluated, then the constant expression “0”, and finally the numeric division is performed. This means that in every scenario, whether the “value” is zero or not, the division will be performed!
What we need is a construct that captures the idea of only one outcome expression being evaluated, based on a condition, but not both. We call this behaviour branching because only one path of logic is ever taken. We can achieve this using the approach seen below. Notice how parts of the graph turns orange which signifies that they become inactive based on the value of the slider.
There are two new components used here that allow us to achieve this conditional execution behaviour: “stream gate” and “stream filter”.
The “stream gate” component redirects the input data on either output based on the boolean value supplied at the gate. It behaves like a valve for fluids, hence the stream analogy perhaps. Nevertheless, we need a symmetric opposite of this to merge back the flow of execution so we use the “stream filter”.
The “stream filter” component selects either of the two stream inputs based on the boolean value supplied at the gate. Notice that the output name changes based on which stream is selected.
Practice
The “stream gate” component by itself might not appear very useful but you can create a “warning” system as means of trapping invalid user input
Instead of the “stream filter” you may use a “list item” component to select the first or second item from a list or even “addition” to merge results. Experiment with various merging techniques.
Using gates in visual programming sense feels a bit verbose and crufty. The graph below shows the equivalent of the “expression” editor using the “python” component instead. Python has no problem with our number inversion logic because “if” is not a function, which forces the evaluation of its arguments. It is a different construct altogether created exactly for the purposes of controlling the flow of data.
We investigated the most common uses of conditional expressions, namely to filter data and to control computational flow. Filtering is a critical tool for data processing operations. Semantically, it allow us to select or discard values and introduce abrupt discontinuities, in the otherwise smooth flow of sequences. It is the foundation for expressing patterns that capture logic.
We derived the need for control flow to avoid disaster i.e. dividing numbers by zero. Instead of filtering for data values, we filtered i.e. allowed of disallowed computing instructions from being performed. Conditional execution is almost synonymous with computing; it is a prerequisite in the sense without it, we would have been able to perform only complex calculations. With the ability to express decision making, we can alter the flow of events without even always resulting in a predetermined result.
Boolean operations are typically applied to true/false data values. However, if we represent integers as sequences of on/off bits, we can extend the use of boolean algebra to groups of bits. Those are called bitwise operations and as they are not essential, additional information is offered as bonus materials [>].