clojure - iterate a sequence, accessing current item and the following in each step -
this question has answer here:
- in clojure how map on overlapping pairs? 4 answers
say, have 5 javascript objects stored in vector:
(def v [o1 o2 o3 o4 o5]) each o them has method "connect", gets object parameter.
manually now:
o1.connect(o2); o2.connect(o3); o3.connect(o4); o4.connect(o5); what approach automate this?
only weird solutions come mind: as:
(doseq [[a b] (zipmap (butlast v) (rest v))] (.connect b)) is there better way?
you can use partition:
(doseq [[a b] (partition 2 1 v)] (.connect b))
Comments
Post a Comment