1

I have a simple sequence of arbitrary elements that I would like to reduce over two-by-two.

In order to do that, I generate pairs with the data, but the way I do it is wrong since I need to call a function generating the data twice :

(defn gen-pairs [l]
  (partition 2 (drop 1 (take l (interleave (gen-data) (gen-data))))))

How can I avoid calling gen-data twice (gen-data returns a sequence of items lazily, like range for instance) ?

1
  • 1
    One way would be to write your own version of interleave that took the gen-data function as its parameter. Commented Apr 5, 2016 at 13:54

1 Answer 1

3

Your question would be clearer if you included an example of what output you wanted, but I think that you're after partition with a step of 1:

user=> (partition 2 1 [1 2 3 4 5 6 7])
((1 2) (2 3) (3 4) (4 5) (5 6) (6 7))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.