-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up type conversions, functions, removed return type restricti…
…ons, added `passmissing()` to handle `missing` values, reserving `catch()` for parsing failures.
- Loading branch information
Karandeep Singh
committed
Apr 12, 2023
1 parent
2a7d480
commit 1f24583
Showing
3 changed files
with
107 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,47 @@ | ||
|
||
function as_float(value)::Union{AbstractFloat, Missing} | ||
""" | ||
$docstring_as_float | ||
""" | ||
function as_float(value) | ||
try | ||
convert(AbstractFloat, value) | ||
passmissing(convert)(Float64, value) | ||
catch | ||
missing | ||
missing # if parsing failure | ||
end | ||
end | ||
|
||
function as_float(value::String)::Union{Float64, Missing} | ||
function as_float(value::AbstractString) | ||
try | ||
parse(Float64, value) | ||
passmissing(parse)(Float64, value) | ||
catch | ||
missing | ||
missing # if parsing failure | ||
end | ||
end | ||
|
||
function as_integer(value)::Union{Integer, Missing} | ||
""" | ||
$docstring_as_integer | ||
""" | ||
function as_integer(value) | ||
try | ||
convert(Integer, value) | ||
passmissing(floor)(value) |> | ||
x -> passmissing(convert)(Int64, x) | ||
catch | ||
missing | ||
missing # if parsing failure | ||
end | ||
end | ||
|
||
function as_integer(value::String)::Union{Int64, Missing} | ||
function as_integer(value::AbstractString) | ||
try | ||
parse(Int64, value) | ||
passmissing(parse)(Float64, value) |> | ||
x -> passmissing(floor)(x) |> | ||
x -> passmissing(convert)(Int64, x) | ||
catch | ||
missing | ||
missing # if parsing failure | ||
end | ||
end | ||
|
||
function as_string(value)::String | ||
string(value) | ||
""" | ||
$docstring_as_string | ||
""" | ||
function as_string(value) | ||
passmissing(string)(value) | ||
end |