Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding with-meta data to clojure structures

Tags:

clojure

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?

like image 379
yazz.com Avatar asked Mar 23 '11 18:03

yazz.com


People also ask

What is Clojure metadata?

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.

How is metadata used?

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.

Why is metadata important?

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.

How do you define a list in Clojure?

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.


1 Answers

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"}
like image 81
michiakig Avatar answered Sep 22 '22 01:09

michiakig