2017-02-11 61 views
0
(defn returnLoc [obj-data super-cat] 
    ;If the list passed through is not empty 
    (if-not (empty? obj-data) 
    ;If the super-category passed in (i.e. Fruit/Agent) is equal 
    ;to the Super-Category (the second object in the first row) 
    (if (= super-cat (nth (first obj-data) 2)) 
     ;Recurvisely goes through the same process as above, 
     ;To see if there is any other records in the list with the same super-cat 
     ;then finds the location of the object and conj[oin]'s that to the returned values 
     (conj (returnLoc (rest obj-data) super-cat) 
      (nth (first obj-data) 3)) 
     ;If the super-cat passed through is not equal, it does not add it to the list 
     ;And recursively goes back through to check if there are any other possible items to add to 
     ;the list. 
     (returnLoc (rest obj-data) super-cat) 
    ) 
    ()) 
) 

正如你可以看到,我返回OBJ-數據的第3個值一旦代碼已經找到了比賽,有沒有辦法在那裏我能得到OBJ-數據的第2個值,每次3日返回,然後在末尾添加一個分隔符,將2個值連接到返回列表的方法?

它當前返回一個項目的位置(fruit/agent),但我希望它返回特定的項目和位置。

所以它看起來像:

(returnLoc obj-data 'agent) 

回報:

=>(hallway bedroom) 

理想的地方,我想它返回:

=>(tom is in hallway | jerry is in bedroom) 

有沒有人對如何解決做這個?

回答

-1

想通了我自己的解決方案,它的工作原理:

(def obj-data 
       '((apple#3 apple fruit kitchen) 
       (mango#5 mango fruit kitchen) 
       (tom cat agent hallway) 
       (jerry mouse agent bedroom) 
       (matthew JavaStudentMatthew student livesAtHome) 
       (tom NetworkStudentTom student newcastleHome) 
       (Nathan NetworkStudentNathan student middlesbroughHome) 
       (Jack NetworkStudentJack student kexgillHome) 
       )) 

(defn returnLoc [obj-data super-cat] 
    ;If the list passed through is not empty 
    (if-not (empty? obj-data) 
    ;If the super-category passed in (i.e. Fruit/Agent) is equal 
    ;to the Super-Category (the second object in the first row) 
    (if (= super-cat (nth (first obj-data) 2)) 
     ;Recurvisely goes through the same process as above, 
     ;To see if there is any other records in the list with the same super-cat 
     ;then finds the location of the object and conj[oin]'s that to the returned values 
     (cons (list (nth (first obj-data) 1) " is in " (nth (first obj-data) 3)) 
     (returnLoc (rest obj-data) super-cat)) 
     ;If the super-cat passed through is not equal, it does not add it to the list 
     ;And recursively goes back through to check if there are any other possible items to add to 
     ;the list. 
     (returnLoc (rest obj-data) super-cat) 
    ) 
    ()) 
) 
1

conj需要一個以上的元素被添加到集合:

(conj '(on this list) 'elements 'two) 
; ==> (two elements on this list) 
3

如果你讓你的解決方案更地道,你眼前的問題消失:

  1. obj-data的每個元素表示爲映射(或記錄),不是列表。
  2. 識別returnLoc計算的模式。

1代表的obj-data每個元素作爲一個地圖(或記錄),而不是作爲一個列表。然後

你想要的解決方案可能是

[{:who 'Tom, :where 'hallway} {:who 'Jerry, :where 'bedroom}] 

這是相當可讀的,所以你不必急着把它翻譯成純文本。

2.識別returnLoc計算的模式。

returnLoc是做什麼用的?

  • 它選擇具有特定
    characeristic OBJ-數據的元素:其:where值是super-cat。這是一個 filter操作。
  • 它從所有這些元素中提取:who屬性。這是一個 map操作。然後

returnLoc功能可能是

(defn returnLoc [obj-data super-cat] 
    (map 
    :who 
    (filter 
     #(= (:where %) super-cat) 
     obj-data))) 

...或者,使用線程宏,

(defn returnLoc [obj-data super-cat] 
    (->> obj-data 
     (filter #(= (:where %) super-cat)) 
     (map :who))) 
  • 關鍵字:who:where用作存取功能。
  • 這些版本維護obj-data中元素的順序。您的 代碼將其逆轉。

既然你想同時保留:who:where領域,爲什麼不直接返回全圖/記錄:

(defn returnLoc [obj-data super-cat] 
    (filter 
    #(= (:where %) super-cat) 
    obj-data)) 

節省工作,返回不可變的映射引用。不需要構建新的地圖。

如果你有決心擺脫其他領域,使用select-keys

(defn returnLoc [obj-data super-cat] 
    (->> obj-data 
     (filter #(= (:where %) super-cat)) 
     (map #(select-keys % [:who :where])))) 
相關問題