InfoQ Homepage News Clojure 1.7 Introduces Transducers, Improves Cross-platform Support

# Clojure 1.7 Introduces Transducers, Improves Cross-platform Support

This item in japanese

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 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.

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.

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

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

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

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.