Transducers Come To Clojure and Javascript
Written by Alex Denham   
Monday, 22 September 2014

Two new libraries have been released adding transducers to Clojure and to JavaScript

 

A transducer gives you a way to work on a collection of values to produce either a new value or a collection of new values. The name is derived by combining transform and reduce, and a transducer (used in the context of Clojure and JavaScipt) is a reducing function transformer.

A transducer transforms the data to produce some value from another, and reduces it in the sense of combining the values of a data structure to produce a new one.

The reduce function is the base transformation; any other transformation can be expressed in terms of it (map, filter, etc).

In a blog post describing what transducers do, Rich Hickey says transducers provide a better way of dealing with situations where you might need to create bespoke replicas of map, filter and mapcat.

Hickey says that a reducing function is just the kind of function you'd pass to reduce - it takes a result so far and a new input and returns the next result-so-far. In the context of transducers it's best to think about this as:

;;reducing function signature

whatever, input -> whatever

and that  a transducer is a function that takes one reducing function and returns another:

;;transducer signature

(whatever, input -> whatever) ->
                   (whatever, input -> whatever)

 

Hickey explains that the primary power of transducers comes from their fundamental decoupling and that they they don't care (or know about):

  • the 'job' being done (what the reducing function does)
  • the context of use (what 'whatever' is)
  • the source of inputs (where input comes from)

He explains these ideas in more detail in Anatomy of a Reducer on the Clojure blog.

The transducer support Hickey has added to Clojure on Github is being described as alpha and subject to change.

Meanwhile, inspired by the power of transducers, Mozilla webdev James Long has written his own version for JavaScript. On his blog describing the new library, Long describes transducers as mind-blowing, and says transducers.js lets you build transformations of data and apply it to any data type you could imagine.

It is an aproach worth finding out about if you need a new way to work with data. 

 

Banner


Pi Day - The Great Unanswered Questions
14/03/2024

It's Pi day again, again, again... Even after so many, I still have things to say about this most intriguing number. The most important things about Pi is that it is irrational and one of the few tran [ ... ]



Apache Lucene Adds Similarity Vector Searches
27/02/2024

Apache Lucene 9.10 has been released with support for similarity-based vector searches. Other improvements include block join compatible index sorting, and several improvements to ensure the software  [ ... ]


More News

 

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

 

 

Last Updated ( Monday, 22 September 2014 )