Search code examples
rakufunction-object

how to pass a class method as argument to another method of the class in perl 6


I have a script like the below. Intent is to have different filter methods to filter a list.

Here is the code.

  2 
  3 class list_filter {
  4   has @.my_list = (1..20);
  5 
  6   method filter($l) { return True; }
  7 
  8   # filter method
  9   method filter_lt_10($l) {
 10     if ($l > 10) { return False; }
 11     return True;
 12   }
 13 
 14   # filter method
 15   method filter_gt_10($l) {
 16     if ($l < 10) { return False; }
 17     return True;
 18   }
 19 
 20   # expecting a list of (1..10) to be the output here
 21   method get_filtered_list_lt_10() {
 22     return self.get_filtered_list(&{self.filter_lt_10});
 23   }
 24 
 25   # private
 26   method get_filtered_list(&filter_method) {
 27     my @newlist = ();
 28     for @.my_list -> $l {
 29       if (&filter_method($l)) { push(@newlist, $l); }
 30     }
 31     return @newlist;
 32   }
 33 }
 34 
 35 my $listobj = list_filter.new();
 36 
 37 my @outlist = $listobj.get_filtered_list_lt_10();
 38 say @outlist;

Expecting [1..10] to be the output here. But getting following error.

Too few positionals passed; expected 2 arguments but got 1

  in method filter_lt_10 at ./b.pl6 line 9
  in method get_filtered_list_lt_10 at ./b.pl6 line 22
  in block <unit> at ./b.pl6 line 37

What am I doing wrong here?


Solution

  • Passing a method as a parameter in Perl 6 either requires you to use MOP (Meta-Object Protocol) methods, or pass the method by name (which would then do the lookup for you at runtime).

    But why use methods if you're not really doing something with the object in those methods? They might as well be subs then, which you can pass as a parameter.

    Perhaps this is best by example:

    class list_filter {
        has @.my_list = 1..20;  # don't need parentheses
    
        sub filter($ --> True) { } # don't need code, signature is enough
    
        # filter sub
        sub filter_lt_10($l) { not $l > 10 }
    
        # filter sub
        sub filter_gt_10($l) { not $l < 10 }
    
        # private
        method !get_filtered_list(&filter_sub) {
            @.my_list.grep(&filter_sub);
        }
    
        # expecting a list of (1..10) to be the output here
        method get_filtered_list_lt_10() {
            self!get_filtered_list(&filter_lt_10);
        }
    }
    
    my $listobj = list_filter.new();
    my @outlist = $listobj.get_filtered_list_lt_10();
    say @outlist; # [1 2 3 4 5 6 7 8 9 10]
    

    The first sub filter, which only returns a constant value (in this case True), can be represented much more easily in the signature with an empty body.

    The filter_lt_10 and filter_gt_10 subs only need the condition negated, hence the use of the not.

    The get_filtered_list method is supposed to be private, so make it a private method by prefixing !.

    In the get_filtered_list_lt_10 you now need to call get_filtered_list with a ! instead of a .. And you pass the filter_lt_10 sub as a parameter by prefixing the & (otherwise it would be considered a call to the sub without any parameters, which would fail).

    Change the get_filtered_listto use the built-in grep method: this takes a Callable block that takes a single parameter and which should return something True to include the value of the list it works upon. Since a sub taking a single parameter is a Callable, we can just specify the sub there directly.

    Hope this made sense. I tried to stay as close as possible to the intended semantics.

    Some general programming remarks: it feels to me that the naming of the subs is confusing: it feels to me that they should be called filter_le_10 and filter_ge_10, because that's really what they do it appears to me. Also, if you really don't want any ad-hoc filtering, but only filtering from a specific set of predefined filters, you would probably be better of by creating a dispatch table using constants or enums, and use that to indicate which filter you want, rather than encoding this information in the name of yet another method to make and maintain.

    Hope this helps.