Skip to content

Allow RESULT-TYPE arguments to reduction clauses

Phoebe Goldman requested to merge result-type into master

It’s always felt bad to me that the canonical way to get specialized numeric code in SUM, MULTIPLY, COUNTING and REDUCING loops was:

(iter (declare (declare-variables))
      (for i from 0 below 100)
      (summing i into (the fixnum result))
      (finally (return result)))

The mental and syntactic overhead of binding a name to the result and then explicitly returning it feels unnecessary, especially when compared to the RESULT-TYPE argument to COLLECT and friends.

This MR extends the reduction clauses COUNTING, SUM, MULTIPLY and REDUCE to accept an optional RESULT-TYPE argument. Those clauses’ behaviors are unchanged when RESULT-TYPE is not supplied. When RESULT-TYPE is supplied, it behaves so that the above example can be rewritten as:

(iter (declare (declare-variables))
      (for i from 0 below 100)
      (summing i result-type fixnum))

I’ve also taken the liberty of updating the manual to reflect this change, and writing a few simple tests to ensure that it behaves sanely.

Merge request reports