Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combine: map supports callable object #339

Merged
merged 1 commit into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/combine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
13 changes: 13 additions & 0 deletions test/combine.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ using MarketData
using TimeSeries


struct _TestType
end


@testset "combine" begin


Expand Down Expand Up @@ -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


Expand Down