-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
410 additions
and
77 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Experimental | ||
|
||
```@meta | ||
CurrentModule = Main | ||
``` | ||
|
||
## [Customizing short-circuit evaluation](@id customize-short-circuit) | ||
|
||
```@docs | ||
TryExperimental.branch | ||
TryExperimental.Break | ||
TryExperimental.Continue | ||
TryExperimental.resultof | ||
TryExperimental.valueof | ||
``` |
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
TryExperimental.Break(result) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
TryExperimental.Continue(result) |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
TryExperiment.branch(result) -> Continue(result) | ||
TryExperiment.branch(result) -> Break(result) | ||
|
||
`branch` implements a short-circuiting evaluation API. It must return a `Continue` or a | ||
`Break`. |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
TryExperimental.resultof(branch) -> result | ||
TryExperimental.resultof(branch::Continue{<:Ok}) -> result::Ok | ||
TryExperimental.resultof(branch::Break{<:Err}) -> result::Err | ||
TryExperimental.resultof(branch::Continue{<:Some}) -> result::Some | ||
TryExperimental.resultof(branch::Break{Nothing}) -> nothing |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
TryExperimental.valueof(branch) -> value | ||
TryExperimental.valueof(branch::Continue{Ok{T}}) -> value::T | ||
TryExperimental.valueof(branch::Break{Err{T}}) -> value::T | ||
TryExperimental.valueof(branch::Continue{Some{T}}) -> value::T | ||
TryExperimental.valueof(branch::Break{Nothing}) -> nothing |
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
### | ||
### Experimental API | ||
### | ||
|
||
# This is very similar to Rust's `Try`` trait and `ControlFlow` enum. | ||
# https://doc.rust-lang.org/std/ops/trait.Try.html | ||
# https://doc.rust-lang.org/std/result/enum.Result.html | ||
# https://doc.rust-lang.org/std/ops/enum.ControlFlow.html | ||
|
||
struct Break{T} | ||
result::T | ||
end | ||
|
||
struct Continue{T} | ||
result::T | ||
end | ||
|
||
function branch end | ||
function resultof end | ||
function valueof end | ||
|
||
### | ||
### Implementation | ||
### | ||
|
||
branch(ok::Ok) = Continue(ok) | ||
branch(err::Err) = Break(err) | ||
branch(result::AbstractResult) = | ||
if Try.isok(result) | ||
Continue(result) | ||
else | ||
Break(result) | ||
end | ||
|
||
resultof(br) = br.result | ||
|
||
valueof(br::Continue{<:AbstractResult}) = Try.unwrap(br.result) | ||
valueof(br::Break{<:AbstractResult}) = Try.unwrap_err(br.result) | ||
|
||
branch(some::Some) = Continue(some) | ||
branch(::Nothing) = Break(nothing) | ||
|
||
valueof(br::Continue{<:Some}) = something(br.result) | ||
valueof(::Break{Nothing}) = nothing | ||
|
||
const var"@or_return" = var"@?" | ||
macro or_return(ex) # aka @? | ||
quote | ||
br = branch($(esc(ex))) | ||
if br isa Break | ||
return br.result | ||
else | ||
valueof(br) | ||
end | ||
end | ||
end | ||
|
||
macro and_return(ex) | ||
quote | ||
br = branch($(esc(ex))) | ||
if br isa Continue | ||
return br.result | ||
else | ||
valueof(br) | ||
end | ||
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 | ||
f(valueof(br)) | ||
else | ||
br.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.or_else(f, result) | ||
br = branch(result) | ||
if br isa Break | ||
f(valueof(br)) | ||
else | ||
br.result | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
@? result | ||
|
||
Evaluates to an unwrapped "success" result value; return `result` if it is a "failure." | ||
|
||
If `result` is an `Ok` or a `Some`, `@?` is equivalent to unwrapping the value. If `result` | ||
is an `Err` or `nothing`, `@?` is equivalent to `return`. | ||
|
||
| Invocation | Equivalent code | | ||
|:--- |:--- | | ||
| `@? Ok(value)` | `value` | | ||
| `@? Err(value)` | `return value` | | ||
| `@? Some(value)` | `value` | | ||
| `@? nothing` | `return nothing` | | ||
|
||
See also: [`@and_return`](@ref), [`and_then`](@ref), [`or_else`](@ref). | ||
|
||
# Extended help | ||
|
||
## Examples | ||
|
||
```julia | ||
using Try, TryExperimental | ||
|
||
function try_map_prealloc(f, xs) | ||
T = @? trygeteltype(xs) # macro-based short-circuiting | ||
n = @? trygetlength(xs) | ||
ys = Vector{T}(undef, n) | ||
for (i, x) in zip(eachindex(ys), xs) | ||
ys[i] = f(x) | ||
end | ||
return Ok(ys) | ||
end | ||
|
||
Try.unwrap(try_map_prealloc(x -> x + 1, 1:3)) | ||
|
||
# output | ||
3-element Vector{Int64}: | ||
2 | ||
3 | ||
4 | ||
``` |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Try.@and_return result -> result′ | ||
|
||
Evaluate `f(value)` if `result` is a "success" wrapping a `value`; otherwise, a "failure" | ||
`value` as-is. | ||
|
||
| Invocation | Equivalent code | | ||
|:--- |:--- | | ||
| `@and_return Ok(value)` | `value` | | ||
| `@and_return err::Err` | `return err` | | ||
| `@and_return Some(value)` | `value` | | ||
| `@and_return nothing` | `return nothing` | | ||
|
||
See also: [`@?`](@ref) [`and_then`](@ref), [`or_else`](@ref). | ||
|
||
# Extended help | ||
|
||
## Examples | ||
|
||
Let's define a function `nitems` that works like `length` but falls back to iteration-based | ||
counting: | ||
|
||
```julia | ||
using Try, TryExperimental | ||
|
||
function trygetnitems(xs) | ||
Try.@and_return trygetlength(xs) | ||
Ok(count(Returns(true), xs)) | ||
end | ||
|
||
nitems(xs) = Try.unwrap(trygetnitems(xs)) | ||
|
||
nitems(1:3) | ||
|
||
# output | ||
3 | ||
``` | ||
|
||
`nitems` works with arbitrary iterator, including the ones that does not have `length`: | ||
|
||
```julia | ||
ch = foldl(push!, 1:3; init = Channel{Int}(3)) | ||
close(ch) | ||
|
||
nitems(ch) | ||
|
||
# output | ||
3 | ||
``` |
Oops, something went wrong.