# Julia equivalent of purrr::safely in R?

**URL:** <https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217>\
**Category:** New to Julia\
**Created:** [August 30, 2019, 7:25pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217 "2019-08-30T19:25:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abeburnett](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abeburnett/32/8974_2.png) [@abeburnett](https://discourse.julialang.org/u/abeburnett)\
**Post date:** [August 30, 2019, 7:25pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/1 "2019-08-30T19:25:58Z")

</div>

Hi again,

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?

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [August 30, 2019, 8:22pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/2 "2019-08-30T20:22:43Z")

</div>

Something like this?

```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"))

```

---

<div class="post-metadata">

**Author:** ![abeburnett](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abeburnett/32/8974_2.png) [@abeburnett](https://discourse.julialang.org/u/abeburnett)\
**Post date:** [August 30, 2019, 8:56pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/3 "2019-08-30T20:56:54Z")

</div>

> [@ggggggggg](#):
>
> function safely(f) x=try f() catch ex return nothing, ex end return x,nothing end

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

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [August 30, 2019, 9:13pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/5 "2019-08-30T21:13:07Z")

</div>

This seems to work. I don’t have a good sense of the downsides to this approach, but there are probably some.

```julia
julia> safewrap(f) = (x...) -> safely(()->f(x...))
safewrap (generic function with 1 method)

julia> z(x,y) = x+y
z (generic function with 1 method)

julia> Z=safewrap(z)
#11 (generic function with 1 method)

julia> Z(3,4)
(7, nothing)

julia> Z(3,3)
(6, nothing)

julia> Z(3)
(nothing, MethodError(z, (3,), 0x00000000000063f3))

```

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [August 30, 2019, 11:00pm UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/6 "2019-08-30T23:00:40Z")

</div>

try catch seems barely more verbose and much clearer

---

<div class="post-metadata">

**Author:** ![abeburnett](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abeburnett/32/8974_2.png) [@abeburnett](https://discourse.julialang.org/u/abeburnett)\
**Post date:** [August 31, 2019, 1:04am UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/7 "2019-08-31T01:04:40Z")

</div>

@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)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 31, 2019, 6:15am UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/8 "2019-08-31T06:15:33Z")

</div>

> [@abeburnett](#):
>
> I have a join which fails spectacularly (crashes the Julia REPL)

I think that investigating the cause of this would be the best way to solve your problem.

If it actually crashes the Julia process, a `try ... catch` block is unlikely to help you.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 31, 2019, 7:15am UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/9 "2019-08-31T07:15:04Z")

</div>

> [@abeburnett](#):
>
> Here’s what I’m trying to do: I have a join which fails spectacularly (crashes the Julia REPL)

You may want to try [Debugger.jl](https://github.com/JuliaDebug/Debugger.jl) to find out when/why it crashes.

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [August 31, 2019, 8:09am UTC](https://discourse.julialang.org/t/julia-equivalent-of-purrr-safely-in-r/28217/10 "2019-08-31T08:09:25Z")

</div>

Try

```julia
safewrap(f) = (args...; kwargs...) -> safely(() -> f(args...; kwargs...))

```

instead.
