I’m looking to see if anyone has already created a library like purrr in R, which offers the excellent functionality of safely. For anyone unfamiliar, safely is awesome for capturing side effects. It’s like try-catch, but more convenient. In other words, it yields an object containing both a result and an error. If error is populated then you know whatever you were trying to do failed, and if not then whatever you were doing succeeded. You simply check to see if error is null/not-null and handle however you please (push a notification, write to a log, etc.).
I’ve used this for wrapping httr API calls, or for wrapping joins, …pretty much everything.
Is there anything like this for side effect handling in Julia?
julia> function safely(f)
x=try
f()
catch ex
return nothing, ex
end
return x,nothing
end
safely (generic function with 1 method)
julia> f() = 4
f (generic function with 1 method)
julia> g() = error("not 4")
g (generic function with 1 method)
julia> safely(f)
(4, nothing)
julia> safely(g)
(nothing, ErrorException("not 4"))
Yep, almost exactly like that. Except, the purrr::safely package allows us to wrap other functions in a generalized way. E.g., I could do something like safe_fromJSON = purrr::safely(jsonlite::fromJSON) or safe_GET = purrr::safely(httr::GET). Then I could use, say, safe_GET in-place of httr::GET. E.g., httr::GET(get_URL) vs safe_GET(get_URL)
Does this make sense? Ideas on how to modify the function you shared to achieve this? Thanks for the rapid response, @ggggggggg
@bramtayl I see your point, but I really appreciate convenience functions like the one @ggggggggg shared.
@ggggggggg Here’s what I’m trying to do: I have a join which fails spectacularly (crashes the Julia REPL), and I want to wrap it in such a way that failure of the join doesn’t kill the running Julia process. I have both the safely function you describe, and safewrap but the following code doesn’t work:
safe_join = safewrap(join) safe_join(submission_df, submission_query_df, on = [:audience, :subdomain, :reddit_date])
The error is “function #43 doesn’t accept keyword arguments”.
I really appreciate your help trying to crack this nut as I stumble through learning Julia (after years in R/tidyverse)