Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improves explanation of reduce and checks examples
As a matter of fact, #1399 is no longer relevant or has been
solved. So it closes #1399
  • Loading branch information
JJ committed May 29, 2019
1 parent 1a11797 commit 6c0e8cb
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions doc/Type/List.pod6
Expand Up @@ -831,13 +831,16 @@ say <01 11 111 2 20 02>.sort( { .Int, .comb.sum, .Str } );
Defined as:
multi sub reduce(&with, *@values)
multi method reduce(List:D: &with)
multi method reduce(Any:U: & --> Nil)
multi method reduce(Any:D: &with)
multi sub reduce (&with, +list)
Generates a single "combined" value from a list of arbitrarily many of values,
by iteratively applying a function which knows how to combine I<two> values.
The first form is obviously a no-op. The second form generates a single
"combined" value from a list of arbitrarily many values, by iteratively
applying a function which knows how to combine I<two> values.
If C<@values> contains just a single element, the operator is applied to that single element if possible; if not, it returns the element itself.
If C<@values> contains just a single element, the operator is applied to that
single element if possible; if not, it returns the element itself.
say [-] <10 5 3>; #OUTPUT: 2␤
say [-] 10; #OUTPUT: 10␤
Expand Down Expand Up @@ -877,8 +880,9 @@ metaoperator provides a syntactic shortcut:
say reduce &[+], @numbers; # operator does not need explicit identity
say [+] @numbers; # most people write it this way
Since C<reduce> is an implicit loop, it responds to C<next>, C<last> and
C<redo> statements inside C<&with>:
Please note also the use of C<reduce> in sub form. Since C<reduce> is an
implicit loop, it responds to C<next>, C<last> and C<redo> statements inside
C<&with>:
say (2,3,4,5).reduce: { last if $^a > 7; $^a + $^b }; # says 9
Expand Down

0 comments on commit 6c0e8cb

Please sign in to comment.