Skip to content

Fix partitions(0), closes #143 #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/partitions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ end
Base.length(p::IntegerPartitions) = npartitions(p.n)
Base.eltype(p::IntegerPartitions) = Vector{Int}

function Base.iterate(p::IntegerPartitions, xs = Int[])
function Base.iterate(p::IntegerPartitions, xs = nothing)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we not instead use xs = Int[] and do isempty(xs)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, and that's exactly the point.

collect(partitions(0)) must return Vector{Int}[[]].

This means that on the first call iterate(partitions(0)) must return (Int[], Int[]) and on the second call iterate(partitions(0), Int[]) must return nothing. Therefore the default value of xs (which is used in the first call) cannot be Int[] (which is used in the second call), because the behavior must be different.

Of course we could swap the role of nothing and Int[] and have iterate(partitions(0)) return (Int[], nothing) and iterate(partitions(0), nothing) return nothing, but this doesn't really work around the necessity of having an extra sentinel value which is different from Int[].

Leaving the code as it currently is preserves the "symmetry" that iterate always returns (xs, xs). Swapping nothing and Int[] instead has the benefit that the sentinel value nothing is used only for partitions(0) and not partitions(n) with n>0.

Copy link
Contributor Author

@FedericoStra FedericoStra May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for reference, the "swapped" code would look something like this:

function Base.iterate(p::IntegerPartitions, xs = Int[])
    if p.n == 0
        if xs === nothing # sentinel value
            return nothing
        else
            return (Int[], nothing) # sentinel value
        end
    end
    length(xs) == p.n && return
    xs = nextpartition(p.n, xs)
    return (xs, xs)
end

In this code it is maybe clearer that the nothing in the two lines marked with # sentinel value can be replaced by anything else we like as long as it is different from Int[]. For instance, we could use [0], which makes the code more type stable. I tried benchmarking this and it is slightly faster for collect(permutations(5)) (thanks to type stability), at the expenses of collect(permutations(0)) which is slower because Int[] === nothing is faster than Int[] == [0].

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clear explanation. I would be more fan of the latter approach. @inkydragon What do you think?

if xs === nothing
p.n == 0 && return Int[], Int[]
xs = Int[]
end
length(xs) == p.n && return
xs = nextpartition(p.n,xs)
(xs, xs)
Expand Down
2 changes: 1 addition & 1 deletion test/partitions.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "partitions" begin

@testset "partitions(n::Integer)" begin
@test_broken collect(partitions(0)) == [[]]
@test collect(partitions(0)) == [[]]
@test collect(partitions(1)) == [[1]]
@test collect(partitions(2)) == [[2], [1, 1]]
@test collect(partitions(3)) == [[3], [2, 1], [1, 1, 1]]
Expand Down
Loading