2016-05-03 28 views
0

我有一個向量fo關鍵字,我有一個向量與實際數據的集合,我想要做的是搜索每個關鍵字,並返回true,如果任何向量包含該關鍵字。目前,我只有一個關鍵字的實現,但是我無法使它適用於關鍵字向量。我在Clojure非常新,所以任何建議將不勝感激。 這是到目前爲止我的代碼:向量集合中搜索矢量元素

(def results (apply map vector [["test1" "test2"] ["test3" "test4" "test5"]])) 
(defn in? 
    "true if coll contains elm" 
    [coll] 
    (some #(= "test4" %) coll)) 

    (println (map #(in? %) results)) 

,但我想是這樣的:

(def searchwords ["test3" "test2"]) 
    (def results (apply map vector [["test1" "test2"] ["test3" "test4" "test5"]])) 
    (defn in? 
     "true if coll contains elm" 
     [coll keyword] 
     (some #(= keyword %) coll)) 
+5

你能詳細介紹一下你想達到的目標嗎?示例輸入和輸出將會有所幫助。 – OlegTheCat

+0

例如現在在?函數返回(nil,true),這意味着在第二個向量中找到了「test4」,我想要實現的是每當(某個#(= keyword%)coll)返回true時更改關鍵字再次搜索 – fishera

+0

請提供示例您需要實現的功能的輸入和預期輸出。附加他們正確的問題。 – OlegTheCat

回答

0

你可以嘗試以下

(defn search 
    [coll keyword] 
    (println (flatten coll)) 
    (if (= (flatten coll) (list keyword)) 
    true)) 

(defn in? 
    [coll keyword] 
    (map #(search % %2) coll keyword))  

(in? results searchwords) 
;=> (true) 

這隻能如果searchwords是[「測試2 「],結果是[[」test2「]]。 也許如果你可以找出一種方法來使用in?遍歷結果。

P.s clojure也是新手。