I liked Coke++'s response, but was kinda wondering why he was
repeating things I had already said...

Then I discovered I had only replied to Parrot Raiser, and not to p6u.
So here, belatedly, is my response. :-)

// Carl

---------- Forwarded message ----------
From: Carl Mäsak <cma...@gmail.com>
Date: Mon, May 21, 2012 at 6:07 PM
Subject: Re: lc on arrays
To: Parrot Raiser <1parr...@gmail.com>


Parrot Raiser (>):
> ./perl6 -e 'my @a = < A B C >; @a = lc @a; say @a, " Size = ", @a + 0;'
> a b c Size = 1
>
> Is this the way lc is supposed to operate on the elements of an array?
>
> I.e. converting the individual elements of the source, but combining
> them into one element in the destination.

In a word, yes. Longer explanation follows.

Some operations work on individual items. (Examples: arithmetic
operators, .flip, .abs, .chars and so on.) Other operations work on
collections. (Examples: .keys, .sort, .map) An operation in Perl 6 is
generally one or the other. `lc` happens to work on individual items,
and basically expects a string.

When `lc` doesn't get a string, it stringifies what it did get. In
this case, it stringifies the array @a, turning it into a single item,
a string representation of the array's contents. Lower-cases it, and
assigns back to the array, now as one item.

If you want to take an item-oriented operation such as `lc` and have
it operate on a list, you have to specify it explicitly, in either of
any number of ways:


   @a = @a.map: { lc $_ };
   @a = @a.map: { .lc };
   @a = @a.map: &lc;
   @a = @a».lc;
   @a».=lc;
   @a = (.lc for @a);

Many ways to skin a cat -- but they all have in common that you have
to explicitly "lift" the `lc` function or method to work on each item
of the collection. If you don't it'll treat the collection as one
item.

HTH,
// Carl

Reply via email to