array-reduce Bradley Lucier 18 Jan 2020 20:45 UTC

One use case for array-reduce over array-fold is that, theoretically,
array-reduce can be parallelized, and array-fold, in general, cannot.

When operating on lists (or any one-dimensional data structure, really),
it's easy to write reduce in terms of fold,

(reduce sum list) => (fold sum (car list) (cdr list))

because stripping the first element off a list gives you another list.

Conceptually, that doesn't quite work for arrays (cut the top left cell
out of a spreadsheet, you don't get another spreadsheet), so the best I
could come up with for reduce is roughly

      (let ((box '())
            (A_ (array-getter A)))
        (interval-for-each
         (lambda args
           (if (null? box)
               (set! box (list (apply A_ args)))
               (set-car! box (sum (car box)
                                  (apply A_ args)))))
         (array-domain A))
        (car box))

I don't particularly like this code.  Does anyone have a better suggestion?

Thanks.

Brad