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) ...
Get Clojure Polymorphism now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.