Skip to content

Commit

Permalink
Metaoperator normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Jun 14, 2018
1 parent ad8e495 commit c53ef11
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 47 deletions.
16 changes: 8 additions & 8 deletions doc/Language/haskell-to-p6.pod6
Expand Up @@ -4,11 +4,11 @@
=SUBTITLE Learning Perl 6 from Haskell, in a nutshell: What do I already know?
Haskell and Perl 6 are I<very> different languages. This is obvious. However,
that does not mean there are not similarities or shared ideas! This page
attempts to get a Haskell user up and running with Perl 6. The Haskell user may
find that they need not abandon all of their Haskelly thoughts while scripting
in Perl 6.
Haskell and Perl 6 are I<very> different languages. This is obvious.
However, that does not mean there are not similarities or shared ideas!
This page attempts to get a Haskell user up and running with Perl 6. The
Haskell user may find that they need not abandon all of their Haskelly
thoughts while scripting in Perl 6.
Note that this should not be mistaken for a beginner tutorial or overview of
Perl 6; it is intended as a technical reference for Perl 6 learners with a
Expand Down Expand Up @@ -439,8 +439,8 @@ Fold in Haskell is called Reduce in Perl 6.
@numbers.reduce({$^a + $^b}, with => 0)
=end code
However, in Perl 6, if you want to use an infix operator (+ - / % etc) there is a nice little
helper called the Reduction Metaoperator.
However, in Perl 6, if you want to use an infix operator (+ - / % etc)
there is a nice little helper called the Reduction metaoperator.
=begin code
my @numbers = {...};
Expand All @@ -459,7 +459,7 @@ associativity attached to the operator/subroutine.
# you can use a subroutine as an infix operator
say 'a' [&two-elem-list] 'b'; # (a b)
# as the reduction prefix meta operator takes an infix operator, it will work there too;
# as the reduction prefix metaoperator takes an infix operator, it will work there too;
[[&two-elem-list]] 1..5; # ((((1 2) 3) 4) 5)
say (1..5).reduce: &two-elem-list; # ((((1 2) 3) 4) 5)
Expand Down
51 changes: 26 additions & 25 deletions doc/Language/operators.pod6
Expand Up @@ -116,15 +116,16 @@ L<#prefix ...> etc. stub operators.
Defining custom operators is covered in
L<Defining Operators functions|/language/functions#Defining_Operators>.
=head1 Meta Operators
=head1 Metaoperators
Meta operators can be parameterized with other operators or subroutines in the
same way as functions can take functions as parameters. To use a subroutine as
a parameter, prefix its name with a C<&>. Perl 6 will generate the actual
combined operator in the background, allowing the mechanism to be applied to
user defined operators. To disambiguate chained meta operators, enclose the
inner operator in square brackets. There are quite a few Meta operators with
different semantics as explained, next.
Metaoperators can be parameterized with other operators or subroutines
in the same way as functions can take functions as parameters. To use a
subroutine as a parameter, prefix its name with a C<&>. Perl 6 will
generate the actual combined operator in the background, allowing the
mechanism to be applied to user defined operators. To disambiguate
chained metaoperators, enclose the inner operator in square brackets.
There are quite a few metaoperators with different semantics as
explained, next.
=head1 Substitution Operators
Expand Down Expand Up @@ -251,7 +252,7 @@ Although not strictly operators, methods can be used in the same fashion.
$a .= round; # RESULT: «3»
=head1 Negated Relational Operators
X<|! (negation meta operator)>X<|!==>X<|!eq>
X<|! (negation metaoperator)>X<|!==>X<|!eq>
The result of a relational operator returning C<Bool> can be negated by
prefixing with C<!>. To avoid visual confusion with the C<!!> operator,
Expand All @@ -268,10 +269,10 @@ There are shortcuts for C<!==> and C<!eq>, namely C<!=> and C<ne>.
say so $release !before $today; # OUTPUT: «False␤»
=head1 Reversed Operators
X<|R,reverse meta operator>
X<|R,reverse metaoperator>
Any infix operator may be called with its two arguments reversed by prefixing
with C<R>. Associativity of operands is reversed as well.
Any infix operator may be called with its two arguments reversed by
prefixing with C<R>. Associativity of operands is reversed as well.
say 4 R/ 12; # OUTPUT: «3␤»
say [R/] 2, 4, 16; # OUTPUT: «2␤»
Expand All @@ -290,7 +291,7 @@ list until all elements of the longer list are processed.
say (1, 2, 3, 4) »~» <a b>; # OUTPUT: «(1a 2b 3a 4b)␤»
say (1, 2, 3) »+« (4, 5, 6); # OUTPUT: «(5 7 9)␤»
Assignment meta operators can be I<hyped>.
Assignment metaoperators can be I<hyped>.
my @a = 1, 2, 3;
say @a »+=» 1; # OUTPUT: «[2 3 4]␤»
Expand Down Expand Up @@ -381,7 +382,7 @@ You can chain hyper operators to destructure a List of Lists.
say $neighbors »>>+<<» ($p, *); # OUTPUT: «((1 3) (2 2) (2 4) (3 3))␤»
=head1 Reduction Operators
X<|[] (reduction meta operators)>X<|[+] (reduction meta operators)>
X<|[] (reduction metaoperators)>X<|[+] (reduction metaoperators)>
The reduction metaoperator, C<[ ]>, reduces a list with the given infix
operator. It gives the same result as the L<reduce> routine - see there for
Expand Down Expand Up @@ -412,7 +413,7 @@ quote it with C<[]> (e.g. C<[\[\x]]>).
say @n[^5]; # OUTPUT: «(1 12 123 1234 12345)␤»
=head1 Cross Operators
X<|X (cross meta operator)>
X<|X (cross metaoperator)>
The cross metaoperator, C<X>, will apply a given infix operator in order of
cross product to all lists, such that the rightmost operator varies most
Expand All @@ -421,7 +422,7 @@ quickly.
1..3 X~ <a b> # RESULT: «<1a, 1b, 2a, 2b, 3a, 3b>␤»
=head1 Zip Operators
X<|Z (zip meta operator)>
X<|Z (zip metaoperator)>
The zip metaoperator (which is not the same thing as L<Z|#infix_Z>) will
apply a given infix operator to pairs taken one left, one right, from its
Expand All @@ -441,16 +442,16 @@ If an infix operator is not given, C<,> (comma operator) will be used by default
my @l = 1 Z 2; # RESULT: «[(1 2)]»
=head1 Sequential Operators
X<|S,sequential meta operator>
X<|S,sequential metaoperator>
The sequential metaoperator, C<S>, will suppress any concurrency or reordering
done by the optimizer. Most simple infix operators are supported.
say so 1 S& 2 S& 3; # OUTPUT: «True␤»
=head1 Nesting of Meta Operators
=head1 Nesting of Metaoperators
To avoid ambiguity when chaining meta operators, use square brackets to help the
To avoid ambiguity when chaining metaoperators, use square brackets to help the
compiler understand you.
my @a = 1, 2, 3;
Expand Down Expand Up @@ -2220,7 +2221,7 @@ values for all C<Z> operators in a chain.
}
# OUTPUT: «a:1␤b:2␤c:3␤»
The C<Z> operator also exists as a meta operator, in which case the inner
The C<Z> operator also exists as a metaoperator, in which case the inner
lists are replaced by the value from applying the operator to the
list:
Expand All @@ -2239,9 +2240,8 @@ elements vary most rapidly:X<|cross product operator>
# (2 a 9) (2 b 9) (2 c 9)
# (3 a 9) (3 b 9) (3 c 9))
The C<X> operator also exists as a meta operator, in which case the inner
lists are replaced by the value from applying the operator to the
list:
The C<X> operator also exists as a metaoperator, in which case the inner
lists are replaced by the value from applying the operator to the list:
1..3 X~ <a b c> X~ 9
# produces (1a9 1b9 1c9 2a9 2b9 2c9 3a9 3b9 3c9)
Expand Down Expand Up @@ -2348,8 +2348,9 @@ object.
If type constrains on variables or containers are present a type check will be
performed at runtime. On failure C<X::TypeCheck::BindingType> will be thrown.
Please note that C<:=> is a compile time construct. As such it can not be
referred to at runtime and thus can't be used as an argument to meta operators.
Please note that C<:=> is a compile time construct. As such it can not
be referred to at runtime and thus can't be used as an argument to
metaoperators.
=head2 infix C«::=»
Expand Down
9 changes: 5 additions & 4 deletions doc/Language/testing.pod6
Expand Up @@ -5,9 +5,9 @@
=SUBTITLE Writing and running tests in Perl 6
Testing code is an integral part of software development.
Tests provide automated, repeatable
verifications of code behaviour, and ensures your code works as expected.
Testing code is an integral part of software development. Tests provide
automated, repeatable verifications of code behaviour, and ensures your
code works as expected.
In Perl 6, the L<Test|https://github.com/rakudo/rakudo/blob/master/lib/Test.pm6>
module provides a testing framework. Perl 6's official spectest suite uses C<Test>.
Expand Down Expand Up @@ -466,7 +466,8 @@ user-defined infix.
cmp-ok 'my spelling is apperling', '~~', /perl/, "bad speller";
Meta operators cannot be given as a string; pass them as a L<Callable> instead:
Metaoperators cannot be given as a string; pass them as a L<Callable>
instead:
cmp-ok <a b c>, &[!eqv], <b d e>, 'not equal';
Expand Down
10 changes: 5 additions & 5 deletions doc/Language/traps.pod6
Expand Up @@ -4,10 +4,10 @@
=SUBTITLE Traps to avoid when getting started with Perl 6
When learning a programming language, possibly with the background of being
familiar with another programming language, there are always some things
that can surprise you and might cost valuable time in debugging and
discovery.
When learning a programming language, possibly with the background of
being familiar with another programming language, there are always some
things that can surprise you and might cost valuable time in debugging
and discovery.
This document aims to show common misconceptions in order to avoid them.
Expand Down Expand Up @@ -1684,7 +1684,7 @@ it returns a list with L<Bool> values for each requested
lookup. Non-empty lists always give C<True> when you L<Bool>ify them,
so the check always succeeds no matter what keys you give it.
=head2 Using C<[…]> meta-operator with a list of lists
=head2 Using C<[…]> metaoperator with a list of lists
Every now and then, someone gets the idea that they can use C<[Z]> to
create the transpose of a list-of-lists:
Expand Down
11 changes: 6 additions & 5 deletions doc/Type/List.pod6
Expand Up @@ -880,7 +880,7 @@ for operators which aren't left-associative:
(2-3-4).say; # OUTPUT: «-5␤»
Since reducing with an infix operator is a common thing to do, the C<[ ]>
meta-operator provides a syntactic shortcut:
metaoperator provides a syntactic shortcut:
# The following all do the same thing...
my @numbers = (1,2,3,4,5);
Expand Down Expand Up @@ -946,7 +946,7 @@ for operators which aren't left-associative:
say produce &[-], (2,3,4); # OUTPUT: «(2 -1 -5)␤»
say [\-] (2,3,4); # OUTPUT: «(2 -1 -5)␤»
A triangle meta-operator C<[\ ]> provides a syntactic shortcut for
A triangle metaoperator C<[\ ]> provides a syntactic shortcut for
producing with an infix operator:
# The following all do the same thing...
Expand Down Expand Up @@ -1130,7 +1130,8 @@ to apply to each of the cross product items.
say cross([1, 2, 3], [4, 5, 6], :with(&infix:<*>)).join(",");
# OUTPUT: «4,5,6,8,10,12,12,15,18␤»
The C<X> operator can be combined with another operator as a meta-operator to perform a reduction as well:
The C<X> operator can be combined with another operator as a
metaoperator to perform a reduction as well:
say ([1, 2, 3] X* [4, 5, 6]).join(",")
# same output as the previous example
Expand Down Expand Up @@ -1186,8 +1187,8 @@ single list of products.
# 8␤
# 27␤»
The C<Z> form can also be used to perform reduction by implicitly setting the
C<with> parameter with a meta-operator :
The C<Z> form can also be used to perform reduction by implicitly
setting the C<with> parameter with a metaoperator :
.say for <1 2 3> Z* [1, 2, 3] Z* (1, 2, 3); # same output
Expand Down

0 comments on commit c53ef11

Please sign in to comment.