Skip to content

Commit

Permalink
Merge pull request JuliaLang#5086 from ivarne/docfix
Browse files Browse the repository at this point in the history
Added explanation and example for undefined associativity in reduce
  • Loading branch information
pao committed Dec 10, 2013
2 parents db251c9 + 030c9ee commit bec5ee3
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions doc/stdlib/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,18 @@ Iterable Collections

.. function:: reduce(op, v0, itr)

Reduce the given collection with the given operator, i.e. accumulate ``v = op(v,elt)`` for each element, where ``v`` starts as ``v0``. Reductions for certain commonly-used operators are available in a more convenient 1-argument form: ``maximum(itr)``, ``minimum(itr)``, ``sum(itr)``, ``prod(itr)``, ``any(itr)``, ``all(itr)``.

The associativity of the reduction is implementation-dependent; if you
need a particular associativity, e.g. left-to-right, you should write
your own loop.
Reduce the given collection ``ìtr`` with the given binary operator. Reductions
for certain commonly-used operators are available in a more convenient
1-argument form: ``maximum(itr)``, ``minimum(itr)``, ``sum(itr)``,
``prod(itr)``, ``any(itr)``, ``all(itr)``.

The associativity of the reduction is implementation-dependent. This means
that you can't use non associative operations like ``-`` because it is
undefined whether ``reduce(-,[1,2,3])`` should be evaluated as ``(1-2)-3``
or ``1-(2-3)``. This is because errors tend to accumulate, and it is often
better do reduction in groups e.g ```(a+b+...)+(c+d+...)`` if the chain is
long. Future versions of Julia might change the algorithm. Note that the
elements are not reordered if you use a ordered collection.

.. function:: maximum(itr)

Expand Down

0 comments on commit bec5ee3

Please sign in to comment.