Fork me on GitHub
#clojure
<
2017-07-15
>
nicoschneider14:07:08

hello everybody, is there a way to get the namespace of a map with uniformly namespaced keys?

nicoschneider14:07:04

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

jahson15:07:37

user=> (map namespace [::a ::b ::c])
("user" "user" "user")

noisesmith15:07:21

the bot is kind of weird, the original hash-map was {::a 0 ::b 1 ::c 2}

Vishal Gautam17:07:35

can anyone let me know why this doesnt work

(defn what-is [x]
  (cond
    (identical? x 42) "everything"
    (identical? x (* 42 42)) "everything squared"
    :else "nothing"))

(what-is (* 42 42)) ; "nothing"

noisesmith17:07:04

@vishalgg

+user=> (identical? 127 127)
true
+user=> (identical? 128 128)
false

noisesmith17:07:31

there's a cache for the Longs, but only between -128 and 127, they aren't all cached

noisesmith17:07:53

identical? isn't just an equality check, it checks if the data is stored in the same location on hardware

noisesmith17:07:17

when you type in 127, clojure asks for an instance of Long, and java uses a cached set of Longs to look up the value, when you type in 128 clojure once again asks for a Long, but this time java makes a new one

Vishal Gautam17:07:00

Thanks a lot, i was using the wrong function. This worked

(defn what-is [x]
  (cond
    (= x 42) "everything"
    (= x (* 42 42)) "everything squared"
    :else "nothing"))

noisesmith17:07:18

there's also == for numeric equality

noisesmith17:07:28

eg. it would match 42.0 but = will not

Vishal Gautam18:07:13

Thank you for the help

nicoschneider21:07:13

@jahson @noisesmith thank you, i was looking for something preferably in core that behaves kind of like namespace does, returning the namespace when all keys have the same, nil when not all keys are namespaced, and a set of namespaces when multiple. i’ll write my own then 🙂

noisesmith21:07:34

returning a set or a string from one function is probably less than ideal - that kind of thing tends to increase the complexity of everything around it for no benefit

nicoschneider21:07:30

you are correct, although it’s just for display purposes in a cli statement, so … 😉