Skip to content

Commit

Permalink
Merge pull request #86 from tpgillam/tg/zap_until
Browse files Browse the repository at this point in the history
Implement zap_until
  • Loading branch information
tpgillam committed Mar 24, 2023
2 parents 5501dc1 + aa25013 commit 2a0a4e1
Show file tree
Hide file tree
Showing 5 changed files with 44 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 = "TimeDag"
uuid = "91963e24-6e66-4132-aeb8-436d9f37dbc7"
authors = ["Invenia Technical Computing Corporation", "Tom Gillam <[email protected]>"]
version = "0.1.23"
version = "0.1.24"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference/misc_ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Base.rand
```@docs
Base.filter
Base.skipmissing
zap_until
```

## Type conversion
Expand Down
2 changes: 2 additions & 0 deletions src/TimeDag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export block_node, constant, empty_node, iterdates, pulse, tea_file
# Alignment nodes.
export active_count, align, align_once, coalign, count_knots, first_knot, lag, right, left
export prepend, throttle
# Conditional nodes.
export zap_until
# Type conversion
export convert_value
# Other nodes
Expand Down
18 changes: 18 additions & 0 deletions src/ops/conditional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,21 @@ function Base.skipmissing(x::Node)
_is_empty(x) && return empty_node(T)
return obtain_node((x,), SkipMissing{T}())
end

# TODO a more efficient implementation for this would simply slice blocks as required,
# rather than evaluate the condition knotwise.
struct ZapUntil{T} <: UnaryNodeOp{T}
time::DateTime
end
stateless_operator(::ZapUntil) = true
operator!(op::ZapUntil{T}, t::DateTime, x) where {T} = t < op.time ? Maybe{T}() : Maybe(x)

"""
zap_until(x::Node, t) -> Node
Given a node `x`, return a node in which all knots strictly before `t` are omitted.
"""
function zap_until(x::Node{T}, t::DateTime) where {T}
_is_empty(x) && return x
return obtain_node((x,), ZapUntil{T}(t))
end
22 changes: 22 additions & 0 deletions test/ops/conditional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,26 @@ end
])
end

@testset "zap_until" begin
n = empty_node(Int64)
@test zap_until(n, DateTime(2000)) === n

@test _eval(zap_until(n1, DateTime(2000, 1, 3))) ==
Block([
DateTime(2000, 1, 3) => 3,
DateTime(2000, 1, 4) => 4
])

@test _eval(zap_until(n_boolean, DateTime(2000, 1, 3))) ==
Block([
DateTime(2000, 1, 3) => true,
DateTime(2000, 1, 4) => true
])

for cutoff in first(b4.times):Minute(133):last(b4.times)
@test _eval(zap_until(n4, cutoff)) ==
TimeDag._slice(b4, cutoff, last(b4.times) + Hour(1))
end
end

#! format: on

2 comments on commit 2a0a4e1

@tpgillam
Copy link
Owner Author

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/80292

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.24 -m "<description of version>" 2a0a4e19d61e2b889c302e0717d451f83a20a9e1
git push origin v0.1.24

Please sign in to comment.