0

New to Clojure.

I am trying to build up a data structure programmatically for insertion into a database. I actually have something that works just fine, but it does an insert for each record, and I'd like to generate the whole record, and then insert the whole thing at once with one insert.

Here is what I have working so far:

  (doseq [record-data1 [:one :two :three]
    (doseq [record-data2 [1 2 3]]
      (insert {record-data1 record-data2})

Any suggestions on how to generate the entire bulk structure first before insert? Have tried variations on map, walk, etc. but haven't been able to come up with anything yet.

Thanks.

4
  • Your question is not clear. What do you want the "entire bulk structure" to look like? Commented Sep 30, 2014 at 19:09
  • whoops, you are right. something like this: ({:one 1} {:one 2} {:one 3} {:two 1} ... ) Commented Sep 30, 2014 at 20:18
  • If you require to add every combination of two collections, I'd be concerned about the database design. Ought the table to be factored into two? Commented Oct 1, 2014 at 10:27
  • @Thumbnail, no. The collections are just a way to generate the records I need. The table is properly factored. Commented Oct 1, 2014 at 18:01

1 Answer 1

1

I'm not sure I understand what you mean by "entire bulk structure". You can't put the cross-product of record-data1 and record-data2 in the same dictionary. Maybe you're looking for this:

user=> (for [record-data1 [:a :b :c] record-data2 [1 2 3]] {record-data1 record-data2})
({:a 1} {:a 2} {:a 3} {:b 1} {:b 2} {:b 3} {:c 1} {:c 2} {:c 3})
Sign up to request clarification or add additional context in comments.

2 Comments

i think this is exactly what i'm looking for - thanks. one of the challenges of learning a new language is simply learning all the functions possible in the standard library. point being, i didn't know "for" existed.
You can also use extra bindings on doseq to get the same expansion behavior that for has (though in this case you don't want that because you want a single insert statement).

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.