2

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...

4

2 回答 2

5

元素组合

(map list [1 2 3] [:a :b :c]) ;=> ((1 :a) (2 :b) (3 :c))

笛卡尔积

(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))
于 2013-05-27T15:39:05.300 回答
0

所以 fn 应该是这样的

(defn create-map [](map conj titles descriptions ))

感谢@A。韦伯

于 2013-05-27T15:51:25.573 回答