Skip to content

Commit

Permalink
Improve type-stability of ten function
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano committed Jul 24, 2017
1 parent dcd6695 commit 9979af8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
10 changes: 4 additions & 6 deletions src/ten.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ ten(d::Real, m::Real=0, s::Real=0) =
# make parsing of the string work.
function ten(strng::AbstractString)
# Convert strings into numbers, empty strings into 0s.
tmp = map(x-> x=="" ? 0 : float(x),
# Replace in the string multiple spaces or colons with a single
# space, strip leading whitespaces, and split the resulting string
# using the space as separator.
split(lstrip(replace(strng, r"(\:| )+", s" ")), " "))
# Replace in the string multiple spaces or colons with a single space, strip leading
# whitespaces, and split the resulting string using the space as separator.
tmp = map(x-> x=="" ? 0.0 : float(x), split(lstrip(replace(strng, r"(\:| )+", s" ")), " "))
# Concatenate "tmp" with 3 zeros, so that "angle" has at least 3 elements
# also with an empty string in input.
angle = vcat(tmp, zeros(Float64, 3))
angle = vcat(tmp, zeros(Float64, 3))::Vector{Float64}
ten(angle[1], angle[2], angle[3])
end

Expand Down
7 changes: 3 additions & 4 deletions test/utils-tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -545,8 +545,7 @@ end
# Test mag2flux
@testset "mag2flux" begin
@test @inferred(mag2flux(4.83, 21.12)) 4.1686938347033296e-11
@test mag2flux.([4.83], 21.12) [4.1686938347033296e-11]
@test flux2mag(mag2flux(15, ABwave=12.), ABwave=12) 15.0 # Inferred Type Error
@test flux2mag(mag2flux(15, ABwave=12.), ABwave=12) 15.0 # Inferred Type Error
@test @inferred(mag2flux(8.3)) 1.7378008287493692e-12
@test @inferred(mag2flux(8.3, 12)) 7.58577575029182e-9
@test mag2flux(8.3, ABwave=12) 3.6244115683017193e-7 # Inferred Type Error
Expand Down Expand Up @@ -811,9 +810,9 @@ end
@test @inferred(ten(0, -23, 34)) == @inferred(ten((0, -23, 34))) ==
@inferred(ten([0, -23, 34])) == ten(" : 0 -23 :: 34") == -0.37388888888888894
@test @inferred(ten(-0.0, 60)) == @inferred(ten((-0.0, 60))) ==
@inferred(ten([-0.0, 60])) == ten("-0.0 60") == -1.0 # Inferred Type Error
@inferred(ten([-0.0, 60])) == @inferred(ten("-0.0 60")) == -1.0
@test @inferred(ten(-5, -60, -3600)) == @inferred(ten((-5, -60, -3600))) ==
@inferred(ten([-5, -60, -3600])) == ten(" -5: :-60: -3600") == -3.0 # Inferred Type Error
@inferred(ten([-5, -60, -3600])) == @inferred(ten(" -5: :-60: -3600")) == -3.0
@test ten("") == 0.0
@test ten.([0, -0.0, -5], [-23, 60, -60], [34, 0, -3600]) ==
ten.([(0, -23,34), ":-0.0:60", (-5, -60, -3600)]) ==
Expand Down

0 comments on commit 9979af8

Please sign in to comment.