Em Seg, 2010-03-08 às 12:45 -0800, Little Walker escreveu:
> I've been looking around to see if there's been any discussion of
> introducing functional programming-style pattern matching for method/
> function dispatch.  Could someone point me to any such discussions?

a Tree matching language is on discussion for about three years already
(I remember discussing this during YAPC::EU 2007). We have considered
several things including XPath/XSLT but haven't come to any viable
conclusion.

> This is a contrived example of what I'm referring to:
> sub traverse([Leaf $a]) {
>   # do something
> }
> sub traverse([Tree $left, Tree $right]) {
>   traverse($left);
>   traverse($right);
> }
> my $t = Tree(...);
> traverse($t);

You seem to refer to that as if it wasn't supported by Perl 6, but your
code is almost - except for the Tree initialization code, that probably
should look like Tree.new(...) and you also need to replace "sub" by
"multi".

One other aspect is map/grep/reduce with varying-arity multies which
would allow some very interesting things - The example is how to reduce
a list of tokens for a URL to the action processing (I've implemented
something in this line in http://github.com/ruoso/faz)

multi handle(HTTP::Request $r) {
   return RootAction.new(:req($r), :code({ 
      # here the code to process this action.
   });
}

multi handle(RootAction $a, $category_name) {
   return Action.new(:name</$category>, :outer($a), :code({ 
      # here the code to process this action.
      # $category_name is visible in the closure
   });
}

multi handle(Action $a where { .name eq '/$category' }, $post) {
   return Action.new(:name</$category/$post>, :outer($a), :code({
      # here the code to handle this action,
      # $post is visible in the closure
   });
}

my @tokens = ($request, 'perl', 'tree_matching');
my $action = @tokens.reduce: &handle;
$action.execute;


This will get even better as custom grammars get into the play...

daniel

Reply via email to