Reputation: 463
A still-learning clojure-newbie (me) got a list of maps.
Each map contains one account number and other information
(e.g. ({:account 123, :type "PK", :end "01.01.2013", ...} {:account 456 :type "GK" :end "01.07.2016", ...})
Now I need a function that sequentially puts a increasing number and the account number
( like {1, 123, 2, 456 etc}
). And I didn't get it no matter what I tried.
I once learned Delphi, and it would be there like
for i :=1 to (count MYMAP)
do (put-in-a-list i AND i-th account number in the list)
inc i
Due to some restrictions I am not allowed to use functions out of the core and also I must not use "use", "ns", "require", "cycle", "time", "loop", "while", "defn", "defstruct", "defmacro", "def", "defn", "doall", "dorun", "eval", "read-string", "repeatedly", "repeat", "iterate", "import", "slurp", "spit" .
And - please excuse me if there is any bad english - it's not usual for me to ask such questions in english.
Upvotes: 1
Views: 130
Reputation: 1524
map-indexed
will help you create an increasing number sequence:
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}
Upvotes: 2
Reputation: 3378
For lazy sequence of natural numbers interspersed with account numbers, you can try something like the following:
(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
However, the {}
notation in your example ({1, 123, 2, 456 etc}
) suggests you might be more interested in a map. In that case, you can use 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", ...}, ...]))
Upvotes: 3