The following Ruby code prints the numbers 4 through 6:
The first instance of ".." is the range operator, which produces the enumeration of integers 1 through 10. The second ".." is the flip-flop operator, otherwise known as the flip floperator.4 Note that the number 5 is printed even though both "x == 4" and "x== 6" are false. This is because the expression remembers that "x == 4" was true on a previous iteration, and that "x == 6" had at that point never been true.
The flip-flop operator needs to store its current state. There is no way for the programmer to explicitly define where this state is stored and what its lifetime is. The lifetime makes a difference when the same code is used by several threads, or in recursive functions. These concurrent accesses to the state of the flip-flop operator can lead to undefined behavior, or at least surprising results, depending on the programming language. For example, in Perl each flip-flop operator has its own state, shared among all the threads,5 the other programming languages do the same.
To work around this limitation, the flip-flop operator would have to be modeled as an abstract data type, parameterized with:
This flip-flop data type would provide a function that queries and updates its state at the same time. This function gets the actual data on which the switching predicates depend and passes that data to the two predicates, if necessary.
Due to this inherent complexity, only few programming languages have adopted the flip-flop operator.
"Perl operators and precedence". Retrieved 2016-10-21. http://perldoc.perl.org/perlop.html#Range-Operators ↩
Nithin Bekal (2014-11-21). "Flip Flop Operator in Ruby". http://nithinbekal.com/posts/ruby-flip-flop/ ↩
"PyCon Australia Lightning talk: Flip Flop Operators (flip floperators)". YouTube. 2018-08-26. https://www.youtube.com/watch?v=BmWLhVMWC9I?t=13m7s ↩
"Range Operator in Perl". 2020-08-16. https://perldoc.perl.org/perlop.html#Range-Operators ↩