[Parrot-users] A length() runtime written in NQP

Jay Emerson jayemerson at gmail.com
Wed Jul 6 15:44:19 UTC 2011


>> I'm trying to write a function which would behave something like (pseudo-code):
>>
>> a = 0
>> b = (1, 2, 3)
>> length(a)       # Returns 1
>> length(b)       # Returns 3
>
> Just to clarify, you want a single number or string passed in to
> return a length of 1, but aggregate types should return the number of
> elements in them?

Yes.

> Do you still want length(555) = 1 and length("555") = 1, or should they be 3 ?

No these would both have length 1.

>> Now, in a squaak-like language, I think a is a scalar and b is a pmc.
>> I tried the following:
>>
>> sub length(*@args) {
>>    my @arg := @args[0];
>>    my $len := @arg;
>>    pir::return($len);
>> }
>
> One gotcha about NQP is that the sigils (@ and $) are lies, they are just
> namespaces that you can use, and NQP doesn't care if you store an array in $foo
> or a scalar in @bar.

Er, ok... I'll catch up eventually, but still don't know how to proceed.

>
> Also, is there a reason you are using pir::return() ? I think:
>
>    return $len;
>
> should work.

Yes, I copied what was done in squaak with returns:

sub say(*@args) {
    pir::say(pir::join('', @args));
    pir::return();
}

However, I still don't know how to get the length of an array?  Maybe
the answer is "it depends" and I didn't give you an example was
well-defined.  !-)

Thanks,

Jay

>
> Duke
>
> --
> Jonathan "Duke" Leto <jonathan at leto.net>
> 209.691.DUKE // http://leto.net
> NOTE: Personal email is only checked twice a day at 10am/2pm PST,
> please call/text for time-sensitive matters.
>



-- 
John W. Emerson (Jay)
Associate Professor of Statistics
Department of Statistics
Yale University
http://www.stat.yale.edu/~jay


More information about the Parrot-users mailing list