I have data in the following structure (simplified):
(def m {"a" [1 2 3] "b" [4 5 6] "c" [7 8 9] "d" [10 11 12]})
I have a recursive function and at any iteration I am interested in the i'th element of each of a
, b
, c
and d
.
What I have been doing is:
(loop [i 0]
(let [a ((m "a") i)
b ((m "b") i)
c ((m "c") i)
d ((m "d") i)]
...do stuff with a, b, c and d...
In other words I am creating bindings for a
, b
, c
and d
and that is because I would rather not repeat something like ((m "a") i)
multiple times in my code every time I need this value.
This seems a little clunky and not a very functional style. Is there a better way to achieve this? That is, either a more elegant way of creating the bindings or perhaps even a way that avoids bindings?
Edit: Adding an explanation as to why I need loop and not map:
My data represents a tree and my function traverses the tree to find the appropriate terminal node. The i'th
element of each of the vectors is the data related to the i'th
node. Therefore I start with i = 0
as this is the root node. I do some logic and this tells me which node to go to next. The logic is the same at each node and this is why I have used loop
and recur
. In reality there are perhaps 200 nodes and so one route through the tree could be 0 > 6 > 45 > 67 > 123 > 130 > 156 > done
.
I would be hugely impressed if there is a way to traverse a tree with map
and not loop
.