在clojure中如何映射重叠对? [英] In clojure how to map over overlapping pairs?
本文介绍了在clojure中如何映射重叠对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有序列:
[1 2 3 4 5]
我想成对地映射它们:
[(1, 2), (2, 3), (3, 4), (4, 5)]
我试过了:
(map f (partition 2 [1 2 3 4]))
但这会导致成对序列:
[(1, 2), (3, 4)]
我怎样才能获得所需的功能?
How can I get the desired functionality?
推荐答案
默认情况下 partiton
返回非重叠分区,但您可以提供 step
参数来提供创建分区的偏移量:
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.
这会做你想做的:
(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))
这篇关于在clojure中如何映射重叠对?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文