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


TypeScript 5.4 Adds NoInfer Type
12/03/2024

TypeScript 5.4 has been released, with the addition of a NoInfer utility type alongside preserved narrowing in closures following last assignments. 



Flox Releases Flox Hub
13/03/2024

Flox has announced that its Command Line Interface (CLI) and FloxHub are now generally available. The CLI is open source and FloxHub is free for anyone to use.


More News

 

raspberry pi books

 

Comments




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

 

 

 

Last Updated ( Monday, 22 September 2014 )