Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

by erroneousBollock (Curate)
on Sep 12, 2007 at 06:13 UTC ( #638488=note: print w/ replies, xml ) Need Help??


in reply to Re: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
in thread Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)

I really like that Javascript version.

You either like or hate regexps for this kind of thing, but as regexp-based solutions go "ZBBBCZZ".match(/((.)\2*)/g) is damn concise.

I couldn't get the perl version to be that concise... pity.

I hope perl6's Rules allow one to specify a sub-match that can be referenced and then thrown away.

-David


Comment on Re^2: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
Download Code
Re^3: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
by TimToady (Parson) on Sep 12, 2007 at 16:58 UTC
    Earlier in this thread we already had a Perl 6 solution resembling this:
    "ZBBBCZZ".comb(/(.)$0*/)
    I think that does what you want here, though in this case it's the .comb method that's forgetting the $0 capture, not the regex, so you raise an interesting point for the general case.

    It's not specced yet, but the current standard grammar has two rules called "same" and "differ" that could be used for this, along with the "quantify via separator" form:

    "ZBBBCZZ" ~~ m:g/ . ** <?same> /)
    Though that is also kinda cheating on your general point by not capturing anything in the first place...

    At the moment, forgetting a capture within Perl 6 regex involves forcefully rebinding the capture to nothing, or returning an alternate result via a closure. Perhaps there should be a way to declare temp bindings that are not intended for the final match. But maybe not, if it's just going to turn into an obscure feature. So more likely it'll be some kind of context that just happens to work that way when you expect it to, like .comb does, only inside regex syntax.

Re^3: Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...) (js)
by tye (Cardinal) on Sep 12, 2007 at 17:45 UTC

    I couldn't get that javascript version to work. Sure, it looks nice. It looks very much like something many tried as a first stab in Perl. But I'm not convinced it works in javascript any better than it works in Perl. I was not able to get it to return anything (while in Perl it returns too much).

    Update: Thanks, eB. I was eventually able to get your example to demonstrate that the code works.

    - tye        

      It works just fine. Try pasting the following into your url bar:

         javascript:alert("ZBBBCZZ".match(/((.)\2*)/g))

      That should alert the output of .toString() called on the array result.
      If you have Firefox use the following to see the unadulterated array source:

         javascript:alert(("ZBBBCZZ".match(/((.)\2*)/g)).toSource())

      -David

      Update: Does that imply that Javascript has different capture semantics than perl?

        Does that imply that Javascript has different capture semantics than perl?

        No. It is just that match() returns the matched substrings, not the captured substrings. If //g in a list context could be told to return the matched substrings (what it does when there are no capturing parens), then the same solution would work in Perl.

        A better JavaScript solution is actually: "ZBBBCZZ".match(/(.)\1*/g). No need for the second set of parens.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://638488]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2014-08-20 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (105 votes), past polls