What functions in Clojure kernel metadata?

Clojure meta is saved only if the function takes care of it, and the main functions of Clojure will not globally save the meta. The general rule of thumb that I have heard is that collection functions like conj, assoc, etc., Must save sequence meta-functions like map, filter, take, etc. Do not save meta.

Is there a list somewhere which DO functions save the meta?

+8
clojure meta
source share
1 answer

All about types. The sequence functions act the way they call seq on their argument and therefore do not always return the same type of object. Collection functions and type-specific functions do not call seq and return an object of the same type as what was given to them. This makes them an illusion of returning the same object (this may be the reason for this behavior), even if it really is not. We can say that the rule of thumb is that functions preserve meta while preserving type.

 user> (meta (seq (with-meta (list 1) {:a 1}))) {:a 1} user> (meta (seq (with-meta (vector 1) {:a 1}))) nil 

Be sure to remember when laziness was tough:

 user> (type (list 1)) clojure.lang.PersistentList user> (type (map identity (list 1))) clojure.lang.LazySeq user> (meta (seq (with-meta (map identity (list 1)) {:a 1}))) nil 

For a list of functions storing meta on collection, see the data structure section. Those that don't save the meta are on the sequences page, except when they return an object of the same type.

Under the hood, I'm not quite sure of the details, as a sequence of laziness and chunked has been added, but you can look at the cons , seq and seqFrom from the RT class. These methods perform functions that do not store metadata. For now, collection functions end using methods specific to their types.

+5
source share

Source: https://habr.com/ru/post/650993/


All Articles