Skip to content

Commit

Permalink
Added join_dates and MultiSubs.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpapp committed Dec 6, 2017
1 parent 0f45f32 commit 191a1e7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/AMDB.jl
Expand Up @@ -4,6 +4,7 @@ using ArgCheck: @argcheck
using ByteParsers: parsenext, isparsed, Skip
using CodecZlib: GzipDecompressorStream
using DocStringExtensions: SIGNATURES
using DiscreteRanges: DiscreteRange
using EnglishText: ItemQuantity
using FlexDates: FlexDate
# FIXME commented out selective import until
Expand Down Expand Up @@ -359,5 +360,41 @@ function preview_column(colname;
values
end


# tuple processing (first pass)

"""
$SIGNATURES
Join the second and the third argument as a DiscreteRange compressed dates.
"""
join_dates(record) = _join_dates(record...)

@inline _join_dates(id, spell_start, spell_end, rest...) =
id, DiscreteRange(AMDB_Date(spell_start), AMDB_Date(spell_end)), rest...

struct MultiSubs{P, F}
functions::F
end

"""
$SIGNATURES
Return a callable that maps tuples by maps elements at `positions` with the
corresponding function in `functions`, leaving the rest of the elements alone.
"""
function MultiSubs(positions::NTuple{N, Int}, functions::F) where {N, F}
@argcheck length(positions) == length(functions)
@argcheck allunique(positions) && all(positions .> 0)
MultiSubs{positions, F}(functions)
end

@generated function (m::MultiSubs{P})(x::NTuple{N}) where {P, N}
result = Any[:(x[$i]) for i in 1:N]
for (i, p) in enumerate(P)
result[p] = :((m.functions[$i])(x[$p]))
end
:(tuple($(result...)))
end

end # module
21 changes: 20 additions & 1 deletion test/runtests.jl
@@ -1,14 +1,19 @@
using Base.Test

using ByteParsers: Skip
using DiscreteRanges

using AMDB:
# errors
FileError, FileErrors, log_error,
# autoindexing
AutoIndex,
# dates
AMDB_Date,
# utilities
narrowest_Int, to_narrowest_Int, column_parsers, TupleMap
narrowest_Int, to_narrowest_Int, column_parsers, TupleMap,
# tuples
join_dates, MultiSubs

# write your own tests here
@testset "paths" begin
Expand Down Expand Up @@ -75,3 +80,17 @@ end
c = column_parsers(["a", "b", "c", "d", "e"], ["b" => :b, "d" => :d])
@test c == (Skip(), :b, Skip(), :d)
end

@testset "joindates" begin
id = 99
d1 = Date(1980, 1, 1)
d2 = Date(1990, 1, 1)
rest = (:misc, 42, :stuff)
@test join_dates((id, d1, d2, rest...)) ==
(id, DiscreteRange(AMDB_Date(d1), AMDB_Date(d2)), rest...)
end

@testset "multisubs" begin
m = MultiSubs((1, 3), (x->x^2, x->x^3))
@test @inferred(m((2, 3, 5))) (4, 3, 125)
end

0 comments on commit 191a1e7

Please sign in to comment.