Skip to content

Commit e73b682

Browse files
authored
Implemented clamp(t, i::ClosedInterval) (#77)
1 parent 4e5e2e9 commit e73b682

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "IntervalSets"
22
uuid = "8197267c-284f-5f27-9208-e0e47529a953"
3-
version = "0.5.3"
3+
version = "0.5.4"
44

55
[deps]
66
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/IntervalSets.jl

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module IntervalSets
22

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

77
using Statistics
88
import Statistics: mean
@@ -270,6 +270,14 @@ range(i::TypedEndpointsInterval{:closed,:open}; length::Integer) =
270270
range(leftendpoint(i); step=width(i)/length, length=length)
271271
range(i::TypedEndpointsInterval{:closed,:open}, len::Integer) = range(i; length=len)
272272

273+
"""
274+
clamp(t, i::ClosedInterval)
275+
276+
Clamp the scalar `t` such that the result is in the interval `i`.
277+
"""
278+
clamp(t, i::TypedEndpointsInterval{:closed,:closed}) =
279+
clamp(t, leftendpoint(i), rightendpoint(i))
280+
273281

274282
"""
275283
duration(iv)

test/runtests.jl

+7
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,13 @@ struct IncompleteInterval <: AbstractInterval{Int} end
705705
@test range(Interval{:closed,:open}(0..1); length=10) == range(0; step=1/10, length=10)
706706
end
707707

708+
@testset "clamp" begin
709+
@test clamp(1, 0..3) == 1
710+
@test clamp(1.0, 1.5..3) == 1.5
711+
@test clamp(1.0, 0..0.5) == 0.5
712+
@test clamp.([pi, 1.0, big(10.)], Ref(2..9.)) == [big(pi), 2, 9]
713+
end
714+
708715
@testset "IteratorSize" begin
709716
@test Base.IteratorSize(ClosedInterval) == Base.SizeUnknown()
710717
end

0 commit comments

Comments
 (0)