与包含所有元素的分区相关的功能?

Dav*_* J. 3 clojure

Clojure partition函数的这种行为不是我需要的:

user=> (partition 3 (range 3))
((0 1 2))
user=> (partition 3 (range 4))
((0 1 2))
user=> (partition 3 (range 5))
((0 1 2))
user=> (partition 3 (range 6))
((0 1 2) (3 4 5))
Run Code Online (Sandbox Code Playgroud)

我需要包含该集合的"剩余"部分,例如:

user=> (partition* 3 (range 4))
((0 1 2) (3))
user=> (partition* 3 (range 5))
((0 1 2) (3 4))
Run Code Online (Sandbox Code Playgroud)

是否有标准库函数可以满足我的需求?

Die*_*sch 7

你在找partition-all.只需在您的示例中替换它:

user> (partition-all 3 (range 4))
((0 1 2) (3))
user> (partition-all 3 (range 5)) 
((0 1 2) (3 4))
Run Code Online (Sandbox Code Playgroud)


Dav*_* J. 5

pad在4-arity版本中有一个论点partition:

user=> (partition 3 3 [] (range 4))
((0 1 2) (3))

user=> (partition 3 3 [] (range 5))
((0 1 2) (3 4))
Run Code Online (Sandbox Code Playgroud)

文档字符串:

user=> (doc partition)
-------------------------
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.
Run Code Online (Sandbox Code Playgroud)