Results.Functions: Functions and Combinators
Exported members
Input/Output functions
Results.Functions.to_nullable — Functionto_nullable(val::Option{T})::Union{T, Nothing}
to_nullable(val::Result{T, E})::Union{T, Nothing}Convert a Result{T, E} or Option{T} into a simple nullable value Union{T, Nothing}. Note that this may lose structure (this transformation is non-injective, as Some(nothing) and nothing map to the same value).
Results.Functions.to_option — Functionto_option(value::Union{T, Nothing})::Option{T}
to_option(value::Result)::OptionConverts a nullable value to an Option. Also converts a Result to an Option, turning Ok into Some and Err into None.
Examples
julia> to_option("value")
Some("value")
julia> to_option(Some(5))
Some(Some(5))
julia> to_option(nothing) |> println
nothingResults.Functions.to_result — Functionto_result(value::T, err)::Ok{T}
to_result(value::None, err::E)::Err{E}
to_result(value::None, err::Function)::ErrConverts a nullable value to a Result, using the supplied error value in place of a nothing. Also converts an Option into a Result, turning Some into Ok and None into Err.
Examples
julia> to_result("value", "error")
Ok("value")
julia> to_result(nothing, "error")
Err("error")
julia> to_result("value", () -> println("lazily calculates errors"))
Ok("value")
julia> to_result(Ok(5), nothing)
Ok(Ok(5))Results.Functions.to_result — Methodto_result(err)::FunctionPartially-applied version of to_result.
Examples
julia> 5 |> to_result("error")
Ok(5)
julia> nothing |> to_result("error")
Err("error")Results.Functions.unwrap — Functionunwrap(o::Option{T})::T
unwrap(r::Result{T, E})::T
unwrap(o::Union{Option, Result}, error::Function)
unwrap(o::Union{Option, Result}, error::Exception)
unwrap(o::Union{Option, Result}, error::String)Unwrap an Ok value. Throws an error if r is Err instead.
In the two argument form, error is raised if it is an Exception. If it is a string, it is passed as a message to UnwrapError. If it is a function, it is called with an error value to produce an error.
Examples
julia> unwrap(Ok(5))
5
julia> unwrap(Err(0))
ERROR: Results.Types.UnwrapError("unwrap() called on an Err: 0")
julia> unwrap(none)
ERROR: Results.Types.UnwrapError("unwrap() called on None")
julia> unwrap(none, "value is none")
ERROR: Results.Types.UnwrapError("value is none")
julia> unwrap(nothing, BoundsError([1,2]))
ERROR: BoundsError: attempt to access 2-element Array{Int64,1}
julia> unwrap(Err(5), v -> "Error value '" * string(v) * "'")
ERROR: Results.Types.UnwrapError("Error value '5'")Results.Functions.unwrap_or — Functionunwrap_or(o::Option{T}, default)::T
unwrap_or(r::Result{T, E}, default)::TUnwrap an Ok value, or return default. default may be T or a function returning T.
Examples
julia> unwrap_or(Ok("value"), "error")
"value"
julia> unwrap_or(Err(5), "error")
"error"
julia> unwrap_or(Some("value"), () -> begin println("Generating error"); "error" end)
"value"
julia> unwrap_or(None(), () -> begin println("Generating error"); "error" end)
Generating error
"error"Combinators
Results.Functions.and_then — Methodand_then(result::Result, funcs...)::ResultBind result to the proceeding functions. If result is Ok, its contents will be passed to each function in turn. Any Err value will be returned immediately.
Examples
julia> Ok("Build") ⊗ val -> Ok(string(val, " a ")) ⊗ val -> Ok(string(val, "string"))
Ok("Build a string")
julia> Err("Error") ⊗ val -> Ok(string(val, " a ")) ⊗ val -> Ok(string(val, "string"))
Err("Error")
julia> Ok("Build") ⊗ val -> Err("Error") ⊗ function (val) error("long circuited"); Ok("value") end
Err("Error")Results.Functions.and_then — Methodand_then(func::Base.Callable)::FunctionPartially-applied version of and_then.
Results.Functions.and_then — Methodand_then(option::Option, funcs...)::OptionBind option to the proceeding functions. While option is Some, its contents will be passed to each function in turn. Any None will be returned immediately.
Examples
julia> Some("Build") ⊗ val -> Some(string(val, " a ")) ⊗ val -> Some(string(val, "string"))
Some("Build a string")
julia> none ⊗ val -> Some(string(val, " a ")) ⊗ val -> Some(string(val, "string"))
julia> Some("Build") ⊗ val -> none ⊗ function (val) error("long circuited"); Some("value") endResults.Functions.flatten — Functionflatten(o::Option{Option{T}})::Option{T}
flatten(r::Result{Result{T, E1}, E2})::Result{T, Union{E1, E2}}Flatten one layer of a nested Option or Result type.
Examples
julia> Ok(Ok(5)) |> flatten
Ok(5)
julia> Ok(Err("inner")) |> flatten
Err("inner")
julia> Some(Some(Some(5))) |> flatten
Some(Some(5))
julia> Ok(Some(5)) |> flatten # mixed Option and Result types not supported
ERROR: MethodErrorResults.Functions.map_err — Functionmap_err(f, result::Result)::ResultMap f over the contents of an Err value, leaving an Ok value untouched.
Results.Functions.map_err — Methodmap_err(f)::FunctionPartially-applied version of map_err.
Results.Functions.ok — Functionok(r::Result{T, E})::Option{T}Convert a Result into an Option, discarding any Err value.
Examples
julia> ok(Ok(5))
Some(5)
julia> ok(Err("error")) |> println
nothingResults.Functions.ok_or — Functionok_or(o::Some{T}, err)::Ok{T}
ok_or(::None, err::E)::Err{E}
ok_or(::None, err::Function)::ErrConvert an Option into a Result, using the supplied error value in place of None.
Examples
julia> ok_or(Some(5), "error")
Ok(5)
julia> ok_or(None(), "error")
Err("error")
julia> ok_or(None(), () -> "lazy error")
Err("lazy error")
julia> None() |> ok_or("partially applied")
Err("partially applied")Results.Functions.ok_or — Methodok_or(err)::FunctionPartially-applied version of ok_or.
Results.Functions.try_collect — Methodtry_collect(iter)
try_collect(result::Result, results...)::Result{Vector}
try_collect(option::Option, options...)::Option{Vector}Collect an iterator of Options or Results into a single Option or Result containing an array. Short-circuits on error.
Examples
julia> try_collect([Ok(5), Ok(10), Ok(3)])
Ok([5, 10, 3])
julia> try_collect([Ok(10), Err("err1"), Err("err2")])
Err("err1")
julia> try_collect([Ok(10), None(), Ok(5)]) # be careful mixing Result with Option!
Ok(Union{Nothing, Int64}[10, nothing, 5])Results.Functions.try_collect_option — Functiontry_collect_option(iter)::Option{Vector}
try_collect_option(options...)::Option{Vector}Version of try_collect specialized for use with Option Prefer this over try_collect whenever possible to prevent surprising behavior.
Results.Functions.try_collect_result — Functiontry_collect_result(iter)::Result{Vector}
try_collect_result(results...)::Result{Vector}Version of try_collect specialized for use with Result. Prefer this over try_collect whenever possible to prevent surprising behavior.
Results.Functions.try_map — Functiontry_map(f, opt::Option...)::Option
try_map(f, rslt::Result...)::Result
try_map(f::Option, rslt::Option...)::Option
try_map(f::Result, rslt::Result...)::ResultMap f over the contents of an Ok or Some value, leaving a None or Err value untouched.
Example
julia> try_map((x) -> 2*x, Ok(5))
Ok(10)
julia> try_map(+, Err("First error"), Err("Second error"))
Err("First error")
julia> try_map.((x) -> 2*x, [Ok(5), Err("missing value")])
2-element Array{Any,1}:
Ok(10)
Err("missing value")
julia> try_map(Ok(+), Ok(5), Ok(10))
Ok(15)Results.Functions.try_map — Methodtry_map(f::Callable)::FunctionPartially-applied version of try_map.
Predicate functions
Results.Functions.has_val — Functionhas_val(result::Union{Result, Option})::BoolReturns true for a successful Result or Option.
Results.Functions.is_err — Functionis_err(r::Result)::BoolReturn whether a Result is Err.
Results.Functions.is_err — Methodis_err(r::Result, value)::BoolReturn whether a Result contains an error equal to value.
Results.Functions.is_none — Methodis_none(o::Option)::BoolReturn whether an Option is None. Equivalent to Base.isnothing.
Results.Functions.is_ok — Functionis_ok(r::Result)::BoolReturn whether a Result is Ok or Err.
Results.Functions.is_ok — Methodis_ok(r::Result, value)::BoolReturn whether a Result contains a value equal to value.
Results.Functions.is_some — Functionis_some(o::Option)::BoolReturn whether an Option is Some.
Results.Functions.is_some — Methodis_some(o::Option, value)::BoolReturn whether an Option contains a value equal to value.
Un-exported members
Results.Functions.:← — FunctionShorthand for try_map. Enter as \leftarrow.
Example
julia> Ok(x -> 2*x) ← Ok(5)
Ok(10)
julia> x -> 2*x ← Ok(5) # Be careful with precedence!
#1 (generic function with 1 method)
julia> (x -> 2*x) ← (x -> x+7) ← Some(3)
Some(20)Results.Functions.:→ — FunctionArgument-swapped version of try_map. Enter as \rightarrow.
Example
julia> Some(5) → x -> 2*x
Some(10)
julia> Ok(5) → (x -> 2*x) → (x -> x+7) # arrows in Julia are right-associative!
ERROR: MethodError
julia> Ok(5) → (x -> 2*x) ∘ (x -> x+7) # but this will work
Ok(24)Results.Functions.:⊗ — FunctionShorthand for and_then (monadic bind). Enter with \otimes
Results.Functions.strip_option_type — Methodstrip_option_type(ty::Type)::TypeReturn the passed type with one layer of Option values stripped.
Results.Functions.strip_result_type — Methodstrip_result_type(ty::Type)::TypeReturn the passed type with one layer of Result values stripped.
Base methods extended
Base.:! — Method(!)(result::Result{T,E})::Result{E,T}Flip an Ok value to an Err value and vice versa.
Base.:& — Method(&)(result::Result, results...)::Result
(&)(option::Option, options...)::OptionReturn the final Ok/Some only if every argument is Ok/Some. Otherwise return the first Err value.
Values can be supplied either as Result/Options or as functions that yield a Result/Option.
Examples
julia> Ok("v1") & Ok("v2") & () -> Ok("v3")
Ok("v3")
julia> Ok("v1") & () -> Err(2) & () -> Ok("v2")
Err(2)
julia> Some(5) & Some(10) & None()Base.:| — Method(|)(result::Result, results...)::Result
(|)(option::Option, options...)::OptionReturn the first Ok value found. If no arguments are Ok, return the final Err value.
Values can be supplied either as Results or as functions that yield a Result.
Examples
julia> Err("err1") | Ok("v2") | () -> Ok("v3")
Ok("v2")
julia> Err("err1") | () -> Err("err2") | Err("err3")
Err("err3")