From f929d43354c536865d9158a9170f5a1c7bf404a7 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sun, 24 Apr 2022 02:20:48 -0400 Subject: [PATCH 1/2] Add `Try.unwrap_or_else` --- src/Try.jl | 1 + src/branch.jl | 43 ++++++++++++++++++++++++--------- src/docs/unwrap_or_else.md | 15 ++++++++++++ test/TryTests/src/test_tools.jl | 5 ++++ 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 src/docs/unwrap_or_else.md diff --git a/src/Try.jl b/src/Try.jl index bf48984..3b89ba6 100644 --- a/src/Try.jl +++ b/src/Try.jl @@ -39,6 +39,7 @@ function var"@?" end function and_then end function or_else end +function unwrap_or_else end module Internal diff --git a/src/branch.jl b/src/branch.jl index 989624f..0c59d6d 100644 --- a/src/branch.jl +++ b/src/branch.jl @@ -66,12 +66,6 @@ macro and_return(ex) end end -function Try.and_then(f::F) where {F} - function and_then_closure(result) - Try.and_then(f, result) - end -end - function Try.and_then(f, result) br = branch(result) if br isa Continue @@ -81,17 +75,44 @@ function Try.and_then(f, result) end end -function Try.or_else(f::F) where {F} - function or_else_closure(result) - Try.or_else(f, result) +function Try.or_else(f, result) + br = branch(result) + if br isa Break + f(valueof(br)) + else + br.result end end -function Try.or_else(f, result) +function Try.unwrap_or_else(f, result) br = branch(result) if br isa Break f(valueof(br)) else - br.result + valueof(br.result) + end +end + +### +### Currying +### + +# TODO: Automate currying? + +function Try.and_then(f::F) where {F} + function and_then_closure(result) + Try.and_then(f, result) + end +end + +function Try.or_else(f::F) where {F} + function or_else_closure(result) + Try.or_else(f, result) + end +end + +function Try.unwrap_or_else(f::F) where {F} + function unwrap_or_else(result) + Try.unwrap_or_else(f, result) end end diff --git a/src/docs/unwrap_or_else.md b/src/docs/unwrap_or_else.md new file mode 100644 index 0000000..7273f00 --- /dev/null +++ b/src/docs/unwrap_or_else.md @@ -0,0 +1,15 @@ + Try.unwrap_or_else(_, Ok(value)) -> value + Try.unwrap_or_else(f, Err(x)) -> f(x) + +Unwrap an [`Ok`](@ref) value or compute a result from the value wrapped in [`Err`](@ref). + +# Examples +```julia +julia> using Try + +julia> Try.unwrap_or_else(length, Try.Ok(1)) +1 + +julia> Try.unwrap_or_else(length, Try.Err("four")) +4 +``` diff --git a/test/TryTests/src/test_tools.jl b/test/TryTests/src/test_tools.jl index 0469c7b..532b498 100644 --- a/test/TryTests/src/test_tools.jl +++ b/test/TryTests/src/test_tools.jl @@ -67,6 +67,11 @@ function test_or_return() @test Try.unwrap(try_map_prealloc2(x -> x + 1, 1:3)) == 2:4 end +function test_unwrap_or_else() + @test Try.unwrap_or_else(length, Try.Ok(1)) == 1 + @test Try.unwrap_or_else(length, Try.Err("four")) == 4 +end + function test_curry() value = tryconvert(String, 1) |> From 6e5f8c43624f321caae6946339a833fac9d100c2 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sun, 24 Apr 2022 02:34:35 -0400 Subject: [PATCH 2/2] Fix `Try.unwrap_or_else` --- src/branch.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/branch.jl b/src/branch.jl index 0c59d6d..44db4f7 100644 --- a/src/branch.jl +++ b/src/branch.jl @@ -89,7 +89,7 @@ function Try.unwrap_or_else(f, result) if br isa Break f(valueof(br)) else - valueof(br.result) + valueof(br) end end