|
From: | siiky |
Subject: | Re: New egg: CHICKEN Transducers |
Date: | Fri, 6 Jan 2023 14:11:26 +0000 |
On 1/5/23 06:11, siiky wrote: I think the easiest way for this would be to just zip the list together: (transduce list-fold (compose (zip-list lst2) (filter (lambda (p) (even? (* (car p) (cdr p)))))) collect-list lst1)
For one, that's not the same code. :p The SRFI 42 code I showed loops over all possible pairs, doesn't zip the inputs.
But the biggest problem with zipping like that, is that then the transducer abstraction breaks -- zip has to be implemented for each input data structure _pair_(?). With SRFI 42 I could also do this:
(list-ec (:list x lst) (:vector y vec) (if (even? (* x y))) (cons x y))
FYI: Clojure's `sequence` function accepts varargs like `map` and also a transducer which allows you to express this like so: (sequence (comp (map *) (filter odd?)) lst1 lst2) The docstring describes it like this: (...) I think you could offer the same API for `transduce`. Of course, you'll have to adjust the `map` transducer accordingly, too.
This wouldn't work very well with the current API in Scheme, because the fold is passed to transduce explicitly. And given that, either a restriction must be imposed on the input data structures (they must all be of the same type); or the folder of each input data structure must be given. If the latter something like this would be kinda awkward (though not impossible, I guess):
(transduce list-fold vector-fold (map *) collect-list lst vec)And accepting the optional sentinel with this new API would also be awkward to implement[^1]...
[^1]: This can be easily remied if the sentinel is given to the collect instead of the transduce, in which case the current def of transduce is a single case-lambda case instead of two:
(transduce fold (map *) (collect 'sentinel-value) data)
[Prev in Thread] | Current Thread | [Next in Thread] |