Clojure

(partition 0 seq) and (parition-all 0 seq) are infinite sequences of empty sequences

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: Release 1.1, Release 1.2
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

(partition n seq) and (partition-all n seq) are implemented by taking n and dropping n from seq. When n=0 this becomes an infinite sequence of empty sequences.

While conceptionally this makes sense, I think practically it may surprise people, and perhaps it should return an empty sequence or a sequence of a single empty sequence.

Activity

Hide
Paul Stadig added a comment -

Ugh! I didn't mean for this to be major priority, and I can't seem to edit it after the fact.

Show
Paul Stadig added a comment - Ugh! I didn't mean for this to be major priority, and I can't seem to edit it after the fact.
Hide
Mike Anderson added a comment -

I think the current behaviour is logically more correct than returning a empty sequence or a single empty sequence.

In particular, I would expect (partition n infinite-seq) to return an infinite sequence of sequences of length n, for any value of n >= 0. Anything else would be very surprising.

The only other sensible option would be throwing an exception, on the philosophical grounds that it isn't possible to partition a non-empty sequence into sub-sequences of length 0. But I think that changing behaviour in this way would break backwards compatibility for no obvious gain.

I suggest closing this issue.

Show
Mike Anderson added a comment - I think the current behaviour is logically more correct than returning a empty sequence or a single empty sequence. In particular, I would expect (partition n infinite-seq) to return an infinite sequence of sequences of length n, for any value of n >= 0. Anything else would be very surprising. The only other sensible option would be throwing an exception, on the philosophical grounds that it isn't possible to partition a non-empty sequence into sub-sequences of length 0. But I think that changing behaviour in this way would break backwards compatibility for no obvious gain. I suggest closing this issue.
Hide
Andrea Richiardi added a comment -

I am checking the partition function as well as I making, using recur, tail-recursive.
I have run into this issue while testing. Then I came here and spotted it.
At least I would suggest to remove these from ...test_clojure/sequences.clj:
{{(partition -1 [1 2 3]) ()}}
{{(partition -2 [1 2 3]) ()}}

I will do it as soon as I have finished my little change..

Show
Andrea Richiardi added a comment - I am checking the partition function as well as I making, using recur, tail-recursive. I have run into this issue while testing. Then I came here and spotted it. At least I would suggest to remove these from ...test_clojure/sequences.clj: {{(partition -1 [1 2 3]) ()}} {{(partition -2 [1 2 3]) ()}} I will do it as soon as I have finished my little change..
Hide
Alex Miller added a comment -

No plans to change this.

Show
Alex Miller added a comment - No plans to change this.

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: