Frank C.
Frank C.

Reputation: 8088

IllegalArgumentException Key must be integer error in Clojure

Am getting the subject error when trying to do a simple thread macro.

Keyword defines

(def entityType (keyword "name"))
(def entityURI (keyword "uri"))

I have the following 'lazy' sequence:

(def m1
    (({:uri "...#OWLClass_82601afd_b43d_43b4_94fe_2836f40009ca", :name "foo"} 
    {:uri "...#OWLClass_8c6759f0_a165_4a09_a9d8_2603bb106fc6", :name "bar"}))

Here is the REPL thread:

(->> (first m1)
     (map (fn [rec] (rec entityType))))

And the resulting error:

IllegalArgumentException Key must be integer  clojure.lang.APersistentVector.invoke (APersistentVector.java:265)

Anyone with any insight?

Upvotes: 1

Views: 3195

Answers (2)

Benjamin McFerren
Benjamin McFerren

Reputation: 862

For those who wind up on this post after searching this error string, be careful you are not accidentlly trying to assoc a (keyword ...) to a vector:'

(def foo [])

(assoc-in foo [:aaa (keyword "b-bb")] ["obj"] )

Upvotes: 0

ponzao
ponzao

Reputation: 20934

Based on your code I am guessing you want m1 to be declared like this:

(def m1
  (list (list {:uri "...#OWLClass_82601afd_b43d_43b4_94fe_2836f40009ca", :name "foo"} 
              {:uri "...#OWLClass_8c6759f0_a165_4a09_a9d8_2603bb106fc6", :name "bar"})))

Thus:

(->> (first m1)
     (map (fn [rec] (rec entityType))))
;=> ("foo" "bar")

Is this what you were after?

EDIT:

Based on your comment I've understood that the structure of m1 is flatter, like this:

(def m1
  (list {:uri "...#OWLClass_82601afd_b43d_43b4_94fe_2836f40009ca", :name "foo"} 
        {:uri "...#OWLClass_8c6759f0_a165_4a09_a9d8_2603bb106fc6", :name "bar"}))

Based on this you can just do (map (fn [rec] (rec entityType)) m1) and drop the ->>.

Upvotes: 3

Related Questions