mirror of
https://github.com/dlang/phobos.git
synced 2025-04-27 13:40:20 +03:00
Change documentation for reduce (#10707)
* reduce: Document Acc, Elem order in most visible place * Reduce doesn't enforce predicates * Reduce: Document behaviour on range with 1 elem * Doc: Remove references to implemententation detail Accumulator is the commonly used term for this concept.
This commit is contained in:
parent
12f99e5fff
commit
91af96cddb
1 changed files with 22 additions and 16 deletions
|
@ -4314,19 +4314,23 @@ Implements the homonym function (also known as `accumulate`, $(D
|
||||||
compress), `inject`, or `foldl`) present in various programming
|
compress), `inject`, or `foldl`) present in various programming
|
||||||
languages of functional flavor. There is also $(LREF fold) which does
|
languages of functional flavor. There is also $(LREF fold) which does
|
||||||
the same thing but with the opposite parameter order.
|
the same thing but with the opposite parameter order.
|
||||||
The call `reduce!(fun)(seed, range)` first assigns `seed` to
|
|
||||||
an internal variable `result`, also called the accumulator.
|
* Use `seed` to initialize an internal `accumulator`.
|
||||||
Then, for each element `x` in `range`, `result = fun(result, x)`
|
* For each element `e` in $(D range), evaluate `accumulator = fun(result, e)`.
|
||||||
gets evaluated. Finally, `result` is returned.
|
* Return $(D accumulator).
|
||||||
|
|
||||||
The one-argument version `reduce!(fun)(range)`
|
The one-argument version `reduce!(fun)(range)`
|
||||||
works similarly, but it uses the first element of the range as the
|
works similarly, but it uses the first element of the range as the
|
||||||
seed (the range must be non-empty).
|
seed (the range must be non-empty, else this throws).
|
||||||
|
|
||||||
|
If range has only one element, the functions are never invoked and
|
||||||
|
`result` and the seed is returned unchanged.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
the accumulated `result`
|
the accumulated result
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
fun = one or more functions
|
fun = one or more functions of the form `Acc function(Acc, ElemT)`
|
||||||
|
|
||||||
See_Also:
|
See_Also:
|
||||||
$(HTTP en.wikipedia.org/wiki/Fold_(higher-order_function), Fold (higher-order function))
|
$(HTTP en.wikipedia.org/wiki/Fold_(higher-order_function), Fold (higher-order function))
|
||||||
|
@ -4808,26 +4812,28 @@ languages of functional flavor, iteratively calling one or more predicates.
|
||||||
$(P Each predicate in `fun` must take two arguments:)
|
$(P Each predicate in `fun` must take two arguments:)
|
||||||
* An accumulator value
|
* An accumulator value
|
||||||
* An element of the range `r`
|
* An element of the range `r`
|
||||||
$(P Each predicate must return a value which implicitly converts to the
|
$(P Each function must return a value which implicitly converts to the
|
||||||
type of the accumulator.)
|
type of the accumulator.)
|
||||||
|
|
||||||
$(P For a single predicate,
|
$(P For a single function,
|
||||||
the call `fold!(fun)(range, seed)` will:)
|
the call `fold!(fun)(range, seed)` will:)
|
||||||
|
|
||||||
* Use `seed` to initialize an internal variable `result` (also called
|
* Use `seed` to initialize an internal `accumulator`.
|
||||||
the accumulator).
|
* For each element `e` in $(D range), evaluate `accumulator = fun(result, e)`.
|
||||||
* For each element `e` in $(D range), evaluate `result = fun(result, e)`.
|
* Return $(D accumulator).
|
||||||
* Return $(D result).
|
|
||||||
|
|
||||||
$(P The one-argument version `fold!(fun)(range)`
|
$(P The one-argument version `fold!(fun)(range)`
|
||||||
works similarly, but it uses the first element of the range as the
|
works similarly, but it uses the first element of the range as the
|
||||||
seed (the range must be non-empty) and iterates over the remaining
|
seed (the range must be non-empty) and iterates over the remaining
|
||||||
elements.)
|
elements.)
|
||||||
|
|
||||||
Multiple results are produced when using multiple predicates.
|
Multiple results are produced when using multiple functions.
|
||||||
|
|
||||||
|
If range has only one element, the functions are never invoked and
|
||||||
|
`result` and the seed is returned unchanged.
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
fun = the predicate function(s) to apply to the elements
|
fun = one or more functions of the form `Acc function(Acc, ElemT)`
|
||||||
|
|
||||||
See_Also:
|
See_Also:
|
||||||
* $(HTTP en.wikipedia.org/wiki/Fold_(higher-order_function), Fold (higher-order function))
|
* $(HTTP en.wikipedia.org/wiki/Fold_(higher-order_function), Fold (higher-order function))
|
||||||
|
@ -4918,7 +4924,7 @@ This function is also known as
|
||||||
$(HTTP mathworld.wolfram.com/CumulativeSum.html, Cumulative Sum).
|
$(HTTP mathworld.wolfram.com/CumulativeSum.html, Cumulative Sum).
|
||||||
|
|
||||||
Params:
|
Params:
|
||||||
fun = one or more functions to use as fold operation
|
fun = one or more functions of the form `Acc function(Acc, ElemT)`
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The function returns a range containing the consecutive reduced values. If
|
The function returns a range containing the consecutive reduced values. If
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue