I am just starting to learn clojure and reading some simple examples and then doing my best for rtfm for concepts.
However, I'm a little confused by what val does in the example below. This was taken from the clojure doc examples for val .
(first {:one :two}) ;; => [:one :two]
Here is a hash-map with the key :one and the value :two passed first . Behind the scenes, clojure converts this hash-map into a sequence of vectors . Since there is only one vector in this sequence , it returns [:one :two] .
(val (first {:one :two})) ;; => :two (val [:one :two]) ;; => ClassCastException clojure.lang.PersistentVector cannot be cast to java.util.Map$Entry (val {:one :two}) ;; => ClassCastException clojure.lang.PersistentArrayMap cannot be cast to java.util.Map$Entry
If I try to call val on (I think) a hash-map (I understand that this is actually a "persistent array map"), I get an exception, as shown above.
The following also confuses me:
(first {:one :two}) ;;
Why can't I just connect the result (first {:one :two}) to val and get the same result?
In addition, another example shown on the page is as follows:
(map val {:a 1 :b 2}) ;; => (1 2)
This is how I read the line. Take array-map {:a 1 :b 2} . For each key-value pair, call val for the pair to return the value. Return a sequence from the received calls to map . Is this the right way to read the problem?
As always, thanks for any help.
source share