diff --git a/NEWS.md b/NEWS.md index a6acba3e..f9d0e944 100644 --- a/NEWS.md +++ b/NEWS.md @@ -11,6 +11,17 @@ , in order to support do-syntax. The original one is deprecated. (issue #TBD) +* `map` supports callable object. (issue #TBD) + + ```julia + struct T end + (::T)(timestamp, x) = (timestamp, x + 42) + + t = T() + + map(t, ta) + ``` + ### 0.10.0 * add support for time series plotting via RecipesBase dependency (thank you @mkborregaard) diff --git a/src/combine.jl b/src/combine.jl index 1ac6c033..5bc67bab 100644 --- a/src/combine.jl +++ b/src/combine.jl @@ -121,7 +121,7 @@ end # map ###################### -function map(f::Function, ta::TimeArray) +function map(f, ta::TimeArray) timestamps = similar(ta.timestamp) values = similar(ta.values) @@ -131,8 +131,8 @@ function map(f::Function, ta::TimeArray) order = sortperm(timestamps) if length(ta.colnames) == 1 # Check for 1D to ensure values remains a 1D vector. - return TimeArray(timestamps[order], values[order], ta.colnames, ta.meta) + TimeArray(timestamps[order], values[order], ta.colnames, ta.meta) else - return TimeArray(timestamps[order], values[order, :], ta.colnames, ta.meta) + TimeArray(timestamps[order], values[order, :], ta.colnames, ta.meta) end end diff --git a/test/combine.jl b/test/combine.jl index 620a5a4f..a76af2a5 100644 --- a/test/combine.jl +++ b/test/combine.jl @@ -6,6 +6,10 @@ using MarketData using TimeSeries +struct _TestType +end + + @testset "combine" begin @@ -209,6 +213,15 @@ end @test length(b) == length(a) @test issorted(b.timestamp) end + + @testset "map callable object" begin + (::_TestType)(ts, x) = (ts, x + 42) + + ta = map(_TestType(), cl) + @test ta.timestamp == cl.timestamp + @test ta.values == cl.values .+ 42 + @test ta.meta == cl.meta + end end