Anton Harald February 2016

iterate a sequence, accessing current item and the following in each step

say, I have 5 javaScript Objects stored in a vector:

(def v [o1 o2 o3 o4 o5])

Each o them has a method "connect", which gets another object as a parameter.

Manually I would now:

o1.connect(o2);
o2.connect(o3);
o3.connect(o4);
o4.connect(o5);

What would be a good approach to automate this?

only some weird solutions come to my mind: as:

(doseq [[a b] (zipmap (butlast v) (rest v))]
  (.connect a b))

is there a better way?

Answers


Lee February 2016

You can use partition:

(doseq [[a b] (partition 2 1 v)]
  (.connect a b))

Post Status

Asked in February 2016
Viewed 1,790 times
Voted 14
Answered 1 times

Search




Leave an answer