Revision 7360ada07430b1b1dc023edabb67915b445d0a2d authored by Joshua J. Cogliati on 27 September 2019, 15:10:12 UTC, committed by Joshua J. Cogliati on 10 October 2019, 02:46:54 UTC
1 parent 0e21c1d
reduce
REDUCE template data (library procedure)
outputs the result of applying the template to accumulate the
members of the data input. The template must be a two-slot
function. Typically it is an associative function name like SUM.
If the data input has only one constituent (member in a list or
character in a word), the output is that consituent. Otherwise,
the template is first applied with ?1 filled with the next-to-last
consitient and ?2 with the last constituent. Then, if there are
more constituents, the template is applied with ?1 filled with the
next constituent to the left and ?2 with the result from the
previous evaluation. This process continues until all constituents
have been used. The data input may not be empty.
Note: If the template is, like SUM, the name of a procedure that is
capable of accepting arbitrarily many inputs, it is more efficient
to use APPLY instead of REDUCE. The latter is good for associative
procedures that have been written to accept exactly two inputs:
to max :a :b
output ifelse :a > :b [:a] [:b]
end
print reduce "max [...]
Alternatively, REDUCE can be used to write MAX as a procedure
that accepts any number of inputs, as SUM does:
to max [:inputs] 2
if emptyp :inputs ~
[(throw "error [not enough inputs to max])]
output reduce [ifelse ?1 > ?2 [?1] [?2]] :inputs
end
![swh spinner](/static/img/swh-spinner.gif)
Computing file changes ...