BT

Clojure 1.9 Brings Spec and More Language Features

| by Sergio De Simone Follow 9 Followers on Dec 11, 2017. Estimated reading time: 1 minute |

A note to our readers: You asked so we have developed a set of features that allow you to reduce the noise: you can get email and web notifications for topics you are interested in. Learn more about our new features.

As InfoQ previously reported, the most interesting new feature in the recently released Clojure 1.9 is Spec, which provides a standard and integrated system for the specification and testing of data and functions.

Leveraging previous work on other contract systems such as Racket’s, Spec aims to make it possible to automatically validate Clojure code, as well as to support a number of tasks such as generative testing, error reporting, destructuring and more. This is how you can specify a map, using spec/keys:

(spec/keys :req [::x ::y (or ::secret (and ::user ::pwd))]
           :opt [::z])

Keys used in a map specification are somewhat reminiscent of discriminated union tags as found in other languages, in that you define their types globally for a namespace:

(spec/def ::x integer?)
(spec/def ::y integer?)
(spec/def ::z integer?)
(spec/def ::secret string?)

This means that the same type specification is applied to occurrences of a given key in any map defined in the same module. If you want to use keys defined in a different namespace, you can use the new map namespace syntax:

#:types{ :x 1
         :y 2
         :secret "xyz" }

This is interpreted as


{ :types/x 1
  :types/y 2
  :types/secret "xyz }

While Clojure 1.9 is now stable, Spec is still in alpha state and the corresponding libraries must be included. Being alpha implies that API compatibility is not guaranteed.

To increase Spec expressiveness, Clojure 1.9 has added a number of new predicates, including boolean?, double?, simple-symbol, and many more. Still on the language front, clojure.core introduces the following new functions:

  • bounded-count, which allows to count the elements of a collection without realizing it beyond a bound.
  • swap-vals! and reset-vals!, two new atom functions that return both the old and new values.
  • halt-when, a transducer that ends transduction when a given predicate is satisfied.

Clojure 1.9 also introduces several performance enhancements, including optimized seq and destructuring, class derivation caching to speed up compilation, and others.

Clojure 1.9 may be installed using brew on macOS, and an install script on Linux. On Windows, you are still required to install Leiningen or Boot instead.

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