How to write Ruby each_cons in Clojure?

How can I rewrite this Ruby code in Clojure?

seq = [1, 2, 3, 4, 5].each_cons(2) #=> lazy Enumerable of pairs seq.to_a => [[1, 2], [2, 3], [3, 4], [4, 5]] 

Clojure:

 (??? 2 [1 2 3 4 5]) ;=> lazy seq of [1 2] [2 3] [3 4] [4 5] 
+6
source share
1 answer

What you are asking for is called a sliding window in a lazy sequence. That way you can achieve this

 user=> (partition 2 1 [1 2 3 4 5]) ((1 2) (2 3) (3 4) (4 5)) 
+9
source

Source: https://habr.com/ru/post/952896/


All Articles