Flip-flop (programming)
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a flip-flop is a seldom-used syntactic construct which allows a boolean to flip from false to true when a first condition is met and then back to false when a second condition is met. The syntax is available in the programming languages
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
and
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sa ...
. Similar logic is available in
sed sed ("stream editor") is a Unix utility that parses and transforms text, using a simple, compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems. sed wa ...
and
awk AWK (''awk'') is a domain-specific language designed for text processing and typically used as a data extraction and reporting tool. Like sed and grep, it is a filter, and is a standard feature of most Unix-like operating systems. The AWK langu ...
. A flip-flop with first condition A and second condition B is not equivalent to "if A and not B", as the former has persistent state and is true even if A is no longer true, as long as at some point in the past A was true and B has always been false.


Example

The following Ruby code prints the numbers 4 through 6:
(1..10).each do , x, 
  puts x if (x 

4 .. x

6) end
The first instance of ".." is the
range operator Range may refer to: Geography * Range (geographic), a chain of hills or mountains; a somewhat linear, complex mountainous or hilly area (cordillera, sierra) ** Mountain range, a group of mountains bordered by lowlands * Range, a term used to i ...
, which produces the
enumeration An enumeration is a complete, ordered listing of all the items in a collection. The term is commonly used in mathematics and computer science to refer to a listing of all of the elements of a set. The precise requirements for an enumeration (fo ...
of integers 1 through 10. The second ".." is the flip-flop operator, otherwise known as the flip floperator. 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.


Pitfalls

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, 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: * a predicate that tells whether to switch the flip-flop on, * a predicate that tells whether to switch the flip-flop off. 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.


References

Operators (programming) {{compu-prog-stub