develooper Front page | perl.perl6.language | Postings from March 2005

strings, numbers and... lists?

Thread Next
March 28, 2005 06:44
strings, numbers and... lists?
Message ID:
The discussion about x/xx made me wonder. We have this table:

    string  list
    x       xx
    ~       ,

And we also have this table:

    string  number
    ~       +
    x       *
    eq      ==

But there is overlap between the two tables:

    string  number  list
    x       *       xx
    ~       +       ,

Let's add the other operators:

    string  number  list
    x       *       xx
    ~       +       ,
    eq      ==

The list form of eq/== is known in >>eq<< and >>==<<, but these don't
work well when the types are not balanced.

And the string form of minus is currently spelled s/foo//. There is, as
far as I know, no listy minus.

I suggest we add operators to fill this the void and add to TIMTOWTDI.
Because the ASCIIbet is exhausted, I'll use existing numeric operators
enclosed in square brackets.

    string  number  list
    x       *       xx
    ~       +       , [+]
            -       [-]
    eq      ==      [~~]

I chose chosen smart match instead of equality here, because of the
balancedness thing. [~~] is like the rather verbose all(... >>~~<< ...):

    if (all(@foo >>~~<< @bar) { ... }
    if (@foo [~~] @bar) { ... }

The difference between , and [+] is precedence:

    my @foo = (@bar, @baz);
    my @foo = @bar [+] @baz;

This buys us a nice alternative for push too. It's like the much
discussed ,= but communicating the same thing better:

    push @foo, @bar;
    @foo [+]= @bar;

This is still rather boring. It gets more interesting when we add minus:

    splice @foo, $_, 1 given first { @foo[$_] ~~ 15 }, 0..@foo.last;
    @foo [-] 15;  # whoa!

Note that grep solves a different problem: it would remove all
occurrences of 15, instead of only the first encountered. And, this can
remove multiple elements at once:

    splice @foo, $_, 1 given first { @foo[$_] ~~ 15 }, 0..@foo.last;
    splice @foo, $_, 1 given first { @foo[$_] ~~ 15 }, 0..@foo.last;
    splice @foo, $_, 1 given first { @foo[$_] ~~ 42 }, 0..@foo.last;

    @foo [-] (15, 15, 42);

If we allow all() to be a special case on the RHS, we get a synonym for
grep too, now communicating what we want gone instead of what we want to
be left with:

    @foo .= grep :{ not /\W/ };
    @foo [-]= all /\W/;

Consistency would want any() to remove a random matching element, and the
default to really justs imply one().

There's one hole left in the table. Stringy minus can just be ~-:
    string  number  list
    x       *       xx
    ~       +       , [+]
            -       [-]
    eq      ==      [~~]

This wants ~ to be an abbreviation for ~+, and eq for ~==, which if we
grab the table for bitwise operations and put everything to gether, gets
us ?== for boolean equality.

    do { ($temp = $foo) ~~ s/gone//; $temp } 
    $foo ~- "foo";

Of course, you'd want to allow regexes too:

    do { ($temp = $foo) ~~ s/g+one//; $temp }
    $foo ~- /g+one/;

We're now communicating that we want to remove something, instead of
replace it with an empty string. This is very powerful self-documentation.

Again, if we want to remove all of them, a conjunction comes to our

    do { ($temp = $foo) ~~ s/g+one//; $temp }
    $foo ~- all /g+one/;

But, of course, :each should work too.

More dwimmery can probably be invented by looking at the operator table
in S03 and applying the same logic. 

[+] should probably be spelled (+) but that's uglier. Other alternatives
are @+ (but this REALLY says array, while it's for lists) and *+ (hard
to read, but ~^ and ~- have the same problem).


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About