It's often quoted that
"It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures." — Alan Perlis
and this is something that is heavily observed in Clojure & it's libraries too e.g
In the gist here, Rich has explained the design principle of clojure.xml
where once the XML file is read, it is converted to a map and then you have all the functions available to manipulate these maps however you need and that you can reuse these functions with other maps
I'm having trouble understanding how these functions implemented to manipulate maps representing XML's to be used anywhere else?
I mean wouldn't the 100 functions I would've written be specific to the XML domain (i.e. specific to the map schema for XML), so the only reuse of these fn's would be if the map adheres to the same schema
Am I missing anything?
From the linked gist:
Look at the way
clojure.xml
processes XML - it deals with an external entity, a SAX parser fronting some XML source, gets the data out of it and into a map, and it's done. There aren't and won't be many more functions specific to XML. The world of functions for manipulating maps will now apply. And if it becomes necessary to have more XQuery-like capabilities in order to handle XML applications, they will be built for maps generally, and be available and reusable on all maps. Contrast this with your typical XML DOM, a huge collection of functions useful nowhere else.
No.
Because you would not be dealing with XML anymore. XML is merely a markup language. In other language ecosystems (say, Java) when someone refers to XML, it means the markup language AND (almost always) it's extensions and the tools built around them. The way Clojure (at least clojure.xml
) handles XML is to read the data and then continue processing it in Clojure's native maps.
It is worth noting (as of now) clojure.xml
contains only one function, parse
. This means it does not support serializing to XML, neither does it support in-place editing of XML data elements.