Tom
Tom

Reputation: 6971

In clojure how to map over overlapping pairs?

Say I have the sequence:

[1 2 3 4 5]

And I want to map over them in the pairs:

[(1, 2), (2, 3), (3, 4), (4, 5)]

I have tried:

(map f (partition 2 [1 2 3 4]))

But this results in the sequence of pairs:

[(1, 2), (3, 4)]

How can I get the desired functionality?

Upvotes: 2

Views: 835

Answers (4)

user4813927
user4813927

Reputation:

This also works independent of the order of numbers in your vector:

(let [a [5 4 3 2 1] b (rest a)] (map vector a b))

will yield:

([5 4] [4 3] [3 2] [2 1])

Upvotes: 0

cfrick
cfrick

Reputation: 37008

An alternative would be map both with the list you have and the rest of the list. E.g.:

(user=> (let [l [1 2 3 4 5]] (map list l (rest l)))
((1 2) (2 3) (3 4) (4 5))

Upvotes: 2

mtyaka
mtyaka

Reputation: 8850

By default partiton returns non-overlapping partitions, but you can supply a step argument to provide the offset at which partitions are created:

clojure.core/partition
([n coll] [n step coll] [n step pad coll])
  Returns a lazy sequence of lists of n items each, at offsets step
  apart. If step is not supplied, defaults to n, i.e. the partitions
  do not overlap. If a pad collection is supplied, use its elements as
  necessary to complete last partition upto n items. In case there are
  not enough padding elements, return a partition with less than n items.

This will do what you want:

(partition 2 1 [1 2 3 4 5 6 7 8]))
; #=> ((1 2) (2 3) (3 4) (4 5) (5 6) (6 7) (7 8))

Upvotes: 12

birdspider
birdspider

Reputation: 3074

I would do it as follows

; first generate data 
; (that is if you really are talking about all the ints)
(map (juxt identity inc) [1 2 3 4 5])
=> ([1 2] [2 3] [3 4] [4 5] [5 6])

; inline example
(map (comp 
       (fn [[a b]] (println a b)) ; this would be your f
       (juxt identity inc)) [1 2 3 4 5])

; using your function f
(map (comp 
       f
       (juxt identity inc)) [1 2 3 4 5])

Upvotes: 1

Related Questions