Skip to content

Commit

Permalink
Implemented clamp(t, i::ClosedInterval) (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagot authored May 4, 2021
1 parent 4e5e2e9 commit e73b682
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "IntervalSets"
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
version = "0.5.3"
version = "0.5.4"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
10 changes: 9 additions & 1 deletion src/IntervalSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module IntervalSets

using Base: @pure
import Base: eltype, convert, show, in, length, isempty, isequal, issubset, ==, hash,
union, intersect, minimum, maximum, extrema, range,
union, intersect, minimum, maximum, extrema, range, clamp,

using Statistics
import Statistics: mean
Expand Down Expand Up @@ -270,6 +270,14 @@ range(i::TypedEndpointsInterval{:closed,:open}; length::Integer) =
range(leftendpoint(i); step=width(i)/length, length=length)
range(i::TypedEndpointsInterval{:closed,:open}, len::Integer) = range(i; length=len)

"""
clamp(t, i::ClosedInterval)
Clamp the scalar `t` such that the result is in the interval `i`.
"""
clamp(t, i::TypedEndpointsInterval{:closed,:closed}) =
clamp(t, leftendpoint(i), rightendpoint(i))


"""
duration(iv)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,13 @@ struct IncompleteInterval <: AbstractInterval{Int} end
@test range(Interval{:closed,:open}(0..1); length=10) == range(0; step=1/10, length=10)
end

@testset "clamp" begin
@test clamp(1, 0..3) == 1
@test clamp(1.0, 1.5..3) == 1.5
@test clamp(1.0, 0..0.5) == 0.5
@test clamp.([pi, 1.0, big(10.)], Ref(2..9.)) == [big(pi), 2, 9]
end

@testset "IteratorSize" begin
@test Base.IteratorSize(ClosedInterval) == Base.SizeUnknown()
end
Expand Down

6 comments on commit e73b682

@hyrodium
Copy link
Collaborator

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Error while trying to register: Register Failed
@hyrodium, it looks like you are not a publicly listed member/owner in the parent organization (JuliaMath).
If you are a member/owner, you will need to change your membership to public. See GitHub Help

@hyrodium
Copy link
Collaborator

Choose a reason for hiding this comment

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

The registration with version v0.5.4 seems forgotten.

@dlfivefifty Could you run the registration?

@dlfivefifty
Copy link
Member

Choose a reason for hiding this comment

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

@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/56992

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.5.4 -m "<description of version>" e73b6823eddb8d31c2fd3a98ff87e5980657a68f
git push origin v0.5.4

@hyrodium
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, the registration PR was merged, but the TagBot is not working.
Log: https://github.com/JuliaMath/IntervalSets.jl/runs/5625237461?check_suite_focus=true#step:3:42

Please sign in to comment.