如何在 Clojure 中重写这段 Ruby 代码?
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]
您所要求的称为惰性序列上的滑动窗口。这样您就可以实现
user=> (partition 2 1 [1 2 3 4 5])
((1 2) (2 3) (3 4) (4 5))