rebellion icon indicating copy to clipboard operation
rebellion copied to clipboard

Batching, but only once

Open jackfirth opened this issue 5 years ago • 1 comments

Transducers are strictly more expressive than reducers, so it should be possible to lift a reducer into an equivalent transducer. The batching transducer constructor kind of does this, but it repeatedly applies the reducer. The more direct approach would be to apply the reducer only once, either allowing or disallowing empty reduction. Signature:

(reducing [red reducer?]) -> transducer?
(reducing-nonempty [red reducer?]) -> transducer?

Examples:

> (transduce (list 1 2 3) (reducing into-sum) #:into into-list)
(list 6)
> (transduce (list) (reducing into-sum) #:into into-list)
(list 0)
> (transduce (list 1 2 3) (reducing-nonempty into-sum) #:into into-list)
(list 6)
> (transduce (list) (reducing-nonempty into-sum) #:into into-list)
(list)

Note that batching can be implemented in terms of reducing-nonempty and repeating (from #337):

(define (batching batch-reducer)
  (repeating (reducing-nonempty batch-reducer)))

jackfirth avatar Nov 05 '19 04:11 jackfirth

I can't think of a use case for reducing, it seems to me every use case wants reducing-nonempty instead. So to keep the API simpler I'll only provide one reducing function that requires a nonempty reducer, and not bother adding an alternative that works for empty reducers.

jackfirth avatar Nov 05 '19 07:11 jackfirth