I want to add meta-data to different items in a map, but I get an error in Clojure if I rry this with:
{:a
(with-meta
1
{:some-meta-tag "some-meta-data-value"}
)
}
: Is this possible?
In Clojure, metadata is used to annotate the data in a collection or for the data stored in a symbol. This is normally used to annotate data about types to the underlying compiler, but can also be used for developers. Metadata is not considered as part of the value of the object.
Metadata can be explained in a few ways: Data that provide information about other data. Metadata summarizes basic information about data, making finding & working with particular instances of data easier. Metadata can be created manually to be more accurate, or automatically and contain more basic information.
Metadata is important because it allows you to organize your data in a way that is meaningful to you and makes it easier to find the information you are looking for. It also helps to keep your data consistent and accurate.
List is a structure used to store a collection of data items. In Clojure, the List implements the ISeq interface. Lists are created in Clojure by using the list function.
I could be wrong, but think you can't attach metadata to a number:
user=> (with-meta 1 {:meta-tag "foo"})
java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IObj
From the docs
"Symbols and collections support metadata, a map of data about the symbol or collection."
This seemed to work:
user=> {:a (with-meta 'foo {:meta-tag "foo"})}
{:a foo}
And
user=> (meta (:a {:a (with-meta 'foo {:meta-tag "foo"})}))
{:meta-tag "foo"}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With