Documentation
¶
Overview ¶
Package result provides a helper type for error handling without returning multiple values. It also provides methodes for dealing with the Result type.
Index ¶
- func Contains[S comparable, F any](value S, r Result[S, F]) bool
- func Count[S, F any](r Result[S, F]) int
- func DefaultValue[S, F any](success S, r Result[S, F]) S
- func DefaultWith[S, F any](defThunk func() S, r Result[S, F]) S
- func Exists[S, F any](predicate func(S) bool, r Result[S, F]) bool
- func Fold[S, F, State any](folder func(State, S) State, s State, r Result[S, F]) State
- func FoldBack[S, F, State any](folder func(S, State) State, r Result[S, F], s State) State
- func ForAll[S, F any](predicate func(S) bool, r Result[S, F]) bool
- func Get[S, F any](r Result[S, F]) S
- func HandleResult[S, F, R any](r Result[S, F], whenSuccess func(S) R, whenFailure func(F) R) R
- func IsNone[S, F any](r Result[S, F]) bool
- func IsSome[S, F any](r Result[S, F]) bool
- func Iter[S, F any](action func(S), r Result[S, F])
- func Lift[S, F any](f func() (S, error)) func() Result[S, F]
- func Lift1[T, S, F any](f func(T) (S, error)) func(T) Result[S, F]
- func Lift2[T1, T2, S, F any](f func(T1, T2) (S, error)) func(T1, T2) Result[S, F]
- func ToNullable[S, F any](r Result[S, F]) *S
- func ToSlice[S, F any](r Result[S, F]) []S
- type Result
- func Bind[S, F, R any](binder func(S) Result[R, F], r Result[S, F]) Result[R, F]
- func Failure[S, F any](v F) Result[S, F]
- func Flatten[S, F any](rr Result[Result[S, F], F]) Result[S, F]
- func Map[S, F, R any](mapping func(S) R, r Result[S, F]) Result[R, F]
- func Map2[S1, S2, F, R any](f func(S1, S2) R, r1 Result[S1, F], r2 Result[S2, F]) Result[R, F]
- func Map3[S1, S2, S3, F, R any](f func(S1, S2, S3) R, r1 Result[S1, F], r2 Result[S2, F], r3 Result[S3, F]) Result[R, F]
- func MapError[S, F any](mapping func(F) F, r Result[S, F]) Result[S, F]
- func OfNullable[S, F any](value *S) Result[S, F]
- func OrElse[S, F any](ifNone Result[S, F], r Result[S, F]) Result[S, F]
- func OrElseWith[S, F any](ifNoneThunk func() Result[S, F], r Result[S, F]) Result[S, F]
- func Success[S, F any](v S) Result[S, F]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[S comparable, F any](value S, r Result[S, F]) bool
Contains tests whether the result contains value.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](1)
input2 := result.Failure[int]("error")
fmt.Println(result.Contains(1, input), result.Contains(1, input2))
}
Output: true false
func Count ¶
Count returns 0 if this result is Failure. Otherwise returns 1.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](1)
input2 := result.Failure[int]("error")
fmt.Println(result.Count(input), result.Count(input2))
}
Output: 1 0
func DefaultValue ¶
DefaultValue returns the value of r if r is Success. Otherwise, it returns success.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, error](1)
input2 := result.Failure[int](fmt.Errorf("error"))
r := result.DefaultValue(2, input)
r2 := result.DefaultValue(2, input2)
fmt.Println(r, r2)
}
Output: 1 2
func DefaultWith ¶
DefaultWith returns the value of r if r is Success. Otherwise, it returns the output of defThunk.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](1)
input2 := result.Failure[int]("error")
r := result.DefaultWith(func() int { return 2 }, input)
r2 := result.DefaultWith(func() int { return 2 }, input2)
fmt.Println(r, r2)
}
Output: 1 2
func Exists ¶
Exists tests whether the value of r matches the predicate. If the Result is an error, it returns false.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](1)
input2 := result.Failure[int]("error")
r := result.Exists(func(value int) bool { return value > 2 }, input)
r2 := result.Exists(func(value int) bool { return value < 2 }, input)
r3 := result.Exists(func(value int) bool { return value < 2 }, input2)
fmt.Println(r, r2, r3)
}
Output: false true false
func Fold ¶
Fold applies the folder function to a Result with s being the initial state for the folder. If the Result is an Failure, the initial state is returned.
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](5)
r := result.Fold(func(state string, value int) string {
return state + strconv.Itoa(value)
}, "state:", input)
fmt.Println(r)
}
Output: state:5
func FoldBack ¶
FoldBack applies the folder function to a Result with s being in the initial state for the folder. If the Result is an Failure, the initial state is returned.
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](5)
r := result.FoldBack(func(value int, state string) string {
return state + strconv.Itoa(value)
}, input, "state:")
fmt.Println(r)
}
Output: state:5
func ForAll ¶
ForAll tests whether the value contained in the Result matches the predicate. It will always return true if the Result is a Failure.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input1 := result.Success[int, string](1)
input2 := result.Failure[int]("error")
r1 := result.ForAll(func(v int) bool { return v < 2 }, input1)
r2 := result.ForAll(func(v int) bool { return v > 2 }, input1)
r3 := result.ForAll(func(v int) bool { return v < 2 }, input2)
fmt.Println(r1, r2, r3)
}
Output: true false true
func HandleResult ¶
HandleResult accepts functions to handle a Result when it has a success or when it has an failure. This will panic if either of the functions are nil.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
res := result.Success[int, error](1)
err := result.HandleResult(res, func(i int) error {
fmt.Println(i)
return nil
}, func(err error) error {
fmt.Println(err)
return err
})
if err != nil {
panic(err)
}
}
Output: 1
func Lift ¶
Lift adapts a function that returns a value and an error into a function that returns a Result that will be Success if there is no error and Failure if there is an error.
func Lift1 ¶
Lift1 adapts a function that accepts one input and returns a value and an error into a function that returns a Result that will be Success if there is no error and Failure if there is an error.
func Lift2 ¶
Lift2 adapts a function that accepts two inputs and returns a value and an error into a function that returns a Result that will be Success if there is no error and Failure if there is an error.
func ToNullable ¶
ToNullable returns a pointer to the value in the Result if it is Success. If the Result is an Failure, it returns nil.
Types ¶
type Result ¶
type Result[S, F any] struct { // contains filtered or unexported fields }
Result is a helper type for error handling without returning multiple values. Any Result will either contain a value or an error.
func Bind ¶
Bind applies binder when result is Success and otherwise returns the Failure.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, error](2)
res := result.Bind(func(t int) result.Result[int, error] { return result.Success[int, error](t * 2) }, input)
if res.IsFailure() {
panic(res.FailureValue())
}
if !res.IsSuccess() {
panic("not ok")
}
fmt.Println(res.SuccessValue())
}
Output: 4
Example (Second) ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, error](2)
res := result.Bind(func(t int) result.Result[int, error] { return result.Failure[int](fmt.Errorf("error: %d", t)) }, input)
res = result.Bind(func(t int) result.Result[int, error] { return result.Success[int, error](t) }, res)
if res.IsSuccess() {
panic("should not be ok")
}
if !res.IsFailure() {
panic("should be error")
}
fmt.Println(res.FailureValue())
}
Output: error: 2
func Map ¶
Map applies mapping when result is Success and otherwise returns the Failure.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
input := result.Success[int, string](1)
res := result.Map(func(i int) string { return fmt.Sprint("input: ", i) }, input)
fmt.Println(res.SuccessValue())
}
Output: input: 1
func Map2 ¶
Map2 applies function f to two Results and returns the function's return value as a Result. If either Result is an Error, it returns the error as the Result.
func Map3 ¶
func Map3[S1, S2, S3, F, R any](f func(S1, S2, S3) R, r1 Result[S1, F], r2 Result[S2, F], r3 Result[S3, F]) Result[R, F]
Map3 applies function f to three Results and returns the function's return value as a Result. If any of the Results is an Error, it returns the error as the Result.
func MapError ¶
MapError applies mapping to the error when the result is Failure and otherwise returns the Success.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/result"
)
func main() {
mapping := func(err error) error {
return fmt.Errorf("%w: wrapped", err)
}
input := result.Success[int, error](1)
input2 := result.Failure[int](fmt.Errorf("inner error"))
res := result.MapError(mapping, input)
res2 := result.MapError(mapping, input2)
fmt.Println(res.String(), res2.String())
}
Output: 1 inner error: wrapped
func OfNullable ¶
OfNullable creates a result from a pointer. If the pointer is nil, the result will be a Failure with the the message "nil". If the pointer is not nil, the result will be Succeess of the value the pointer points to.
func OrElseWith ¶
OrElseWith returns r if it is Success or the Result returned from ifNoneThunk if r is an Error.
func (Result[_, F]) FailureValue ¶
func (r Result[_, F]) FailureValue() F
FailureValue returns the error if this Result is Error. Otherwise, it returns nil.
Example ¶
package main
import (
"fmt"
"strconv"
"github.com/flowonyx/functional/result"
)
func main() {
r := result.Failure[int]("bad argument")
r2 := result.Success[int, error](1)
fmt.Println(strconv.Quote(r.FailureValue()), r2.FailureValue())
}
Output: "bad argument" <nil>
func (Result[_, _]) IsFailure ¶
IsFailure tests if this Result has a failure.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/errors"
"github.com/flowonyx/functional/result"
)
func main() {
r := result.Success[int, error](1)
r2 := result.Failure[int](errors.BadArgumentErr)
fmt.Println(r.IsFailure(), r2.IsFailure())
}
Output: false true
func (Result[_, _]) IsNone ¶
IsNone is an alias for IsFailure to satisfy the option.Optional interface.
func (Result[_, _]) IsSome ¶
IsSome is an alias for IsSuccess to satisfy the option.Optional interface.
func (Result[_, _]) IsSuccess ¶
IsSuccess tests if this Result has a value.
Example ¶
package main
import (
"fmt"
"github.com/flowonyx/functional/errors"
"github.com/flowonyx/functional/result"
)
func main() {
r := result.Success[int, error](1)
r2 := result.Failure[int](errors.BadArgumentErr)
fmt.Println(r.IsSuccess(), r2.IsSuccess())
}
Output: true false
func (Result[S, _]) SuccessValue ¶
func (r Result[S, _]) SuccessValue() S
SuccessValue returns the value if this Result is Success. If the result is an error, Value returns the zero value of the value type.