2012-08-30 31 views
1

一個仍在學習的clojure-newbie(我)得到了一張地圖列表。
每個地圖包含一個帳號和其他信息
(例如({:帳戶123,:類型「PK」,:結束「01.01.2013」​​,...} {:帳戶456:類型「GK」:結束「2016年7月1日」,...}) 現在我需要的是按順序把一個不斷增加和賬戶號碼
(如{1, 123, 2, 456 etc})的功能。而且我並沒有得到它,不管是什麼我試過了。來自地圖的地圖計數值

我曾經瞭解到德爾福,這將是有像

for i :=1 to (count MYMAP)
do (put-in-a-list i AND i-th account number in the list)
inc i

d我不允許使用核心功能,也不能使用「use」,「ns」,「require」,「cycle」,「time」,「loop」,「while」,「 defn「,」defma「,」defmacro「,」def「,」defn「,」doall「,」dorun「,」eval「,」讀取字符串「,」重複「,」重複「進口「,」唾液「,」吐「。

而且 - 請原諒,如果有什麼不好的英語 - 我不會用英語問這些問題。

+1

假設這是家庭作業,看看'assoc'和'地圖,indexed' 。 –

回答

3

map-indexed將幫助您創建越來越多的序列:

user> (let [f (comp (partial into {}) 
        (partial map-indexed #(vector (inc %) (:account %2))))] 
     (f [{:account 123, :type "PK", :end "01.01.2013"} {:account 456 :type "GK" :end "01.07.2016"}])) 
{1 123, 2 456} 
3

對於賬號穿插自然數的懶序列,你可以嘗試類似如下:

(interleave ; splices together the following sequences 
(map inc (range)) ; an infinite sequence of numbers starting at 1 
(map :account ; gets account numbers out of maps 
     [{:account 123, :type "PK", :end "01.01.2013", ...}, ...])) ; your accounts 

然而,{}符號在你的例子({1, 123, 2, 456 etc})提供您可能更感興趣的地圖。在這種情況下,你可以使用zipmap

(zipmap ; makes a map with keys from first sequence to values from the second 
(map inc (range)) 
(map :account 
     [{:account 123, :type "PK", :end "01.01.2013", ...}, ...]))