Reputation: 345
I want to combine elements from two lists, my program looks like this
(ns datamodel
(:use
[net.cgrand.enlive-html :as en-html ])
(:require
[clojure.zip :as z]
[clojure.data.zip.xml :only (attr text xml->) :as xz]
[clojure.xml :as xml ]
[clojure.data.zip.xml :as zf]
[clojure.java.io :as io]
))
(def data-url "http://api.eventful.com/rest/events/search?app_key=4H4Vff4PdrTGp3vV&keywords=music&location=Belgrade&date=Future")
(defn map-tags-contents [url & tags]
(map #(hash-map % (keyword (last tags)))
(mapcat (comp :content z/node)
(apply xz/xml->
(-> url xml/parse z/xml-zip)
(for [t tags]
(zf/tag= t)
)))))
(def titles (map-tags-contents data-url :events :event :title))
(def descriptions (map-tags-contents data-url :events :event :description))
(defn create-map [](for [el1 titles
el2 descriptions] (into {} (conj el1 el2 ))))
But when I call create-map resulting maps in list are duplicated. I see that I got Cartesian join, because I didn't say the way elements will be combined. And I want first element from first map and first from second map to be combined, second element from first map and second from second map, etc...
Upvotes: 2
Views: 2261
Reputation: 345
So the fn should look like this
(defn create-map [](map conj titles descriptions ))
Thank to @A. Webb
Upvotes: 0
Reputation: 26446
Element-wise combination
(map list [1 2 3] [:a :b :c]) ;=> ((1 :a) (2 :b) (3 :c))
Cartesian product
(for [x [1 2 3], y [:a :b :c]] (list x y))
;=> ((1 :a) (1 :b) (1 :c) (2 :a) (2 :b) (2 :c) (3 :a) (3 :b) (3 :c))
Upvotes: 6