mue

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 mue provides type-changing utility functions for `Either[L,R]`. These functions enable transformations where the Left or Right type changes (L→L2, R→R2), 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 FlatMapLeftTo

func FlatMapLeftTo[L any, R any, L2 any](e mu.Either[L, R], f func(L) mu.Either[L2, R]) mu.Either[L2, R]

FlatMapLeftTo applies a function to a [Left] value, transforming L to L2. If the `Either[L, R]` is a [Right], it returns `Right[L2, R]`. For type-changing transformations (L→L2), use this instead of `Either.FlatMapLeft`. See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

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

func main() {
	// FlatMapLeftTo with Left value (string to Either[int, string])
	leftStr := mu.Left[string, string]("42")
	leftInt := mue.FlatMapLeftTo(leftStr, func(s string) mu.Either[int, string] {
		if val, err := strconv.Atoi(s); err == nil {
			return mu.Left[int, string](val)
		}
		return mu.Right[int, string]("invalid number")
	})
	fmt.Println("Left(\"42\") flat mapped to int:", leftInt.LeftOrZero())

	// FlatMapLeftTo with invalid input
	leftInvalid := mu.Left[string, string]("abc")
	invalidResult := mue.FlatMapLeftTo(leftInvalid, func(s string) mu.Either[int, string] {
		if val, err := strconv.Atoi(s); err == nil {
			return mu.Left[int, string](val)
		}
		return mu.Right[int, string]("invalid number")
	})
	fmt.Println("Left(\"abc\") flat mapped (invalid):", invalidResult.OrZero())

	// FlatMapLeftTo with Right value (no transformation)
	rightValue := mu.Right[string, string]("original")
	rightMapped := mue.FlatMapLeftTo(rightValue, func(_ string) mu.Either[int, string] {
		return mu.Left[int, string](0)
	})
	fmt.Println("Right(\"original\") with left flat mapping:", rightMapped.OrZero())

}
Output:

Left("42") flat mapped to int: 42
Left("abc") flat mapped (invalid): invalid number
Right("original") with left flat mapping: original

func FlatMapTo

func FlatMapTo[L any, R any, R2 any](e mu.Either[L, R], f func(R) mu.Either[L, R2]) mu.Either[L, R2]

FlatMapTo applies a function to a [Right] value, transforming R to R2. If the `Either[L, R]` is a [Left], it returns `Left[L, R2]`. For type-changing transformations (R→R2), use this instead of `Either.FlatMap` which only supports R→R operations. See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

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

func main() {
	// FlatMapTo with Right value (int to Either[string, string])
	rightInt := mu.Right[string, int](42)
	rightStr := mue.FlatMapTo(rightInt, func(x int) mu.Either[string, string] {
		if x > 0 {
			return mu.Right[string, string](strconv.Itoa(x))
		}
		return mu.Left[string, string]("negative number")
	})
	fmt.Println("Right(42) flat mapped to string:", rightStr.OrZero())

	// FlatMapTo with negative input
	rightNegative := mu.Right[string, int](-10)
	negativeResult := mue.FlatMapTo(rightNegative, func(x int) mu.Either[string, string] {
		if x > 0 {
			return mu.Right[string, string](strconv.Itoa(x))
		}
		return mu.Left[string, string]("negative number")
	})
	fmt.Println("Right(-10) flat mapped (negative):", negativeResult.LeftOrZero())

	// FlatMapTo with Left value (no transformation)
	leftValue := mu.Left[string, int]("error")
	leftMapped := mue.FlatMapTo(leftValue, func(x int) mu.Either[string, string] {
		return mu.Right[string, string](strconv.Itoa(x))
	})
	fmt.Println("Left(\"error\") with right flat mapping:", leftMapped.LeftOrZero())

}
Output:

Right(42) flat mapped to string: 42
Right(-10) flat mapped (negative): negative number
Left("error") with right flat mapping: error

func Fold

func Fold[L any, R any, U any](e mu.Either[L, R], onLeft func(L) U, onRight func(R) U) U

Fold applies pattern matching functions to an `Either[L, R]`, returning U. Calls onLeft for [Left] values, onRight for [Right] values. For type-changing pattern matching (L,R→U), use this instead of method-based folding. See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"
	"strings"

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

