Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

[Perl6] Can i put a operator in container

by freakcoco (Sexton)
on Jul 10, 2017 at 16:16 UTC ( [id://1194724]=perlquestion: print w/replies, xml ) Need Help??

freakcoco has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk, When I look at the perl6 learning resources, I saw this "Operators are simply specially-named multi subs" in "PERL 6 ADVENT CALENDAR"
how to put it in value or pass it to function

my Sub @array = ( &one , &two, &three ) sub high_order_function ( Sub $op ) { $op() } # ? my Operator @array = ( cmp , eqv , gdb ) sub high_order_function ( Operator $op ) { $op() }

Replies are listed 'Best First'.
Re: [Perl6] Can i put a operator in container (Perl 6)
by Laurent_R (Canon) on Jul 10, 2017 at 17:21 UTC
    This is how you could define a "hash subtraction" operator, i.e. an operator that returns the set difference between two hashes:
    multi sub infix:<-> (%main, %dict) { my %difference; for %main.keys -> $word { %difference{$word} = 1 unless %dict{$word}:exists; } return %difference; }
    You can now use the minus operator too compute the set differences between the two hashes.

    Update: I did not have time to complete when I wrote the above.

    Now, assuming you have a %book-words hash containing all the different words in a given book, and a %dictionary hash, you could build a hash with all the words in the book and not in the dictionary:

    my %missing-words = %book-words - %dictionary;
    Perl 6 does not define the minus operator between hashes, but it can easily be added to the language as shown above, and Perl 6 won't get confused.
      Please note that my book on Perl 6, Think Perl 6, is freely available in PDF format: http://greenteapress.com/wp/think-perl-6/.

      You'll see explanations and examples on constructing new operators in section 11.10 (the example above is from this section) and subsection 14.2.6.

      Now I know that that when I work with data Structure, custom operators is my friend.
      But there are still a type of operator i cannot create.
      hyper operators «», reduce [], zip Z (Ex: «eqv» [ lcm ] Z- .....).
      How to create them ?

        Those are basically all meta ops, ops that operate on other ops.

        I can interpret your question in a couple ways.

        Creating new meta ops

        Iirc Larry has said there may one day (years from now) be a simple and convenient official way to add new meta ops without doing explicit direct modifications of the main grammar. But I'm pretty sure there's currently no official way to create new meta operators.

        In the meantime it might be reasonable for someone into -Ofun culture to create a slang that adds a meta op.

        Slangs are not officially supported. I can't even link to decent currently correct unofficial documentation. I'll just summarize them here.

        The term "slang" is short for "sub-language". The full P6 language consists of a collection (called a "language braid") of slangs that can call into, and modify, each others' parsing and/or post-parse-processing. A slang consists of a grammar/actions pair. A grammar is either a special type of class that contains parsing declarations or a role that gets mixed into an existing grammar. An actions class is either a class that contains AST generation or manipulation methods or a role that gets mixed into an existing actions class.

        Here's a toy slang demo that just tweaks parsing of identifiers.

        Creating/naming ops that are aliases for metaop / op combinations

        Is this code of interest?

        sub op { @^lhs Z+ @^rhs }; say (1,2) [&op] (3,4); # (4 6)

        This creates a function named op that is invoked using either op( arg1, arg2 ) or op arg1, arg2. The function applies the infix metaop Z combined with the infix op + to its left and right arguments. The next line uses this function via another infix metaop [...] where the ... is a named (not called) binary arity routine.

        Or:

        my &infix:<op> = { @^lhs Z+ @^rhs }; say (1,2) op (3,4); # (4 6)

        This creates an infix operator named op. It does the same thing as the op function we created earlier but can be referred to in infix position as just op. (To bring things full circle, you could also write infix:<op>( arg1, arg2 ) or infix:<op> arg1, arg2.)

        Or, simplifying / complexifying further:

        my &infix:<op> = &[Z+]; say (1,2) op (3,4); # (4 6)

        The syntax &[...] parses the ... part as an infix operator and returns the resulting routine.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: [Perl6] Can i put a operator in container
by raiph (Deacon) on Jul 11, 2017 at 04:15 UTC
    Hi freakcoco,

    Is this the sort of answer you're looking for?

    my Sub @array = ( &infix:<cmp>, &infix:<eqv>, &infix:<gcd>); sub hof ( &foo ) { foo(3,42) } my &op = &infix:<gcd>; say hof &op; # 3

    Hth.

      That's what i want, thank

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1194724]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found