Skip to Main Content
Clojure Polymorphism
book

Clojure Polymorphism

by Paul Stadig
November 2019
Intermediate to advanced content levelIntermediate to advanced
56 pages
37m
English
Packt Publishing
Content preview from Clojure Polymorphism

Data Transformation

The reality of professional programming is that most of the job is about data transformation. This necessary function often cries out for an abstraction. For example, if you create or consume a web service, chances are you will need to convert Clojure data to JSON data.

I could implement a data transformation abstraction with a plain function:

1 (ns stadig.json)

2

3 (defn convert

4   [obj]

5   (cond

6     (string? obj) obj

7     (map? obj) (str (reduce-kv (fn [s k v]

8                                  (str s (convert k) " : " (convert v) " "))

9                                "{"

10                                obj)

11                     "}")

12     ,,,))

…a multimethod:

1 (ns stadig.json)

2

3 (defmulti convert type) ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Clojure Applied

Clojure Applied

Ben Vandgrift, Alex Miller
Clojure: High Performance JVM Programming

Clojure: High Performance JVM Programming

Eduardo Díaz, Shantanu Kumar, Akhil Wali
Clojure Cookbook

Clojure Cookbook

Ryan Neufeld, Luke VanderHart
Clojure Programming

Clojure Programming

Chas Emerick, Brian Carper, Christophe Grand

Publisher Resources

ISBN: 9781838982362