result

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package result provides free functions for transforming and combining Result[T] values from the gofp package.

These functions exist as free functions rather than methods because Go does not allow methods to introduce new type parameters. A method like Map[U any](f func(T) U) Result[U] is illegal in Go the type parameter U cannot be declared on the method itself.

Transformations

Map and FlatMap transform the value inside a Result, propagating errors automatically:

r := result.Map(gofp.Of(strconv.Atoi(s)), func(n int) string {
    return strconv.Itoa(n * 2)
})

Combining

All2, All3, AllOf combine multiple Results into one:

all := result.All2(parseAge(s), parseName(s))

AllOfCollectErrs collects all errors instead of short-circuiting:

all := result.AllOfCollectErrs(validateName(), validateEmail(), validateAge())

Batch Operations

MapAll, MapAllOk, Partition work on slices of Results:

saved, errs := result.Partition(result.MapAll(users, saveUser)...)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All2

func All2[A, B any](a gofp.Result[A], b gofp.Result[B]) gofp.Result[tuple.Pair[A, B]]

All2 returns Ok(Pair[A, B]) if both a and b are Ok. Returns the first Err encountered.

all := result.All2(parseAge(s), parseName(s))

func All3

func All3[A, B, C any](a gofp.Result[A], b gofp.Result[B], c gofp.Result[C]) gofp.Result[tuple.Triple[A, B, C]]

All3 returns Ok(Triple[A, B, C]) if all three results are Ok. Returns the first Err encountered.

all := result.All3(parseAge(s), parseName(s), parseEmail(s))

func AllErrors

func AllErrors[T any](results ...gofp.Result[T]) []error

AllErrors returns the errors from all Err results, ignoring Ok results. Returns nil if all results are Ok.

errs := result.AllErrors(r1, r2, r3)

func AllOf

func AllOf[T any](results ...gofp.Result[T]) gofp.Result[[]T]

AllOf returns Ok([]T) if all results are Ok. Returns the first Err encountered, short-circuiting the rest.

result.AllOf(validate(a), validate(b), validate(c))

func AllOfCollectErrs

func AllOfCollectErrs[T any](results ...gofp.Result[T]) gofp.Result[[]T]

AllOfCollectErrs returns Ok([]T) if all results are Ok. Unlike AllOf, it does not short-circuit — it collects all errors and returns them joined via errors.Join. Use for form validation or batch operations where all errors matter.

result.AllOfCollectErrs(validateName(), validateEmail(), validateAge())

func And

func And[T, U any](r gofp.Result[T], other gofp.Result[U]) gofp.Result[U]

And returns other if the Result is Ok, otherwise returns self (the Err). Use to chain two results where both must succeed but only the second value matters.

// Validate user exists, then return the permission check result
result.And(r, checkPermission(userID))

func AndThen

func AndThen[T, U any](r gofp.Result[T], f func(T) gofp.Result[U]) gofp.Result[U]

AndThen applies f to the contained value if Ok, returning the Result produced by f. If Err, the error is passed through unchanged.

This is the primary chaining method. Use when f can fail.

result.AndThen(r, func(u User) gofp.Result[Order] {
	return gofp.Of(getOrder(u.ID))
})

func FirstOk

func FirstOk[T any](results ...gofp.Result[T]) gofp.Result[T]

FirstOk returns the first Ok result. If all results are Err, returns Err with all errors joined via errors.Join. Returns ErrNoResults if no results are provided. Use for fallback chains: try cache → try db → try default.

result.FirstOk(tryCache(id), tryDB(id), getDefault())

func Flatten

func Flatten[T any](r gofp.Result[gofp.Result[T]]) gofp.Result[T]

Flatten removes one level of nesting from a Result[Result[T]].

var nested gofp.Result[gofp.Result[int]] = gofp.Ok(gofp.Ok(42))
result.Flatten(nested).Unwrap() // 42

func Map

func Map[T, U any](r gofp.Result[T], f func(T) U) gofp.Result[U]

Map applies f to the contained value if Ok, returning a new Result with the transformed value. If Err, the error is passed through unchanged.

Note: Map cannot change the Result to Err. Use AndThen if f can fail.

nameResult := result.Map(r, func(u User) string { return u.Name })

func MapAll

func MapAll[T, U any](slice []T, f func(T) gofp.Result[U]) []gofp.Result[U]

MapAll applies f to each element of slice, returning a slice of Results. All elements are processed regardless of individual errors.

results := result.MapAll(users, saveUser)

func MapAllOk

func MapAllOk[T, U any](slice []T, f func(T) gofp.Result[U]) gofp.Result[[]U]

MapAllOk applies f to each element of slice and collects all Ok values. Equivalent to AllOf(MapAll(slice, f)...) — returns the first Err encountered.

result.MapAllOk(ids, fetchUser)

func Partition

func Partition[T any](results ...gofp.Result[T]) (values []T, errs []error)

Partition splits results into separate Ok values and errors. Order is preserved within each group.

values, errs := result.Partition(result.MapAll(users, saveUser)...)

func PartitionResults

func PartitionResults[T any](results ...gofp.Result[T]) (oks []gofp.Result[T], errs []gofp.Result[T])

PartitionResults splits results into separate Ok and Err slices, preserving the Result wrapper unlike Partition. Use when you need to inspect or re-process the results themselves.

oks, errs := result.PartitionResults(results...)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL