From: Jamis Buck Date: 2004-02-04T15:23:10+09:00 Subject: Re: flip flop operator proposal (from condition to condition do ) nobu.nokada@softhome.net wrote: >Hi, > >At Wed, 4 Feb 2004 09:21:49 +0900, >Jamis Buck wrote: > > >>The advantage of using a boolean phrase is that you can use it in >>conditions, where it is most common. That also lets you do something >>when the flipflop is false, which the 'from..do' construct you suggested >>doesn't (easily) allow. >> >> > >Agreed, and it seems easy to implement. > > As evidenced by your patch. Thanks! > > >>Since getting at the object that encapsulates the state of the flipflop >>will be less common (one might even say "rare", although its hard to say >>at this point since neither Perl nor Ruby allow it), the common case >>(just getting the boolean result) should be simple and straightforward. >>Getting at the object might be done via something like this: >> >> in l=~/BEGIN/ .. l=~/END/ => state >> >>The expression itself still returns boolean, but the object >>encapsulating the flipflop is placed in 'state'. >> >> > >How will you use it, in other words, how and when it will be >changed? > > > I suggested this primarily because there were a few posters that mentioned the fact that the "hidden state" aspect of the flip-flop op turned them off from the whole concept. By exposing the state, it makes it less "mysterious". However, I'll give a very contrived example of where it might be useful. Suppose you had an input file that contained the following: BEGIN 1 2 special 3 END In general, you want a parser that extracts everything between BEGIN and END, but there are a few cases where you want to treat the word 'special' as the end instead, and then ignore everything until the next BEGIN. while line = file.gets if in line=~/BEGIN/ .. line=~/END/ => state state.set_state( line =~ /special/ ) if state.get_state # process line end end end Like I said, it's pretty contrived, and would be better solved by choosing smarter guard conditions. However, it demonstrates a situation where exposing an interface for manipulating a flip-flop's state may be useful. -- Jamis Buck jgb3@email.byu.edu ruby -h | ruby -e 'a=[];readlines.join.scan(/-(.)\[e|Kk(\S*)|le.l(..)e|#!(\S*)/) {|r| a << r.compact.first };puts "\n>#{a.join(%q/ /)}<\n\n"'