Skip to content

Commit

Permalink
combine: map supports callable object (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin authored Nov 8, 2017
1 parent 38e8703 commit 42f5ed1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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

0 comments on commit 42f5ed1

Please sign in to comment.