Skip to content

Commit

Permalink
Allow TimeSpans to be broadcasted (#43) (#44)
Browse files Browse the repository at this point in the history
* 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 902c49a)
  • Loading branch information
kendal-s authored Jul 20, 2022
1 parent adef4d2 commit e9ec7a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/TimeSpans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
#####
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit e9ec7a5

@ararslan
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/64573

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

Please sign in to comment.