Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Docs by

The doc for partition states "In case there are not enough padding elements, return a partition with less than n items." However, the behavior of this function is as follows:

user=> (partition 3 (range 10)) ((0 1 2) (3 4 5) (6 7 8)) user=> (partition 4 (range 10)) ((0 1 2 3) (4 5 6 7))

Proposed: Proposed docstring update in bold:

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 not supplied, any tail elements
that remain from dividing the input collection length by n will not be
included in a partition.
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.

Patch: clj-1117.patch

7 Answers

0 votes
by

Comment made by: jafingerhut

That would be a potentially breaking change for some people's code that uses partition. partition-all behaves as you wish.

Also, your concern with the documentation is for when there are padding elements specified as an argument, but your examples don't specify any padding elements.

0 votes
by

Comment made by: halgari

I agree, but I think the docs should then explicitly state: "if no padding is given, not all input elements may be returned in the output partitions" or something to that line.

0 votes
by

Comment made by: jafingerhut

More precise documentation of current behavior is always welcome in my opinion.

0 votes
by

Comment made by: cldwalker

I've uploaded a patch that calls out when and how partition drops tail elements:
"If a pad collection is not supplied, any tail elements that remain from dividing the input collection length by n will not be included in a partition."

0 votes
by

Comment made by: arichiardi

I was a little bit baffled by this as well...in particular, I could not figure out why (partition 2 3 (link: 1 2 3 4 5 6 7)) is not '((1 2) (4 5) (7))...it would really be better to rewrite that comment.

0 votes
by

Comment made by: stu

I do not find the new wording more clear. Also it does not s/less/fewer

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1117 (reported by halgari)
...