Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Smart match in p5

by halley (Prior)
on Mar 14, 2005 at 13:52 UTC ( [id://439274]=note: print w/replies, xml ) Need Help??


in reply to Smart match in p5

I read Juerd's request. I read the summary of smart matching you linked. I'd like to think I'm not dense, but I don't get what this operator is supposed to do, or why it's better than the various operators that exist?

Maybe it's because I watched The Fifth Element this weekend, but I read the ~~ as that annoying 'bzzzZZz!' noise which the superstar personality did whenever he wanted other people to divine his vague intentions and to get lost.

I know Perl is supposed to have lots of DWIM, but another programmer should be able to understand WIM too.

--
[ e d @ h a l l e y . c c ]

Replies are listed 'Best First'.
Re^2: Smart match in p5
by Juerd (Abbot) on Mar 14, 2005 at 16:38 UTC

    I don't get what this operator is supposed to do, or why it's better than the various operators that exist?

    It's one operator to compare anything to anything. It's eq, ==, grep, exists, all in one. That's useful if you want to pass a condition to be applied to a general function. Normally, you'd supply a coderef, but now, you can just supply any value it can be smart matched to. And the great thing is, that you can still pass that coderef.

    The "when" operator implicitly smart-matches against $_.

    given ($address) { when (@whitelist) { $score -= 30 } when (@blacklist) { $score += 50 } when (/example\.com$/) { $score = 0 } default { ... } }
    The smart match operator, in this example invisible because it's used by when internally, avoids a lot of typing. Compare the alternative:
    for my $a ($address) { if (grep $_ eq $a, @whitelist) { $score -= 30; last; } if (grep $_ eq $a, @blacklist) { $score += 50; last; } if ($a =~ /example\.com$/) { $score = 0; last; } ... }
    Smart match replaces grep and =~ here. But it can also replace eq and == and exists, making it a great tool for making code easier to read, especially when implied with given/when.

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

    PS The default block { } around ... isn't needed, but it's good documentation.
      I know that idiom plays a large part of understanding a language, but I feel like neither of those examples are particularly self-documenting or literate.

      Saying "Given X, when A, B, C, do T" does not roll off the tongue. It doesn't express any particular relationship between X and the list. It doesn't help me understand what the when operator does. Are we iterating? Triggering? You said "smart matching," but uh, that still leaves my uninitiated brain lost. Also, "when" connotates time or passive reaction to me, not compliance or active processing.

      "Given X, if it matches any of A, B, C, do T" is a step up. Going from a complicated concept of "does whatever you want" to a more specific concept of "performs this particular comparison" is a shift toward self-documentary or literate programming.

      As you can see, I am very dubious of the value of Perl6, and this sort of divergence from pragmatic programming to ethereal ivory-tower philosophizing just doesn't get me any closer to enjoying the changes in the language. The language should express generic problem-solving succinctly, but not so tersely it cannot be understood concretely.

      --
      [ e d @ h a l l e y . c c ]

        Let's discuss that in a separate thread. Do note that switch/case (just spelled differently in Perl) is much requested and exists in many languages. See the link to S04 to see the table of how matches operate. If you don't know what S04 is, then you should indeed read it before questioning the use of what it describes.

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

Re^2: Smart match in p5
by scrottie (Scribe) on Mar 14, 2005 at 19:26 UTC
    Blatent-plug: As brian d foy commented in his review, Perl 6 Now: The Core Ideas Illustrated with Perl 5 has a whole chapter on Switch and smart matching. Sadly it came out before xmath's amazing hack, but there are a few examples in there where smart matching saves a lot of typing and makes code a lot more clear. (Thankfully, there are 22 other chapters - as cool as smart matching is, vector operations and coroutines are much cooler, IMO). So go fork over your cash right a away - I worked harder for your cash than you did ;)

    -scott

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-05-27 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found