Luke Palmer wrote:

On Thu, Apr 03, 2003 at 07:29:37AM -0800, Austin Hastings wrote:


This has been alluded to before.

What would /A*B*/ produce?

Because if you were just processing the rex, I think you'd have to
finish generating all possibilities of A* before you began iterating
over B*...


The "proper" way would be to first produce all possibilities of length n before giving any possibility of length n+1.

''
'A'
'B'
'AA'
'AB'
'BB'
'AAA'
'AAB'
...

I haven't spent a milisecond of working out whether that's feasible to implement, but from a theoretical POV it seems like the solution.



Well, I'm not certain there is really a "proper" way. But sure, your way is doable.

use Permutations <<permutations compositions>>;

# Generate all strings of length $n
method Rule::Group::generate(Int $n) { # Type sprinkles :)
compositions($n, [EMAIL PROTECTED]) ==> map {
my @rets = map { $^atom.generate($^n)
} zip(@.atoms, $_);
*permutations([EMAIL PROTECTED])
}
}


How's that for A4 and A6 in a nutshell, implementing an A5 conept? :)
I hope I got it right....

Provided each other kind of rx element implemented generate, that
returned all generated strings of length $n, which might be zero.
This would be trivial for most other atoms and ops (I think).

Oh, compositions($a,$b) is a function that returns all lists of length
$b whose elements sum to $a.  Yes, it exists.

I have a couple syntax questions about this if anyone knows the answers:

$^atom.generate($^n)

I want @rets to be an array of array refs.  Do I have to explicitly
take the reference of that, or does that work by itself?

zip(@.atoms, $_)

I want the array ref in $_ to be zipped up with @.atoms as if $_ were
a real array.  If this I<is> correct, am I allowed to say:

zip(@.atoms, @$_)

for documentation?

Also, related to the first question:

*permutations([EMAIL PROTECTED])

Does that interpolate the returned list from permutations right into
the map's return, a la Perl5?  Do I need the * ?


As far all of these questions, I think the answer is related. I think the general question is "Is implicit flattening needed for perl6 builtins?" I think that the answer is no, or at least should be no, because it won't be hard to get the builtins to DWIM because of multimethods.

For instance, an implementation of map might be:

sub *map (&code, Array @array) {
   return @array.map(&code);
}

sub *map (&code, [EMAIL PROTECTED]) {
   my @ret;
   for @rest {
       @ret.push( &code.($_) );
   }
   return @ret;
}

So, given an Array/Array Subclass/Reference to one of the two as the
2nd argument to map, map would call the method version of map;
otherwise, the arguments after the code block are flattened and
looped over.

This behaivor should be consistant across all of the perl6 builtins.


Joseph F. Ryan [EMAIL PROTECTED]



Reply via email to