3

I'm reading data from a file where each line has two values. Each line is represented by a sequence within an outer sequence representing the file.

I'd like to restructure the data into a sequence of maps for further processing.

I know how to create a map from a key set and sequence of values:

=> (defstruct entry :name :age)
=> (apply struct entry '("John" 34))
{:name "John", :age 34}

But how do I create a sequence of such maps based on a sequence of value sequences?

(map (apply struct entry) '(("John" 34) ("Lisa" 41))))

results in:

java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.PersistentStructMap$Def

EDIT: Renamed symbol for clarity.

2 Answers 2

5

structs are obsolete, the preference is to use records now.

(defrecord Person [name age])

(map (partial apply ->Person) '(("John" 34) ("Lisa" 41)))
Sign up to request clarification or add additional context in comments.

Comments

3

Use zipmap

(map (partial zipmap [:name :age]) '(("John" 34) ("Lisa" 41)))

;-> ({:name "John", :age 34} {:name "Lisa", :age 5})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.