From e9ec7a585feccc84fc9d205ddf6b284af8bc9ea8 Mon Sep 17 00:00:00 2001 From: kendal-s <99786241+kendal-s@users.noreply.github.com> Date: Tue, 19 Jul 2022 20:09:54 -0400 Subject: [PATCH] Allow TimeSpans to be broadcasted (#43) (#44) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Allow TimeSpans to be broadcasted Currently broadcasting TimeSpans returns an error without wrapping the TimeSpan in a `Ref`. It would be nice to be able to broadcast a Timespan without having to wrap it! Example: ``` foo = DataFrame(a=["test"], b=[TimeSpan(0, 100)]) foo.a .= "new test" # works fine! new_ts = TimeSpan(0, 200) foo.b .= new_ts # julia is angry! ❌ foo.b .= Ref(new_ts) # expected behavior! ✅ ``` * add tests * bump version (cherry picked from commit 902c49a210ead85584d5d21373d64f1a1163b88f) --- Project.toml | 2 +- src/TimeSpans.jl | 3 +++ test/runtests.jl | 10 ++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index a2d3b5c..5058eeb 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "TimeSpans" uuid = "bb34ddd2-327f-4c4a-bfb0-c98fc494ece1" authors = ["Beacon Biosignals, Inc."] -version = "0.2.8" +version = "0.2.9" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/TimeSpans.jl b/src/TimeSpans.jl index b880e29..4264dde 100644 --- a/src/TimeSpans.jl +++ b/src/TimeSpans.jl @@ -53,6 +53,9 @@ Base.in(x::TimePeriod, y::TimeSpan) = start(y) <= x < stop(y) Base.findall(pred::Base.Fix2{typeof(in), TimeSpan}, obj::AbstractArray) = invoke(findall, Tuple{Function, typeof(obj)}, pred, obj) Base.findall(pred::Base.Fix2{typeof(in), TimeSpan}, obj::Tuple) = invoke(findall, Tuple{Function, typeof(obj)}, pred, obj) +# allow TimeSpans to be broadcasted +Base.broadcastable(t::TimeSpan) = Ref(t) + ##### ##### pretty printing ##### diff --git a/test/runtests.jl b/test/runtests.jl index 5537c7b..22dff48 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -186,3 +186,13 @@ end @test length(i_spans) == 6 @test all(duration.(i_spans) .== Second(8)) end + +@testset "broadcast_spans" begin + test_vec = [TimeSpan(0, 100), TimeSpan(0, 200)] + test_vec .= TimeSpan(0, 300) + @test test_vec == [TimeSpan(0, 300), TimeSpan(0, 300)] + + test_vec = [] + test_vec .= TimeSpan(0, 300) + @test test_vec == [] +end