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


Data Wrangler Gets Copilot Integration
11/11/2024

Microsoft has announced that Copilot is being integrated into Data Wrangler. The move will give data scientists the ability to use natural language to clean and transform data, and to get help with fi [ ... ]



OpenAI Library For .NET Exits Beta
19/11/2024

A few months ago the OpenAI .NET library was released as a beta. It has now reached version 2.0.0 and the time has come to leave beta and, with a few amendments enter production readiness.


More News

 

espbook

 

Comments




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

 

 

 

Last Updated ( Monday, 22 September 2014 )