简体   繁体   中英

Atoms have Properties in Maclisp. How to do the same in Clojure?

Quoting from the (1975) Maclisp Reference Manual: "Each atomic symbol has associated with it a property-list, which can be retrieved with the plist function."

A Maclisp property-list was a list of 'indicator/value' pairs. In Maclisp,

(get x y)

gets x's y-property.

(putprop x 'banana y)

sets x's y-property to banana.

I am tasked with converting a lot of old Maclisp code into Clojure. I am new to Clojure but won't be for long as this project unfolds. Before I run off and write something myself, I'm wondering if Clojure already has a "property list" feature? Or something close?

And if not, what would the assembled Clojure gods have me do to implement such a feature? Remember, every atomic symbol in Maclisp can but does not have to have a property-list. Thank you.

clojure has metadata maps associated with variables / data values:

user> (def x [1 2 3])
#'user/x

user> (reset-meta! #'x {:my-data 1})
;;=> {:my-data 1}

notice that this metadata is associated with variable, not with variable bound data

user> (-> x var meta)
{:my-data 1}

user> (-> #'x meta) ;; short form
{:my-data 1}

user> (-> x meta)
nil

otherwise you can attach it to the value itself:

user> (def x ^{:some-data 101} [1 2 3])
#'user/x
user> (meta x)
{:some-data 101}

depending on how do you want to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM