7.5.46.4 Transducers

Scheme Procedure: tmap proc

Returns a transducer that applies proc to all values. Stateless.

Scheme Procedure: tfilter pred?

Returns a transducer that removes values for which pred? returns #f.

Stateless.

Scheme Procedure: tremove pred?

Returns a transducer that removes values for which pred? returns non-#f.

Stateless

Scheme Procedure: tfilter-map proc

The same as (compose (tmap proc) (tfilter values)). Stateless.

Scheme Procedure: treplace mapping

The argument mapping is an association list (using equal? to compare keys), a hash-table, a one-argument procedure taking one argument and either producing that same argument or a replacement value.

Returns a transducer which checks for the presence of any value passed through it in mapping. If a mapping is found, the value of that mapping is returned, otherwise it just returns the original value.

Does not keep internal state, but modifying the mapping while it’s in use by treplace is an error.

Scheme Procedure: tdrop n

Returns a transducer that discards the first n values.

Stateful.

Scheme Procedure: ttake n

Returns a transducer that discards all values and stops the transduction after the first n values have been let through. Any subsequent values are ignored.

Stateful.

Scheme Procedure: tdrop-while pred?

Returns a transducer that discards the first values for which pred? returns true.

Stateful.

Scheme Procedure: ttake-while pred?
Scheme Procedure: ttake-while pred? retf

Returns a transducer that stops the transduction after pred? has returned #f. Any subsequent values are ignored and the last successful value is returned. retf is a function that gets called whenever pred? returns false. The arguments passed are the result so far and the input for which pred? returns #f. The default function is (lambda (result input) result).

Stateful.

Scheme Procedure: tconcatenate

tconcatenate is a transducer that concatenates the content of each value (that must be a list) into the reduction.

(list-transduce tconcatenate rcons '((1 2) (3 4 5) (6 (7 8) 9)))
⇒ (1 2 3 4 5 6 (7 8) 9)
Scheme Procedure: tappend-map proc

The same as (compose (tmap proc) tconcatenate).

Scheme Procedure: tflatten

tflatten is a transducer that flattens an input consisting of lists.

(list-transduce tflatten rcons '((1 2) 3 (4 (5 6) 7 8) 9)
⇒ (1 2 3 4 5 6 7 8 9)
Scheme Procedure: tdelete-neighbor-duplicates
Scheme Procedure: tdelete-neighbor-duplicates equality-predicate

Returns a transducer that removes any directly following duplicate elements. The default equality-predicate is equal?.

Stateful.

Scheme Procedure: tdelete-duplicates
Scheme Procedure: tdelete-duplicates equality-predicate

Returns a transducer that removes any subsequent duplicate elements compared using equality-predicate. The default equality-predicate is equal?.

Stateful.

Scheme Procedure: tsegment n

Returns a transducer that groups inputs into lists of n elements. When the transduction stops, it flushes any remaining collection, even if it contains fewer than n elements.

Stateful.

Scheme Procedure: tpartition pred?

Returns a transducer that groups inputs in lists by whenever (pred? input) changes value.

Stateful.

Scheme Procedure: tadd-between value

Returns a transducer which interposes value between each value and the next. This does not compose gracefully with transducers like ttake, as you might end up ending the transduction on value.

Stateful.

Scheme Procedure: tenumerate
Scheme Procedure: tenumerate start

Returns a transducer that indexes values passed through it, starting at start, which defaults to 0. The indexing is done through cons pairs like (index . input).

(list-transduce (tenumerate 1) rcons (list 'first 'second 'third))
⇒ ((1 . first) (2 . second) (3 . third))

Stateful.

Scheme Procedure: tlog
Scheme Procedure: tlog logger

Returns a transducer that can be used to log or print values and results. The result of the logger procedure is discarded. The default logger is (lambda (result input) (write input) (newline)).

Stateless.

Guile-specific transducers

These transducers are available in the (srfi srfi-171 gnu) library, and are provided outside the standard described by the SRFI-171 document.

Scheme Procedure: tbatch reducer
Scheme Procedure: tbatch transducer reducer

A batching transducer that accumulates results using reducer or ((transducer) reducer) until it returns a reduced value. This can be used to generalize something like tsegment:

;; This behaves exactly like (tsegment 4).
(list-transduce (tbatch (ttake 4) rcons) rcons (iota 10))
⇒ ((0 1 2 3) (4 5 6 7) (8 9))
Scheme Procedure: tfold reducer
Scheme Procedure: tfold reducer seed

A folding transducer that yields the result of (reducer seed value), saving its result between iterations.

(list-transduce (tfold +) rcons (iota 10))
⇒ (0 1 3 6 10 15 21 28 36 45)