func main() {
	// Fold with Left value (string, int to bool)
	leftStr := mu.Left[string, int]("error")
	result := mue.Fold(leftStr,
		func(s string) bool { return len(s) > 0 }, // onLeft
		func(x int) bool { return x > 0 },         // onRight
	)
	fmt.Println("Left(\"error\") folded to bool:", result)

	// Fold with Right value
	rightInt := mu.Right[string, int](42)
	result2 := mue.Fold(rightInt,
		func(s string) bool { return len(s) > 0 }, // onLeft
		func(x int) bool { return x > 0 },         // onRight
	)
	fmt.Println("Right(42) folded to bool:", result2)

	// Fold to different type (string, int to string)
	result3 := mue.Fold(leftStr,
		func(s string) string { return "error: " + s },            // onLeft
		func(x int) string { return "value: " + strconv.Itoa(x) }, // onRight
	)
	fmt.Println("Left(\"error\") folded to string:", result3)

	result4 := mue.Fold(rightInt,
		func(s string) string { return "error: " + s },            // onLeft
		func(x int) string { return "value: " + strconv.Itoa(x) }, // onRight
	)
	fmt.Println("Right(42) folded to string:", result4)

	// Fold with complex transformations
	leftUpper := mu.Left[string, int]("hello")
	result5 := mue.Fold(leftUpper,
		func(s string) int { return len(strings.ToUpper(s)) }, // onLeft
		func(x int) int { return x * 2 },                      // onRight
	)
	fmt.Println("Left(\"hello\") folded (uppercase length):", result5)

}
Output:

Left("error") folded to bool: true
Right(42) folded to bool: true
Left("error") folded to string: error: error
Right(42) folded to string: value: 42
Left("hello") folded (uppercase length): 5

func MapLeftTo

func MapLeftTo[L any, R any, L2 any](e mu.Either[L, R], f func(L) L2) mu.Either[L2, R]

MapLeftTo applies a function to a mu.Left value, transforming L to L2. If the `Either[L, R]` is a mu.Right, it returns `Right[L2, R]`. For type-changing transformations (L→L2), use this instead of mu.Either.MapLeft. See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

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

func main() {
	// MapLeftTo with Left value (string to int)
	leftStr := mu.Left[string, int]("42")
	leftInt := mue.MapLeftTo(leftStr, func(s string) int {
		val, _ := strconv.Atoi(s)
		return val
	})
	fmt.Println("Left(\"42\") mapped to int:", leftInt.LeftOrZero())

	// MapLeftTo with Right value (no transformation)
	rightValue := mu.Right[string, int](100)
	rightMapped := mue.MapLeftTo(rightValue, func(s string) int {
		val, _ := strconv.Atoi(s)
		return val
	})
	fmt.Println("Right(100) with left mapping:", rightMapped.OrZero())

	// MapLeftTo with different types (string to bool)
	leftBool := mue.MapLeftTo(leftStr, func(s string) bool {
		return len(s) > 0
	})
	fmt.Println("Left(\"42\") mapped to bool:", leftBool.LeftOrZero())

}
Output:

Left("42") mapped to int: 42
Right(100) with left mapping: 100
Left("42") mapped to bool: true

func MapTo

func MapTo[L any, R any, R2 any](e mu.Either[L, R], f func(R) R2) mu.Either[L, R2]

MapTo applies a function to a [Right] value, transforming R to R2. If the `Either[L, R]` is a [Left], it returns `Left[L, R2]`. For type-changing transformations (R→R2), use this instead of `Either.Map`. See package docs for details.

Example
package main

import (
	"fmt"
	"strconv"

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

func main() {
	// MapTo with Right value (int to string)
	rightInt := mu.Right[string, int](42)
	rightStr := mue.MapTo(rightInt, func(x int) string {
		return strconv.Itoa(x)
	})
	fmt.Println("Right(42) mapped to string:", rightStr.OrZero())

	// MapTo with Left value (no transformation)
	leftValue := mu.Left[string, int]("error")
	leftMapped := mue.MapTo(leftValue, func(x int) string {
		return strconv.Itoa(x)
	})
	fmt.Println("Left(\"error\") with right mapping:", leftMapped.LeftOrZero())

	// MapTo with different types (int to bool)
	rightBool := mue.MapTo(rightInt, func(x int) bool {
		return x > 0
	})
	fmt.Println("Right(42) mapped to bool:", rightBool.OrZero())

}
Output:

Right(42) mapped to string: 42
Left("error") with right mapping: error
Right(42) mapped to bool: true

Types

This section is empty.

Jump to

Keyboard shortcuts

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