Andrea
Andrea

Reputation: 2764

Clojure vector manipulation

I have a vector of 4 numbers: [11 23 37 55]; I want to produce a sequence with 3 numbers where each of them is the result of the difference between the n+1 and the n element: ( (23-11) (37-23) (55-37)) = (12 14 28)

How can I do that in clojure?

Thx

Upvotes: 0

Views: 181

Answers (1)

noisesmith
noisesmith

Reputation: 20194

This can be done easily with map.

user=> (def v [11 23 37 55])
#'user/v
user=> (map - (rest v) v)
(12 14 18)

when it gets more than two args, it takes elements from each sequence as the positional arguments to the function.

Upvotes: 2

Related Questions