mur

package
v0.0.0-...-f1dd236 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package mur provides type-changing utility functions for `Result[T]`. These functions enable transformations where the Ok type changes (T→U), which cannot be expressed as methods due to Go's type system limitations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FlatMapTo

func FlatMapTo[T any, U any](r mu.Result[T], f func(T) mu.Result[U]) mu.Result[U]

FlatMapTo applies a function to an [Ok] value, transforming T to U. If the `Result[T]` is an [Err], it returns `Err[U]` with the same error. For type-changing transformations (T→U), use this instead of `Result.FlatMap`. See package docs for details.

Example
package main

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/appthrust/mu"
	"github.com/appthrust/mu/mur"
)

func main() {
	// FlatMapTo with Ok value (int to Result[string])
	okInt := mu.Ok(42)
	okStr := mur.FlatMapTo(okInt, func(x int) mu.Result[string] {
		if x > 0 {
			return mu.Ok(strconv.Itoa(x))
		}
		return mu.Err[string](errors.New("non-positive number"))
	})
	fmt.Println("Ok(42) flat mapped to string:", okStr.OrZero())

	// FlatMapTo returning error
	okNegative := mu.Ok(-10)
	negativeResult := mur.FlatMapTo(okNegative, func(x int) mu.Result[string] {
		if x > 0 {
			return mu.Ok(strconv.Itoa(x))
		}
		return mu.Err[string](errors.New("non-positive number"))
	})
	fmt.Println("Ok(-10) flat mapped (negative):", negativeResult.ErrOrZero().Error())

	// FlatMapTo with Err value (preserves original error)
	errInt := mu.Err[int](errors.New("original error"))
	errStr := mur.FlatMapTo(errInt, func(x int) mu.Result[string] {
		return mu.Ok(strconv.Itoa(x))
	})
	fmt.Println("Err flat mapped to string:", errStr.ErrOrZero().Error())

	// FlatMapTo with division (safe operation)
	safeDiv := mur.FlatMapTo(okInt, func(x int) mu.Result[float64] {
		if x != 0 {
			return mu.Ok(100.0 / float64(x))
		}
		return mu.Err[float64](errors.New("division by zero"))
	})
	fmt.Println("Ok(42) safe division:", safeDiv.OrZero())

}
Output:

Ok(42) flat mapped to string: 42
Ok(-10) flat mapped (negative): non-positive number
Err flat mapped to string: original error
Ok(42) safe division: 2.380952380952381

func Fold

func Fold[T any, U any](r mu.Result[T], onOk func(T) U, onErr func(error) U) U

Fold applies pattern matching functions to a `Result[T]`, returning U. Calls onOk for [Ok] values, onErr for [Err] values. For type-changing pattern matching (T→U), use this instead of method-based folding. See package docs for details.

Example
package main

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/appthrust/mu"
	"github.com/appthrust/mu/mur"
)

func main() {
	// Fold with Ok value (int to string)
	okInt := mu.Ok(42)
	result := mur.Fold(okInt,
		func(x int) string { return "success: " + strconv.Itoa(x) }, // onOk
		func(e error) string { return "error: " + e.Error() },       // onErr
	)
	fmt.Println("Ok(42) folded to string:", result)

	// Fold with Err value
	errInt := mu.Err[int](errors.New("calculation failed"))
	result2 := mur.Fold(errInt,
		func(x int) string { return "success: " + strconv.Itoa(x) }, // onOk
		func(e error) string { return "error: " + e.Error() },       // onErr
	)
	fmt.Println("Err folded to string:", result2)

	// Fold to different type (int to bool)
	result3 := mur.Fold(okInt,
		func(x int) bool { return x > 0 },   // onOk
		func(_ error) bool { return false }, // onErr
	)
	fmt.Println("Ok(42) folded to bool:", result3)

	result4 := mur.Fold(errInt,
		func(x int) bool { return x > 0 },   // onOk
		func(_ error) bool { return false }, // onErr
	)
	fmt.Println("Err folded to bool:", result4)

	// Fold with complex calculations
	result5 := mur.Fold(okInt,
		func(x int) int { return x * x },            // onOk: square
		func(e error) int { return len(e.Error()) }, // onErr: error length
	)
	fmt.Println("Ok(42) folded (squared):", result5)

}
Output:

