Skip to content

Commit

Permalink
Make integers fixed (#3)
Browse files Browse the repository at this point in the history
* Make integers not flattenable

* Bump patch

* Update README

* Tweak README

Co-authored-by: wt <[email protected]>
  • Loading branch information
willtebbutt and wt authored Aug 28, 2020
1 parent f274f3d commit b21638b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ParameterHandling"
uuid = "2412ca09-6db7-441c-8e3a-88d5709968c5"
authors = ["Invenia Technical Computing Corporation"]
version = "0.1.0"
version = "0.1.1"

[deps]
Bijectors = "76274a88-744f-5084-9051-94815aaf08c4"
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,9 @@ julia> value(unflatten(new_v)) # Obtain constrained value.
It is straightforward to implement your own parameters that interoperate with those already
written by implementing `value` and `flatten` for them. You might want to do this if this
package doesn't currently support the functionality that you need.



# Gotchas

1. `Integer`s typically don't take part in the kind of optimisation procedures that this package is designed to handle. Consequently, `flatten(::Integer)` produces an empty vector.
10 changes: 8 additions & 2 deletions src/flatten.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ true
"""
function flatten end

function flatten(x::Real)
function flatten(x::Integer)
v = Float64[]
unflatten_Integer(v::Vector{Float64}) = x
return v, unflatten_Integer
end

function flatten(x::AbstractFloat)
v = [x]
unflatten_to_Real(v::Vector{<:Real}) = only(v)
return v, unflatten_to_Real
end

flatten(x::Vector{<:Real}) = (x, identity)
flatten(x::Vector{<:AbstractFloat}) = (x, identity)

function flatten(x::AbstractVector)
x_vecs_and_backs = map(flatten, x)
Expand Down
5 changes: 5 additions & 0 deletions test/flatten.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

@testset "Reals" begin
test_flatten_interface(1.0)

@testset "Integers" begin
test_flatten_interface(1)
@test isempty(first(flatten(1)))
end
end

@testset "AbstractArrays" begin
Expand Down

4 comments on commit b21638b

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/20466

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" b21638b239fdca6ae9e36382cb72941cdf28f34e
git push origin v0.1.1

Also, note the warning: This looks like a new registration that registers version 0.1.1.
Ideally, you should register an initial release with 0.0.1, 0.1.0 or 1.0.0 version numbers
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

@willtebbutt
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request updated: JuliaRegistries/General/20466

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" b21638b239fdca6ae9e36382cb72941cdf28f34e
git push origin v0.1.1

Please sign in to comment.