When I used to teach math, one thing that came up a lot in probability and also in proofs is the “At Least One” Rule. In essence, this rule states that “Every P is Q ” and “At least one P is not Q ” are negations.
As an example, imagine we are tossing a coin 50 times. One could make a guess that the outcome will be “Every toss is heads.” This is not a great guess, because the only thing it takes for it to be false is if “At least one toss is not heads.”
It should be noted that “Every toss is not heads” is a frequent mistake we make when forming the negation for “Every toss is heads”.
The essence of a negation is that P and “not P ” can never be true simultaneously and can never be false simultaneously. If one is true, the other is false…and vice-versa. Since “Every toss is heads” and “Every toss is not heads” (i.e. “Every toss is tails”) can both be false at the same time, these are not negations of each other.
The .some and .every functions in JavaScript
The inspiration for this post is that the functions .some and .every are great examples of how the age-old At Least One rule comes into play in programming.
Let’s say that we have an array called coinTosses
which contains items with a property isHeads
. It might look something like this:
Note that we don’t need an isTails
property since this can be determined by the value of isHeads
.
We could then create a condition that will take a coin toss item from the array above and test something about it (say, whether it is heads).
One might be tempted to then do something like this in order to check if every toss is heads:
But note that in checking whether each coin toss is heads, we are programmatically checking whether at least one is not heads. That’s a hidden use of the At Least One rule.
The point of the .every()
function in JavaScript is that we can avoid the above code by writing
…and that’s pretty cool. But I also think it’s cool that the “old school” method using a for
loop explicitly points out that we are really applying the At Least One rule when it comes down to it.
Summary
In essence, it will always be true that for ANY function condition
which returns a boolean and for ANY array myArray
, we have the following:
Or, alternatively we could say that
In math, we are always coming up with theorems to help us move symbols around within statements and to rephrase things symbolically to gain the advantage of clarity and “grokability.” The power resides in being able to prove that the symbolic rearrangements are logically valid steps to take in all possible cases.
For example, I previously wrote about DeMorgan’s Laws, which is all about logically equivalent statements that result from rearranging ! ( A and B )
into (! A) or (! B)
.
The At Least One rule is similar to DeMorgan’s Laws in that it lets us pick which phrasing of a statement is more amenable to our needs in a given situation. You might even notice that extending DeMorgan’s Laws to apply to more than 2 items essentially is the At Least One rule.
When it comes down to it, knowing that something is true for all possible inputs is a powerful thing that transcends language and gets to the heart of why I love symbolic reasoning.
Leave a Reply