Ok(42) folded to string: success: 42
Err folded to string: error: calculation failed
Ok(42) folded to bool: true
Err folded to bool: false
Ok(42) folded (squared): 1764

func From

func From[T any](value T, err error) mu.Result[T]

From converts a result-error pair (commonly used in Go) into a `Result[T]`. If err is nil, it returns `Ok(value)`. If err is non-nil, it returns `Err(err)`.

Example
package main

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/appthrust/mu/mur"
)

func main() {
	// From with successful operation (nil error)
	value, err := strconv.Atoi("42")
	result := mur.From(value, err)
	fmt.Println("From successful Atoi:", result.OrZero())

	// From with failed operation (non-nil error)
	value2, err2 := strconv.Atoi("not-a-number")
	result2 := mur.From(value2, err2)
	fmt.Println("From failed Atoi:", result2.IsErr())
	if result2.IsErr() {
		fmt.Println("Error:", result2.ErrOrZero().Error())
	}

	// From with different types
	text := "hello world"
	result3 := mur.From(text, nil)
	fmt.Println("From string with nil error:", result3.OrZero())

	result4 := mur.From(text, errors.New("string error"))
	fmt.Println("From string with error:", result4.ErrOrZero().Error())

	// Practical example with file operation simulation
	readFile := func(filename string) (string, error) {
		if filename == "exists.txt" {
			return "file content", nil
		}
		return "", errors.New("file not found")
	}

	// Successful file read
	content, err := readFile("exists.txt")
	fileResult := mur.From(content, err)
	fmt.Println("From file read (success):", fileResult.OrZero())

	// Failed file read
	content2, err2 := readFile("missing.txt")
	fileResult2 := mur.From(content2, err2)
	fmt.Println("From file read (error):", fileResult2.ErrOrZero().Error())

}
Output:

From successful Atoi: 42
From failed Atoi: true
Error: strconv.Atoi: parsing "not-a-number": invalid syntax
From string with nil error: hello world
From string with error: string error
From file read (success): file content
From file read (error): file not found

func MapTo

func MapTo[T any, U any](r mu.Result[T], f func(T) U) mu.Result[U]

MapTo applies a function to an [Ok] value, transforming T to U. If the `Result[T]` is an [Err], it returns `Err[U]` with the same error. For type-changing transformations (T→U), use this instead of `Result.Map`. See package docs for details.

Example
package main

import (
	"errors"
	"fmt"
	"strconv"

	"github.com/appthrust/mu"
	"github.com/appthrust/mu/mur"
)

func main() {
	// MapTo with Ok value (int to string)
	okInt := mu.Ok(42)
	okStr := mur.MapTo(okInt, func(x int) string {
		return strconv.Itoa(x)
	})
	fmt.Println("Ok(42) mapped to string:", okStr.OrZero())

	// MapTo with Err value (preserves error)
	errInt := mu.Err[int](errors.New("calculation failed"))
	errStr := mur.MapTo(errInt, func(x int) string {
		return strconv.Itoa(x)
	})
	fmt.Println("Err mapped to string:", errStr.ErrOrZero().Error())

	// MapTo with different types (int to bool)
	okBool := mur.MapTo(okInt, func(x int) bool {
		return x > 0
	})
	fmt.Println("Ok(42) mapped to bool:", okBool.OrZero())

	// MapTo complex transformation
	okFloat := mur.MapTo(okInt, func(x int) float64 {
		return float64(x) / 2.0
	})
	fmt.Println("Ok(42) mapped to float:", okFloat.OrZero())

}
Output:

Ok(42) mapped to string: 42
Err mapped to string: calculation failed
Ok(42) mapped to bool: true
Ok(42) mapped to float: 21

Types

This section is empty.

Jump to

Keyboard shortcuts

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