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

strings, numbers and... lists?

Thread Next
From:
Juerd
Date:
March 28, 2005 06:44
Subject:
strings, numbers and... lists?
Message ID:
20050328144447.GS29322@c4.convolution.nl
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
resque.

    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).


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About