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


Google Intensive AI Course - Free On Kaggle
05/11/2024

Google is offering a 5-Day Gen AI Intensive Course designed to equip data scientists with the knowledge and skills to tackle generative AI projects with confidence. It runs on the Kaggle platform from [ ... ]



Go At Highest Rank Ever in TIOBE Index
20/11/2024

Go is currently in 7th place in the TIOBE Index for November 2024. Not only is this is the highest position it has ever had, it's percentage rating is almost equal to its all-time-high. Will Go contin [ ... ]


More News

 

espbook

 

Comments




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

 

 

 

Last Updated ( Monday, 22 September 2014 )