Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Introspection and list question

0 views
Skip to first unread message

Ovid

unread,
Dec 12, 2006, 5:19:46 PM12/12/06
to perl6...@perl.org
Hi all,

A couple of quick things.

First, how do I do introspection in Pugs? CPAN's Perl6::Bible hasn't
been updated in a while, but the various ways to get a list of methods
(from
http://search.cpan.org/dist/Perl6-Bible/lib/Perl6/Bible/S12.pod#Introspection
or http://tinyurl.com/yxukar) don't appear to work. They all throw
syntax errors or "No compatible subroutine" errors.

Also, I'm having trouble with problem 7 in
http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
or http://tinyurl.com/tt9e7. Basically, it's flattening nested lists
and I'm embarrassed to admit that I can't figure this out in Perl6.
Thoughts? I've been reading synopses and grepping through Pugs, but to
no avail.

Cheers,
Ovid

--

Buy the book -- http://www.oreilly.com/catalog/perlhks/
Perl and CGI -- http://users.easystreet.com/ovid/cgi_course/

Jerry Gay

unread,
Dec 12, 2006, 8:26:32 PM12/12/06
to publiustemp...@yahoo.com, perl6...@perl.org
On 12/12/06, Ovid <publiustemp...@yahoo.com> wrote:
> Hi all,
>
> A couple of quick things.
>
> First, how do I do introspection in Pugs? CPAN's Perl6::Bible hasn't
> been updated in a while, but the various ways to get a list of methods
> (from
> http://search.cpan.org/dist/Perl6-Bible/lib/Perl6/Bible/S12.pod#Introspection
> or http://tinyurl.com/yxukar) don't appear to work. They all throw
> syntax errors or "No compatible subroutine" errors.
>
it seems much of this is unimplemented. from the pugs test dir, C<ack
--perl "HOW"> returns some tests, but most of them are decorated with
C<< :todo<feature> >>.

> Also, I'm having trouble with problem 7 in
> http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
> or http://tinyurl.com/tt9e7. Basically, it's flattening nested lists
> and I'm embarrassed to admit that I can't figure this out in Perl6.
> Thoughts? I've been reading synopses and grepping through Pugs, but to
> no avail.
>

* in index position flattens an array. it's mentioned in S02
(http://dev.perl.org/perl6/doc/design/syn/S02.html), among other
places. a quick test at http://run.pugscode.org/ suggests that it
works, too:

pugs> my @a=[1,2,[3,4],5]; say @a[*];
1 2 3 4 5

~jerry

Gaal Yahas

unread,
Dec 13, 2006, 3:55:28 AM12/13/06
to Ovid, perl6...@perl.org
On Tue, Dec 12, 2006 at 02:19:46PM -0800, Ovid wrote:
> First, how do I do introspection in Pugs? CPAN's Perl6::Bible hasn't
> been updated in a while, but the various ways to get a list of methods
> (from
> http://search.cpan.org/dist/Perl6-Bible/lib/Perl6/Bible/S12.pod#Introspection
> or http://tinyurl.com/yxukar) don't appear to work. They all throw
> syntax errors or "No compatible subroutine" errors.

In general you're better off looking at http://spec.pugscode.org/ for
more updated synopses, but in regard to introspection, the APIs aren't
well specced yet.

> Also, I'm having trouble with problem 7 in
> http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html
> or http://tinyurl.com/tt9e7. Basically, it's flattening nested lists
> and I'm embarrassed to admit that I can't figure this out in Perl6.
> Thoughts? I've been reading synopses and grepping through Pugs, but to
> no avail.

L<S04/"The do-once loop"/"A variant of do is gather"> stipulates the
results of a gather are flattened to a lazy list. I'm not sure how far
that flattenning goes, but one of these should do the trick, I think
(Pugs does not yet implement gather/take):

sub flatten1 (@list) {
gather for @list {
take $_;
}
}

sub flatten2 (@list) {
gather for @list {
take $_.does("List") ?? flatten2 $_ !! $_;
}
}


--
Gaal Yahas <ga...@forum2.org>
http://gaal.livejournal.com/

Larry Wall

unread,
Dec 13, 2006, 2:47:58 PM12/13/06
to perl6...@perl.org
On Wed, Dec 13, 2006 at 10:55:28AM +0200, Gaal Yahas wrote:
: L<S04/"The do-once loop"/"A variant of do is gather"> stipulates the

: results of a gather are flattened to a lazy list. I'm not sure how far
: that flattenning goes, but one of these should do the trick, I think

It would only flatten a recursive structure with the help of something
that recurses. The flattening of gather/take itself is only one level,
insofar as the various takes are treated as "pushes" onto the list
being gathered.

: (Pugs does not yet implement gather/take):

Actually, it does, but only the block form. The generalization to any
statement (using C<do> syntax) was a very recent change.

The following prints (1, 2, 3, 4, 5) in current pugs:

#!/usr/bin/pugs

my $a = [1,2,[3,4],5];

multi flattener ($x) {
take $x;
}
multi flattener (Array @array) {
for @array -> $elem {
flattener($elem);
}
}

sub flatten (*@args) {
for @args -> $arg {
return gather { flattener($arg) }
}
}

$a.flatten.perl.say;

Larry

0 new messages