strings, numbers and... lists?

2 views
Skip to first unread message

Juerd

unread,
Mar 28, 2005, 9:44:47 AM3/28/05
to perl6-l...@perl.org
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

Juerd

unread,
Mar 28, 2005, 9:49:41 AM3/28/05
to perl6-l...@perl.org
Juerd skribis 2005-03-28 16:44 (+0200):

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

Note: unfair comparison. The equal thing would be [-]=.

In fact, this illustrates the problem even better, because to get
@foo [-] 15 without [-], you need either a temporary array:

do {
my @temp = @foo;
splice @temp, $_, 1 given first { @temp[$_] ~~ 15 }, 0..@temp.last;
@temp
}

or a grep with an extra variable:

do {
my $seen_first;
grep { $seen_first or $_ ~~ 15 && $seen_first++ } <== @foo

Reply all
Reply to author
Forward
0 new messages