BT

New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Clojure 1.7 Introduces Transducers, Improves Cross-platform Support

| by Sergio De Simone Follow 5 Followers on Jul 20, 2015. Estimated reading time: 1 minute |

Transducers and reader conditionals are the two most important new features in Clojure 1.7, says Cognitect’s Alex Miller. Transducers aim to enable composable algorithmic transformations on different kinds of collections, while reader conditionals can be used to improve Clojure portability across the JVM and JavaScript platforms.

Transducers

Transducers are reducing function transformers, that is functions that takes one reducing function and return another. A reducing function is just a function that can be used with reduce. Rich Hickey explained that the main advantage of transducers comes from their allowing to decouple three main concerns:

  • what the reducing function does;
  • the kind collection on which it is applied;
  • the inputs to reduce.

To support transducers, in Clojure 1.7 most sequence functions such as map, cat, filter, etc. add a new arity where the input collection is omitted. For example,

(def tr1 (map f))

will return a map transducer. Furthermore, transducers can be composed through the comp function:

(def xform (comp (filter odd?) (map inc)))

to provide a new transducer. Once you have a transducer, you can apply it in several ways, e.g. to some vector data:

(Sequence Xform data)

Using the transduce function instead of sequence, you would obtain the same result as applying reduce to data and the original composed function.

The main benefit of the example above is that it will just create one lazy sequence, not three as it would happen if simple functional composition would be used.

Reader conditionals

Reader conditionals can be useful in the context of .cljc files, which are a new format introduced by Clojure 1.7 that can be loaded by both Clojure and ClojureScript. This approach will make it possible to have libraries and applications that target multiple Clojure platforms with a single codebase, and reader conditionals provide a mechanism that will make it easier to deal with platform-specific code.

Analogously to a cond form, a reader conditional expression allows to specify which platform-specific code should be used on each platform. For example, the following expression:

[1 2 #?@(:clj [3 4] :cljs [5 6])]

will evaluate to [1 2 3 4] on Clojure, and to [1 2 5 6] on ClojureScript.

Besides transducers and reader conditionals, Clojure 1.7 includes many more new features and enhancements.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT