Nigel Sandever wrote:

On Sat, 19 Feb 2005 18:42:36 +1100, [EMAIL PROTECTED] (Damian Conway) wrote:

The Awesome Power of Junctions:



As I tried to express elsehwere, this what I'm looking for.


Instinctively, and for a long time since I first came across Q::S, I thought that the "killer app" of Junctions is there somewhere, I'm just not seeing it yet.

I'd really like to see some practical demonstrations of the Awesome Power.

Something that goes beyond producing a boolean result from a set of values that could equally be done using hyperoperators?


Well, that was one of my stumbling blocks early on. They don't really give you the power to do anything you couldn't previously do. They just make certain things a lot easier to do.

But I'll take a whack at giving you something non-trivial to do without junctions:

$re1 = /^ <-[x]>* x <-[x]>* $/; # match a string with exactly one 'x' in it.
$re2 = /^ <-[y]>* y <-[y]>* $/; # ditto 'y'
$re3 = /^ <-[z]>* z <-[z]>* $/; # ditto 'z'


$re4 = all($re1, $re2, $re3); # matches if x,y, & z all appear exactly once, in any order.
$re5 = one($re1, $re2, $re3); # matches if there is exactly one x, y, or z
$re6 = any($re1, $re2, $re3); # matches if there is at least one of x,y,z that appears exactly once.
$re7 = none($re1, $re2, $re3); # matches if there are 0 or 2+ of each of x,y,z.


And all seven of these can be used as any stored RE can:

   if $x ~~ $re6 {...};
   given $x {
      when $re5 {...}
      when 'santa' {...}
      when ($re1 | $re3) & $re3 {...}
   }

Looking at the junctioned RE's:
#6 would is straight forward to do as a single RE in this case, and in the general case.
#5 would be fairly trivial to in this case, but not in the general case.
#4 is possible with a lot of zero-length look aheads and look-behinds, but very ugly.
#7 I have no idea how to attack as a single RE in anything close to approaching elegance.


So what have we gained here? The ability to join several RE's together, into something that still acts like a single RE. Show me the equivalent code without junctions, and then we'll compare the power of junctions.

btw, the examples above assume the ability to store a junction. So you either have to 'use junctions;', or convince Larry to rescind that restriction.

HTH.

-- Rod Adams




Reply via email to