简体   繁体   English

如何使用统一命名空间的键获取地图的命名空间?

[英]How to get the namespace of a map with uniformly namespaced keys?

Is there a way to get the namespace of a map with uniformly name spaced keys? 有没有一种方法来获取具有统一名称分隔键的映射的名称空间?

For example: 例如:

(map-ns {:some/key :val :some/other :val ,,,}) => “some”)

If the map is not empty and all of its keys have the same namespace, the following suffices: 如果映射不为空,并且其所有键都具有相同的名称空间,则满足以下条件:

(def map-ns (comp namespace first keys))

Alternatively: 或者:

(def map-ns (comp namespace ffirst))

And here is a version that returns nil when the map is empty or has keys with different namespaces: 这是一个版本,当地图为空或具有不同名称空间的键时,返回nil

(def map-ns
  (comp
    #(if-not (next %) (first %))
    distinct
    (partial map namespace)
    keys))

Alternatively: 或者:

(def map-ns
  (comp
    #(if-not (next %) (first %))
    distinct
    (partial map (comp namespace key))))
(->> m keys (map namespace) set)

上面将为您提供所有名称空间的集合,或者如果size> 1,则可以返回nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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