Ken Fox asked:

How about just

  (@foo,@bar,@zap) := classify [ rx/foo/, rx/bar/, rx/zap/ ] @source;

and implement classify as a normal sub?
We could certainly do that. But let's call it C<part>.

Et voilà:

	sub part ($classifier, *@list) {
	    my &classify := convert_to_sub($classifier);
	    my @parts;
	    for @list -> $nextval {
	        my $index = try{ classify($nextval) } // next;
		push @parts[$index], $nextval;
	    }
	    return @parts;
	}

	sub convert_to_sub ($classifier is topic) is cached {
	    when Code  { return $classifier }

	    when Array {
	        my @classifiers = map {convert_to_code($_)} @$classifier;
		return sub ($nextval) {
		    for @classifiers.kv -> $index, &test {
		        return $index if test($nextval);
		    }
		    return;
		}
	    }

	    when Hash  {
		my %classifiers = map { convert_to_code(.key) => .value } %$classifier;
		return sub ($nextval) {
		    my @indices = map { defined .key()($nextval) ?? .value :: () } %classifiers;
		    return @indices ?? any(@indices) :: undef;
		}
	    }

	    default    { croak "Invalid classifier (must be closure, array, or hash)" }
	}

	

But then the thousands of people who are apparently clamouring for this
functionality and who would have no hope of getting the above correct,
would have to pull in some module every time they wanted to partition an array.


> Why does everything have to be built into the first version of Perl 6?

Everything doesn't. Everything shouldn't be. Just the really common,
important stuff.

I have to confess though, there are *many* times I've wished for this particular
functionality as a built-in. Which is why I'm spending time on it now.

Damian

Reply via email to