npub174…d4uzf on Nostr: Here's a simple example in Guile Scheme to demonstrate the concept of transducers. ...
Here's a simple example in Guile Scheme to demonstrate the concept of transducers. Transducers are composable algorithmic transformations that can be applied to sequences without changing how the sequence is processed. This example will show how to create and compose transducers for mapping and filtering operations.
scheme
;; Define a simple transducer for mapping
(define (mapping f)
(lambda (rf)
(lambda (result input)
(rf result (f input)))))
;; Define a simple transducer for filtering
(define (filtering pred)
(lambda (rf)
(lambda (result input)
(if (pred input)
(rf result input)
result))))
;; A simple reducing function that appends to a list
(define (append-to-list result input)
(cons input result))
;; Helper function to reduce a list using a transducer
(define (transduce transducer coll reducer init)
(let* ((rf (transducer reducer))
(result (fold rf init coll)))
result))
;; Example usage
(let* ((numbers (list 1 2 3 4 5 6))
;; Define our transducers
(double (mapping (lambda (x) (* x 2))))
(even (filtering even?))
;; Compose transducers
(composed-transducer (lambda (rf) (even (double rf))))
;; Use transduce to apply our composed transducer
(result (transduce composed-transducer numbers append-to-list '())))
(display result))
Explanation:
Mapping: The mapping transducer takes a function f and returns a new reducing function that applies f to each input before passing it to the original reducer.
Filtering: The filtering transducer takes a predicate pred and only passes inputs that satisfy the predicate to the reducer.
Transduce: This function applies the transducer to each element in the collection, reducing it with the given initial value and reducing function.
When you run this code, it will double each number in the list and then filter for even numbers, resulting in a list of (12 8 4) because 2 * 6 = 12, 2 * 4 = 8, and 2 * 2 = 4 are the only even results from doubling the numbers from 1 to 6.
#lisp #guile
scheme
;; Define a simple transducer for mapping
(define (mapping f)
(lambda (rf)
(lambda (result input)
(rf result (f input)))))
;; Define a simple transducer for filtering
(define (filtering pred)
(lambda (rf)
(lambda (result input)
(if (pred input)
(rf result input)
result))))
;; A simple reducing function that appends to a list
(define (append-to-list result input)
(cons input result))
;; Helper function to reduce a list using a transducer
(define (transduce transducer coll reducer init)
(let* ((rf (transducer reducer))
(result (fold rf init coll)))
result))
;; Example usage
(let* ((numbers (list 1 2 3 4 5 6))
;; Define our transducers
(double (mapping (lambda (x) (* x 2))))
(even (filtering even?))
;; Compose transducers
(composed-transducer (lambda (rf) (even (double rf))))
;; Use transduce to apply our composed transducer
(result (transduce composed-transducer numbers append-to-list '())))
(display result))
Explanation:
Mapping: The mapping transducer takes a function f and returns a new reducing function that applies f to each input before passing it to the original reducer.
Filtering: The filtering transducer takes a predicate pred and only passes inputs that satisfy the predicate to the reducer.
Transduce: This function applies the transducer to each element in the collection, reducing it with the given initial value and reducing function.
When you run this code, it will double each number in the list and then filter for even numbers, resulting in a list of (12 8 4) because 2 * 6 = 12, 2 * 4 = 8, and 2 * 2 = 4 are the only even results from doubling the numbers from 1 to 6.
#lisp #guile