You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you love functools.reduce, you're in the right place!
Transducers are reduce on steroids.
Overview
Transducers are a collection of techniques for transforming sequential data.
A regular function is sometimes abbreviated as fn.
A transducing fn is sometimes abbreviated as xfn, which is
where this library derives its name.
Transducers were invented by Rich Hickey and introduced in Clojure v1.7.
However, they are a general technique akin to map, filter, and reduce.
The code here is a transcription of the code and API.
Status
Alpha.
All functions believed to be working, but no tests yet.
API subject to change, specifically the signature for transduce,
pending user feedback.
Functions that use transducers
This library features two functions that use transducers:
fn
Description
transduce
Passes items through a transducer then aggregates them
eduction
a generator that lazily pass items through a transducer
Transducers - the toolkit
xfn
Description
cat
Concatenate lists on the fly
distinct
Remove duplicates on the fly
drop
drop the first n items from the sequence
drop_while
drop items while a condition exists
filter
only keep items that match a certain criteria
halt_when
stop processing items when an item is encountered that meets some criteria
interpose
insert an element of your choice between items
keep_indexed
filter based on index and value
map
transform items on the fly
map_indexed
transform items based on index and value
mapcat
transform items and concatenate the results on the fly
partition_all
group items by a chunk size
partition_by
group sequences of item matching a criteria
random_sample
include result based on probability threshold
remove
opposite of filter, remove items matching some criteria
take
stop after processing a certain number of items
take_nth
only process every n items
take_while
continue processing while some criteria is met, then stop
Helper functions
fn
description
comp
combine xfns together into a reusable data processing pipeline
reduced
call this on an item to return immediately from the transducer
Quickstart
eduction, map, filter, and comp
eduction used with map is the easiest way to get a feel for transducers.
(Note:from xfn import * overloads the builtin map function so that
if acts as a transducer if given only one argument. If given more than one
argument, it calls builtins.map)
Use filter to remove items from the final output.
(Note:from xfn import * overloads the builtin filter function so that
if acts as a transducer if given only one argument. If given more than one
argument, it calls builtins.filter)
The following examples will show how the transducers reshape data.
For brevity, the transducers will be in a table with their name, an explanation,
and input/output examples. For instance: