在clojure如何映射重叠对? [英] In clojure how to map over overlapping pairs?
本文介绍了在clojure如何映射重叠对?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有序列:
[1 2 3 4 5]
我想要将它们映射到它们的对中:
And I want to map over them in the pairs:
[(1, 2), (2, 3), (3, 4), (4, 5)]
我尝试过:
(map f (partition 2 [1 2 3 4]))
但这会导致对的顺序:
[(1, 2), (3, 4)]
$ b b
如何获得所需的功能?
How can I get the desired functionality?
推荐答案
默认情况下 partion
返回不重叠的分区,提供步骤
参数以提供创建分区时的偏移量:
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屋!
查看全文