Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Collections by

What's the most idiomatic way of writing a function that, given a coll (1 2 3 4 5)

Returns

((1 2) (2 3) (3 4) (4 5))

i.e. consecutive couples of elements in a collection.

My naive solution would be:

(map (fn [a b] [a b]) a (rest a))

1 Answer

+2 votes
by

partition has an arity with a step argment that does this.

(partition 2 1 [1 2 3 4 5]) ;=> ((1 2) (2 3) (3 4) (4 5))

...